1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::fields::Field;
use crate::fields::FieldSymbol;
use crate::input::CalendarError;
use crate::pattern::PatternError;
use displaydoc::Display;
use icu_calendar::any_calendar::AnyCalendarKind;
use icu_calendar::types::MonthCode;
use icu_decimal::DecimalError;
use icu_plurals::PluralsError;
use icu_provider::DataError;

#[cfg(feature = "std")]
impl std::error::Error for DateTimeError {}

/// A list of error outcomes for various operations in this module.
///
/// Re-exported as [`Error`](crate::Error).
#[derive(Display, Debug, Copy, Clone, PartialEq)]
#[non_exhaustive]
pub enum DateTimeError {
    /// An error originating from parsing a pattern.
    #[displaydoc("{0}")]
    Pattern(PatternError),
    /// An error originating from the [`Write`](std::fmt::Write) trait.
    #[displaydoc("{0}")]
    Format(core::fmt::Error),
    /// An error originating inside of the [data provider](icu_provider).
    #[displaydoc("{0}")]
    Data(DataError),
    /// An error originating from a missing field in datetime input.
    /// TODO: How can we return which field was missing?
    #[displaydoc("Missing input field")]
    MissingInputField(Option<&'static str>),
    /// An error originating from skeleton matching.
    #[displaydoc("{0}")]
    #[cfg(feature = "experimental")]
    Skeleton(crate::skeleton::SkeletonError),
    /// An error originating from an unsupported field in a datetime format.
    #[displaydoc("Unsupported field: {0:?}")]
    UnsupportedField(FieldSymbol),
    /// An unsupported field with a field length.
    // TODO(#2856): Consider renaming `UnsupportedField` to `UnsupportedFieldSymbol` so that
    // this variant can be named `UnsupportedField`.
    #[displaydoc("Unsupported field: {0:?}")]
    UnsupportedFormattingField(Field),
    /// An error due to there being no patterns for the given options.
    #[displaydoc("Unsupported options")]
    UnsupportedOptions,
    /// An error originating from [`PluralRules`][icu_plurals::PluralRules].
    #[displaydoc("{0}")]
    PluralRules(PluralsError),
    /// An error originating from [`DateTimeInput`][crate::input::DateTimeInput].
    #[displaydoc("{0}")]
    DateTimeInput(CalendarError),
    /// An error originating from a missing weekday symbol in the data.
    #[displaydoc("Data file missing weekday symbol for weekday {0}")]
    MissingWeekdaySymbol(usize),
    /// An error originating from a missing month symbol in the data.
    #[displaydoc("Data file missing month symbol for month code {0}")]
    MissingMonthSymbol(MonthCode),
    /// The FixedDecimalFormatter is not loaded
    #[displaydoc("Missing FixedDecimalFormatter")]
    FixedDecimal,
    /// An error originating from FixedDecimalFormatter
    #[displaydoc("{0}")]
    FixedDecimalFormatter(DecimalError),
    /// An error from mixing calendar types in [`DateTimeFormatter`](crate::DateTimeFormatter)
    #[displaydoc("DateTimeFormatter for {0} calendar was given a {1:?} calendar")]
    MismatchedAnyCalendar(AnyCalendarKind, Option<AnyCalendarKind>),
    /// Missing date symbols
    #[displaydoc("Missing date symbols")]
    MissingDateSymbols,
    /// Missing time symbols
    #[displaydoc("Missing time symbols")]
    MissingTimeSymbols,
    /// ordinal_rules must be set for PatternPlurals::MultipleVariants
    #[displaydoc("ordinal_rules must be set for PatternPlurals::MultipleVariants")]
    MissingOrdinalRules,
    /// The names for the given field are not loaded
    #[displaydoc("Missing names for {0:?}")]
    MissingNames(Field),
    /// The same field occurs multiple times in a pattern or was loaded multiple times
    #[displaydoc("Duplicate field: {0:?}")]
    DuplicateField(Field),
}

/// An error from mixing calendar types in [`DateTimeFormatter`](crate::DateTimeFormatter)
#[derive(Display, Debug, Copy, Clone, PartialEq)]
#[displaydoc("DateTimeFormatter for {this_kind} calendar was given a {date_kind:?} calendar")]
#[non_exhaustive]
pub struct MismatchedCalendarError {
    /// The calendar kind of the target object (formatter).
    pub this_kind: AnyCalendarKind,
    /// The calendar kind of the input object (date being formatted).
    /// Can be `None` if the input calendar was not specified.
    pub date_kind: Option<AnyCalendarKind>,
}

impl From<PatternError> for DateTimeError {
    fn from(e: PatternError) -> Self {
        DateTimeError::Pattern(e)
    }
}

impl From<DataError> for DateTimeError {
    fn from(e: DataError) -> Self {
        DateTimeError::Data(e)
    }
}

impl From<core::fmt::Error> for DateTimeError {
    fn from(e: core::fmt::Error) -> Self {
        DateTimeError::Format(e)
    }
}

impl From<PluralsError> for DateTimeError {
    fn from(e: PluralsError) -> Self {
        DateTimeError::PluralRules(e)
    }
}

impl From<CalendarError> for DateTimeError {
    fn from(e: CalendarError) -> Self {
        DateTimeError::DateTimeInput(e)
    }
}

impl From<DecimalError> for DateTimeError {
    fn from(e: DecimalError) -> Self {
        DateTimeError::FixedDecimalFormatter(e)
    }
}

impl From<MismatchedCalendarError> for DateTimeError {
    fn from(e: MismatchedCalendarError) -> Self {
        DateTimeError::MismatchedAnyCalendar(e.this_kind, e.date_kind)
    }
}