From cb3f7d00ba3094c3a20d3b29a72fd49b19d8d039 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 12 Dec 2024 16:16:58 -0800 Subject: [PATCH] custom_err --- components/datetime/src/error.rs | 6 +++ ffi/capi/src/datetime_formatter.rs | 65 ++++++++++++++++-------------- ffi/capi/src/decimal.rs | 13 +++--- ffi/capi/src/displaynames.rs | 14 ++----- ffi/capi/src/provider.rs | 13 ++++++ ffi/capi/src/zoned_formatter.rs | 24 +++++------ 6 files changed, 77 insertions(+), 58 deletions(-) diff --git a/components/datetime/src/error.rs b/components/datetime/src/error.rs index dde16a054e9..7363d79072d 100644 --- a/components/datetime/src/error.rs +++ b/components/datetime/src/error.rs @@ -30,6 +30,12 @@ pub enum DateTimeFormatterLoadError { Data(DataError), } +impl From for DateTimeFormatterLoadError { + fn from(error: DataError) -> Self { + Self::Data(error) + } +} + /// An error from mixing calendar types in a formatter. #[derive(Display, Debug, Copy, Clone, PartialEq)] #[displaydoc("DateTimeFormatter for {this_kind} calendar was given a {date_kind:?} calendar")] diff --git a/ffi/capi/src/datetime_formatter.rs b/ffi/capi/src/datetime_formatter.rs index 2e4b2fcda71..be5c923f78b 100644 --- a/ffi/capi/src/datetime_formatter.rs +++ b/ffi/capi/src/datetime_formatter.rs @@ -63,12 +63,13 @@ pub mod ffi { let prefs = (&locale.0).into(); let options = T::with_length(Length::from(length)).hm(); - Ok(Box::new(TimeFormatter(call_constructor!( - icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider, - provider, - prefs, - options - )?))) + Ok(Box::new(TimeFormatter( + provider.call_constructor_custom_err(move |provider| { + icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider( + provider, prefs, options, + ) + })?, + ))) } /// Formats a [`Time`] to a string. @@ -130,12 +131,13 @@ pub mod ffi { let prefs = (&locale.0).into(); let options = YMD::with_length(Length::from(length)); - Ok(Box::new(GregorianDateFormatter(call_constructor!( - icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider, - provider, - prefs, - options - )?))) + Ok(Box::new(GregorianDateFormatter( + provider.call_constructor_custom_err(move |provider| { + icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider( + provider, prefs, options, + ) + })?, + ))) } /// Formats a [`IsoDate`] to a string. @@ -193,12 +195,13 @@ pub mod ffi { let prefs = (&locale.0).into(); let options = YMDT::with_length(Length::from(length)).hm(); - Ok(Box::new(GregorianDateTimeFormatter(call_constructor!( - icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider, - provider, - prefs, - options - )?))) + Ok(Box::new(GregorianDateTimeFormatter( + provider.call_constructor_custom_err(move |provider| { + icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider( + provider, prefs, options, + ) + })?, + ))) } /// Formats a [`IsoDateTime`] to a string. @@ -245,12 +248,13 @@ pub mod ffi { let prefs = (&locale.0).into(); let options = YMD::with_length(Length::from(length)); - Ok(Box::new(DateFormatter(call_constructor!( - icu_datetime::DateTimeFormatter::try_new_with_buffer_provider, - provider, - prefs, - options - )?))) + Ok(Box::new(DateFormatter( + provider.call_constructor_custom_err(move |provider| { + icu_datetime::DateTimeFormatter::try_new_with_buffer_provider( + provider, prefs, options, + ) + })?, + ))) } /// Formats a [`Date`] to a string. @@ -333,12 +337,13 @@ pub mod ffi { let prefs = (&locale.0).into(); let options = YMDT::with_length(Length::from(length)).hm(); - Ok(Box::new(DateTimeFormatter(call_constructor!( - icu_datetime::DateTimeFormatter::try_new_with_buffer_provider, - provider, - prefs, - options - )?))) + Ok(Box::new(DateTimeFormatter( + provider.call_constructor_custom_err(move |provider| { + icu_datetime::DateTimeFormatter::try_new_with_buffer_provider( + provider, prefs, options, + ) + })?, + ))) } /// Formats a [`DateTime`] to a string. pub fn format_datetime( diff --git a/ffi/capi/src/decimal.rs b/ffi/capi/src/decimal.rs index 9f12de895ff..b5f905b87da 100644 --- a/ffi/capi/src/decimal.rs +++ b/ffi/capi/src/decimal.rs @@ -67,12 +67,13 @@ pub mod ffi { options.grouping_strategy = grouping_strategy .map(Into::into) .unwrap_or(options.grouping_strategy); - Ok(Box::new(FixedDecimalFormatter(call_constructor!( - icu_decimal::FixedDecimalFormatter::try_new_with_buffer_provider, - provider, - prefs, - options, - )?))) + Ok(Box::new(FixedDecimalFormatter( + provider.call_constructor_custom_err(move |provider| { + icu_decimal::FixedDecimalFormatter::try_new_with_buffer_provider( + provider, prefs, options, + ) + })?, + ))) } /// Creates a new [`FixedDecimalFormatter`] from preconstructed locale data. diff --git a/ffi/capi/src/displaynames.rs b/ffi/capi/src/displaynames.rs index df072d170cf..8441fe2689b 100644 --- a/ffi/capi/src/displaynames.rs +++ b/ffi/capi/src/displaynames.rs @@ -96,12 +96,9 @@ pub mod ffi { let options = icu_experimental::displaynames::DisplayNamesOptions::from(options); Ok(Box::new(LocaleDisplayNamesFormatter( - call_constructor!( - icu_experimental::displaynames::LocaleDisplayNamesFormatter::try_new_with_buffer_provider, - provider, - prefs, + provider.call_constructor_custom_err(move |provider| icu_experimental::displaynames::LocaleDisplayNamesFormatter::try_new_with_buffer_provider(provider, prefs, options, - )?, + ))?, ))) } @@ -145,12 +142,9 @@ pub mod ffi { ) -> Result, DataError> { let prefs = (&locale.0).into(); let options = icu_experimental::displaynames::DisplayNamesOptions::from(options); - Ok(Box::new(RegionDisplayNames(call_constructor!( - icu_experimental::displaynames::RegionDisplayNames::try_new_with_buffer_provider, - provider, - prefs, + Ok(Box::new(RegionDisplayNames(provider.call_constructor_custom_err(move |provider| icu_experimental::displaynames::RegionDisplayNames::try_new_with_buffer_provider(provider, prefs, options - )?))) + ))?))) } /// Returns the locale specific display name of a region. diff --git a/ffi/capi/src/provider.rs b/ffi/capi/src/provider.rs index 4df982c0601..d10e9b78f23 100644 --- a/ffi/capi/src/provider.rs +++ b/ffi/capi/src/provider.rs @@ -50,6 +50,19 @@ pub mod ffi { } } + pub(crate) fn call_constructor_custom_err(&self, ctor: F) -> Result + where + Err: From, + F: FnOnce(&(dyn icu_provider::buf::BufferProvider + 'static)) -> Result, + { + match &self.0 { + DataProviderInner::Destroyed => Err(icu_provider::DataError::custom( + "This provider has been destroyed", + ))?, + DataProviderInner::Buffer(ref buffer_provider) => ctor(buffer_provider), + } + } + /// Constructs an `FsDataProvider` and returns it as an [`DataProvider`]. /// Requires the `provider_fs` Cargo feature. /// Not supported in WASM. diff --git a/ffi/capi/src/zoned_formatter.rs b/ffi/capi/src/zoned_formatter.rs index 9feec66b610..c8621fc4804 100644 --- a/ffi/capi/src/zoned_formatter.rs +++ b/ffi/capi/src/zoned_formatter.rs @@ -61,12 +61,11 @@ pub mod ffi { let options = YMDTV::with_length(Length::from(length)); Ok(Box::new(GregorianZonedDateTimeFormatter( - call_constructor!( - icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider, - provider, - prefs, - options - )?, + provider.call_constructor_custom_err(move |provider| { + icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider( + provider, prefs, options, + ) + })?, ))) } /// Formats a [`IsoDateTime`] and [`TimeZoneInfo`] to a string. @@ -128,12 +127,13 @@ pub mod ffi { let prefs = (&locale.0).into(); let options = YMDTV::with_length(Length::from(length)); - Ok(Box::new(ZonedDateTimeFormatter(call_constructor!( - icu_datetime::DateTimeFormatter::try_new_with_buffer_provider, - provider, - prefs, - options, - )?))) + Ok(Box::new(ZonedDateTimeFormatter( + provider.call_constructor_custom_err(move |provider| { + icu_datetime::DateTimeFormatter::try_new_with_buffer_provider( + provider, prefs, options, + ) + })?, + ))) } /// Formats a [`DateTime`] and [`TimeZoneInfo`] to a string. pub fn format_datetime_with_custom_time_zone(