Skip to content

Commit

Permalink
custom_err
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Dec 13, 2024
1 parent c97ece8 commit cb3f7d0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 58 deletions.
6 changes: 6 additions & 0 deletions components/datetime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub enum DateTimeFormatterLoadError {
Data(DataError),
}

impl From<DataError> 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")]
Expand Down
65 changes: 35 additions & 30 deletions ffi/capi/src/datetime_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
13 changes: 7 additions & 6 deletions ffi/capi/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 4 additions & 10 deletions ffi/capi/src/displaynames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)?,
))?,
)))
}

Expand Down Expand Up @@ -145,12 +142,9 @@ pub mod ffi {
) -> Result<Box<RegionDisplayNames>, 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.
Expand Down
13 changes: 13 additions & 0 deletions ffi/capi/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ pub mod ffi {
}
}

pub(crate) fn call_constructor_custom_err<F, Ret, Err>(&self, ctor: F) -> Result<Ret, Err>
where
Err: From<icu_provider::DataError>,
F: FnOnce(&(dyn icu_provider::buf::BufferProvider + 'static)) -> Result<Ret, Err>,
{
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.
Expand Down
24 changes: 12 additions & 12 deletions ffi/capi/src/zoned_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit cb3f7d0

Please sign in to comment.