errors.rs (12208B)
1 // This file is part of ICU4X. For terms of use, please see the file 2 // called LICENSE at the top level of the ICU4X source tree 3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). 4 5 use ffi::*; 6 7 #[diplomat::bridge] 8 #[diplomat::abi_rename = "icu4x_{0}_mv1"] 9 #[diplomat::attr(auto, namespace = "icu4x")] 10 pub mod ffi { 11 #[cfg(feature = "datetime")] 12 use diplomat_runtime::DiplomatOption; 13 14 #[cfg(feature = "datetime")] 15 use crate::unstable::calendar::ffi::CalendarKind; 16 17 #[derive(Debug, PartialEq, Eq)] 18 #[repr(C)] 19 #[diplomat::rust_link(icu_provider::DataError, Struct, compact)] 20 #[diplomat::rust_link(icu_provider::DataErrorKind, Enum, compact)] 21 pub enum DataError { 22 Unknown = 0x00, 23 MarkerNotFound = 0x01, 24 IdentifierNotFound = 0x02, 25 InvalidRequest = 0x03, 26 InconsistentData = 0x04, 27 Downcast = 0x05, 28 Deserialize = 0x06, 29 Custom = 0x07, 30 Io = 0x08, 31 } 32 33 #[derive(Debug, PartialEq, Eq)] 34 #[repr(C)] 35 #[diplomat::rust_link(icu::locale::ParseError, Enum, compact)] 36 pub enum LocaleParseError { 37 Unknown = 0x00, 38 Language = 0x01, 39 Subtag = 0x02, 40 Extension = 0x03, 41 } 42 43 #[derive(Debug, PartialEq, Eq)] 44 #[repr(C)] 45 #[diplomat::rust_link(fixed_decimal::ParseError, Enum, compact)] 46 #[cfg(any(feature = "decimal", feature = "plurals"))] 47 pub enum DecimalParseError { 48 Unknown = 0x00, 49 Limit = 0x01, 50 Syntax = 0x02, 51 } 52 53 #[derive(Debug, PartialEq, Eq)] 54 #[diplomat::rust_link(fixed_decimal::LimitError, Struct, compact)] 55 #[cfg(feature = "decimal")] 56 pub struct DecimalLimitError; 57 58 #[derive(Debug, PartialEq, Eq)] 59 #[repr(C)] 60 #[diplomat::rust_link(icu::calendar::RangeError, Struct, compact)] 61 #[diplomat::rust_link(icu::calendar::DateError, Enum, compact)] 62 #[cfg(any(feature = "datetime", feature = "timezone", feature = "calendar"))] 63 pub enum CalendarError { 64 Unknown = 0x00, 65 OutOfRange = 0x01, 66 UnknownEra = 0x02, 67 UnknownMonthCode = 0x03, 68 } 69 70 #[derive(Debug, PartialEq, Eq)] 71 #[repr(C)] 72 #[diplomat::rust_link(icu::calendar::ParseError, Enum, compact)] 73 #[diplomat::rust_link(icu::time::ParseError, Enum, compact)] 74 #[cfg(any(feature = "datetime", feature = "timezone", feature = "calendar"))] 75 pub enum Rfc9557ParseError { 76 Unknown = 0x00, 77 InvalidSyntax = 0x01, 78 OutOfRange = 0x02, 79 MissingFields = 0x03, 80 UnknownCalendar = 0x04, 81 } 82 83 #[derive(Debug, PartialEq, Eq)] 84 #[diplomat::rust_link(icu::time::zone::InvalidOffsetError, Struct, compact)] 85 #[cfg(any(feature = "datetime", feature = "timezone"))] 86 pub struct TimeZoneInvalidOffsetError; 87 88 #[derive(Debug, PartialEq, Eq)] 89 #[repr(C)] 90 #[diplomat::rust_link(icu::datetime::DateTimeFormatterLoadError, Enum, compact)] 91 #[diplomat::rust_link(icu::datetime::pattern::PatternLoadError, Enum, compact)] 92 #[diplomat::rust_link(icu_provider::DataError, Struct, compact)] 93 #[diplomat::rust_link(icu_provider::DataErrorKind, Enum, compact)] 94 pub enum DateTimeFormatterLoadError { 95 Unknown = 0x00, 96 97 InvalidDateFields = 0x8_01, 98 UnsupportedLength = 0x8_03, 99 ConflictingField = 0x8_09, 100 FormatterTooSpecific = 0x8_0A, 101 102 DataMarkerNotFound = 0x01, 103 DataIdentifierNotFound = 0x02, 104 DataInvalidRequest = 0x03, 105 DataInconsistentData = 0x04, 106 DataDowncast = 0x05, 107 DataDeserialize = 0x06, 108 DataCustom = 0x07, 109 DataIo = 0x08, 110 } 111 112 #[cfg(feature = "datetime")] 113 #[diplomat::rust_link(icu::datetime::MismatchedCalendarError, Struct)] 114 pub struct DateTimeMismatchedCalendarError { 115 pub this_kind: CalendarKind, 116 pub date_kind: DiplomatOption<CalendarKind>, 117 } 118 119 /// An error when formatting a datetime. 120 /// 121 /// Currently the only reachable error here is a missing time zone variant. If you encounter 122 /// that error, you need to call `with_variant` or `infer_variant` on your `TimeZoneInfo`. 123 #[cfg(feature = "datetime")] 124 #[derive(Debug, PartialEq, Eq)] 125 #[repr(C)] 126 #[diplomat::rust_link( 127 icu::datetime::unchecked::FormattedDateTimeUncheckedError, 128 Enum, 129 compact 130 )] 131 pub enum DateTimeWriteError { 132 Unknown = 0x00, 133 MissingTimeZoneVariant = 0x01, 134 } 135 } 136 137 impl From<icu_provider::DataError> for DataError { 138 fn from(e: icu_provider::DataError) -> Self { 139 match e.kind { 140 icu_provider::DataErrorKind::MarkerNotFound => Self::MarkerNotFound, 141 icu_provider::DataErrorKind::IdentifierNotFound => Self::IdentifierNotFound, 142 icu_provider::DataErrorKind::InvalidRequest => Self::InvalidRequest, 143 icu_provider::DataErrorKind::InconsistentData(..) => Self::InconsistentData, 144 icu_provider::DataErrorKind::Downcast(..) => Self::Downcast, 145 icu_provider::DataErrorKind::Deserialize => Self::Deserialize, 146 icu_provider::DataErrorKind::Custom => Self::Custom, 147 #[cfg(all( 148 feature = "provider_fs", 149 not(any(target_arch = "wasm32", target_os = "none")) 150 ))] 151 icu_provider::DataErrorKind::Io(..) => Self::Io, 152 _ => Self::Unknown, 153 } 154 } 155 } 156 157 #[cfg(any(feature = "datetime", feature = "timezone", feature = "calendar"))] 158 impl From<icu_calendar::RangeError> for CalendarError { 159 fn from(_: icu_calendar::RangeError) -> Self { 160 Self::OutOfRange 161 } 162 } 163 164 #[cfg(any(feature = "datetime", feature = "timezone", feature = "calendar"))] 165 impl From<icu_calendar::DateError> for CalendarError { 166 fn from(e: icu_calendar::DateError) -> Self { 167 match e { 168 icu_calendar::DateError::Range { .. } => Self::OutOfRange, 169 icu_calendar::DateError::UnknownEra => Self::UnknownEra, 170 icu_calendar::DateError::UnknownMonthCode(..) => Self::UnknownMonthCode, 171 _ => Self::Unknown, 172 } 173 } 174 } 175 176 #[cfg(any(feature = "datetime", feature = "timezone", feature = "calendar"))] 177 impl From<icu_calendar::ParseError> for Rfc9557ParseError { 178 fn from(e: icu_calendar::ParseError) -> Self { 179 match e { 180 icu_calendar::ParseError::Syntax(_) => Self::InvalidSyntax, 181 icu_calendar::ParseError::MissingFields => Self::MissingFields, 182 icu_calendar::ParseError::Range(_) => Self::OutOfRange, 183 icu_calendar::ParseError::UnknownCalendar => Self::UnknownCalendar, 184 _ => Self::Unknown, 185 } 186 } 187 } 188 189 #[cfg(any(feature = "datetime", feature = "timezone", feature = "calendar"))] 190 impl From<icu_time::ParseError> for Rfc9557ParseError { 191 fn from(e: icu_time::ParseError) -> Self { 192 match e { 193 icu_time::ParseError::Syntax(_) => Self::InvalidSyntax, 194 icu_time::ParseError::MissingFields => Self::MissingFields, 195 icu_time::ParseError::Range(_) => Self::OutOfRange, 196 icu_time::ParseError::UnknownCalendar => Self::UnknownCalendar, 197 // TODO 198 _ => Self::Unknown, 199 } 200 } 201 } 202 203 #[cfg(feature = "datetime")] 204 impl From<icu_datetime::DateTimeFormatterLoadError> for DateTimeFormatterLoadError { 205 fn from(e: icu_datetime::DateTimeFormatterLoadError) -> Self { 206 match e { 207 icu_datetime::DateTimeFormatterLoadError::Names( 208 icu_datetime::pattern::PatternLoadError::ConflictingField { .. }, 209 ) => Self::ConflictingField, 210 icu_datetime::DateTimeFormatterLoadError::Names( 211 icu_datetime::pattern::PatternLoadError::UnsupportedLength(_), 212 ) => Self::UnsupportedLength, 213 icu_datetime::DateTimeFormatterLoadError::Names( 214 icu_datetime::pattern::PatternLoadError::FormatterTooSpecific(_), 215 ) => Self::FormatterTooSpecific, 216 icu_datetime::DateTimeFormatterLoadError::Names( 217 icu_datetime::pattern::PatternLoadError::Data(data_error, _), 218 ) => data_error.into(), 219 icu_datetime::DateTimeFormatterLoadError::Data(data_error) => data_error.into(), 220 _ => Self::Unknown, 221 } 222 } 223 } 224 225 #[cfg(feature = "datetime")] 226 impl From<icu_provider::DataError> for DateTimeFormatterLoadError { 227 fn from(e: icu_provider::DataError) -> Self { 228 match e.kind { 229 icu_provider::DataErrorKind::MarkerNotFound => Self::DataMarkerNotFound, 230 icu_provider::DataErrorKind::IdentifierNotFound => Self::DataIdentifierNotFound, 231 icu_provider::DataErrorKind::InvalidRequest => Self::DataInvalidRequest, 232 icu_provider::DataErrorKind::InconsistentData(..) => Self::DataInconsistentData, 233 icu_provider::DataErrorKind::Downcast(..) => Self::DataDowncast, 234 icu_provider::DataErrorKind::Deserialize => Self::DataDeserialize, 235 icu_provider::DataErrorKind::Custom => Self::DataCustom, 236 #[cfg(all( 237 feature = "provider_fs", 238 not(any(target_arch = "wasm32", target_os = "none")) 239 ))] 240 icu_provider::DataErrorKind::Io(..) => Self::DataIo, 241 _ => Self::Unknown, 242 } 243 } 244 } 245 246 #[cfg(feature = "datetime")] 247 impl From<icu_datetime::pattern::PatternLoadError> for ffi::DateTimeFormatterLoadError { 248 fn from(value: icu_datetime::pattern::PatternLoadError) -> Self { 249 match value { 250 icu_datetime::pattern::PatternLoadError::ConflictingField { .. } => { 251 Self::ConflictingField 252 } 253 icu_datetime::pattern::PatternLoadError::UnsupportedLength(_) => { 254 Self::UnsupportedLength 255 } 256 icu_datetime::pattern::PatternLoadError::FormatterTooSpecific(_) => { 257 Self::FormatterTooSpecific 258 } 259 icu_datetime::pattern::PatternLoadError::Data(data_error, _) => data_error.into(), 260 _ => Self::Unknown, 261 } 262 } 263 } 264 265 #[cfg(feature = "datetime")] 266 impl From<icu_datetime::MismatchedCalendarError> for ffi::DateTimeMismatchedCalendarError { 267 fn from(value: icu_datetime::MismatchedCalendarError) -> Self { 268 Self { 269 this_kind: value.this_kind.into(), 270 date_kind: value.date_kind.map(Into::into).into(), 271 } 272 } 273 } 274 275 #[cfg(feature = "datetime")] 276 impl From<icu_datetime::unchecked::FormattedDateTimeUncheckedError> for DateTimeWriteError { 277 fn from(value: icu_datetime::unchecked::FormattedDateTimeUncheckedError) -> Self { 278 match value { 279 icu_datetime::unchecked::FormattedDateTimeUncheckedError::MissingInputField( 280 icu_datetime::unchecked::MissingInputFieldKind::TimeZoneVariant, 281 ) => Self::MissingTimeZoneVariant, 282 err => { 283 debug_assert!(false, "unexpected datetime formatting error: {err}"); 284 Self::Unknown 285 } 286 } 287 } 288 } 289 290 #[cfg(any(feature = "decimal", feature = "plurals"))] 291 impl From<fixed_decimal::ParseError> for DecimalParseError { 292 fn from(e: fixed_decimal::ParseError) -> Self { 293 match e { 294 fixed_decimal::ParseError::Limit => Self::Limit, 295 fixed_decimal::ParseError::Syntax => Self::Syntax, 296 _ => Self::Unknown, 297 } 298 } 299 } 300 301 #[cfg(feature = "decimal")] 302 impl From<fixed_decimal::LimitError> for DecimalLimitError { 303 fn from(_: fixed_decimal::LimitError) -> Self { 304 Self 305 } 306 } 307 308 impl From<icu_locale_core::ParseError> for LocaleParseError { 309 fn from(e: icu_locale_core::ParseError) -> Self { 310 match e { 311 icu_locale_core::ParseError::InvalidLanguage => Self::Language, 312 icu_locale_core::ParseError::InvalidSubtag => Self::Subtag, 313 icu_locale_core::ParseError::InvalidExtension => Self::Extension, 314 icu_locale_core::ParseError::DuplicatedExtension => Self::Extension, 315 _ => Self::Unknown, 316 } 317 } 318 } 319 320 #[cfg(any(feature = "timezone", feature = "datetime"))] 321 impl From<icu_time::zone::InvalidOffsetError> for TimeZoneInvalidOffsetError { 322 fn from(_: icu_time::zone::InvalidOffsetError) -> Self { 323 Self 324 } 325 }