use icu_calendar::week::WeekCalculator;
use icu_calendar::{AnyCalendar, CalendarError};
use icu_decimal::options::FixedDecimalFormatterOptions;
use icu_decimal::{DecimalError, FixedDecimalFormatter};
use icu_provider::prelude::*;
pub(crate) trait FixedDecimalFormatterLoader {
fn load(
&self,
locale: &DataLocale,
options: FixedDecimalFormatterOptions,
) -> Result<FixedDecimalFormatter, DataError>;
}
pub(crate) trait WeekCalculatorLoader {
fn load(&self, locale: &DataLocale) -> Result<WeekCalculator, DataError>;
}
pub(crate) trait AnyCalendarLoader {
fn load(&self, locale: &DataLocale) -> Result<AnyCalendar, DataError>;
}
pub(crate) struct PhantomLoader {
_not_constructible: core::convert::Infallible,
}
impl FixedDecimalFormatterLoader for PhantomLoader {
fn load(
&self,
_locale: &DataLocale,
_options: FixedDecimalFormatterOptions,
) -> Result<FixedDecimalFormatter, DataError> {
unreachable!() }
}
impl WeekCalculatorLoader for PhantomLoader {
#[inline]
fn load(&self, _locale: &DataLocale) -> Result<WeekCalculator, DataError> {
unreachable!() }
}
impl AnyCalendarLoader for PhantomLoader {
#[inline]
fn load(&self, _locale: &DataLocale) -> Result<AnyCalendar, DataError> {
unreachable!() }
}
#[cfg(feature = "compiled_data")]
pub(crate) struct ExternalLoaderCompiledData;
#[cfg(feature = "compiled_data")]
impl FixedDecimalFormatterLoader for ExternalLoaderCompiledData {
#[inline]
fn load(
&self,
locale: &DataLocale,
options: FixedDecimalFormatterOptions,
) -> Result<FixedDecimalFormatter, DataError> {
FixedDecimalFormatter::try_new(locale, options).map_err(|e| match e {
DecimalError::Data(e) => e,
_ => unreachable!(),
})
}
}
#[cfg(feature = "compiled_data")]
impl WeekCalculatorLoader for ExternalLoaderCompiledData {
#[inline]
fn load(&self, locale: &DataLocale) -> Result<WeekCalculator, DataError> {
WeekCalculator::try_new(locale).map_err(|e| match e {
CalendarError::Data(e) => e,
_ => unreachable!(),
})
}
}
#[cfg(feature = "compiled_data")]
impl AnyCalendarLoader for ExternalLoaderCompiledData {
#[inline]
fn load(&self, locale: &DataLocale) -> Result<AnyCalendar, DataError> {
Ok(AnyCalendar::new_for_locale(locale)).map_err(|e| match e {
CalendarError::Data(e) => e,
_ => unreachable!(),
})
}
}
pub(crate) struct ExternalLoaderAny<'a, P: ?Sized>(pub &'a P);
impl<P> FixedDecimalFormatterLoader for ExternalLoaderAny<'_, P>
where
P: ?Sized + AnyProvider,
{
#[inline]
fn load(
&self,
locale: &DataLocale,
options: FixedDecimalFormatterOptions,
) -> Result<FixedDecimalFormatter, DataError> {
FixedDecimalFormatter::try_new_with_any_provider(self.0, locale, options).map_err(|e| {
match e {
DecimalError::Data(e) => e,
_ => unreachable!(),
}
})
}
}
impl<P> WeekCalculatorLoader for ExternalLoaderAny<'_, P>
where
P: ?Sized + AnyProvider,
{
#[inline]
fn load(&self, locale: &DataLocale) -> Result<WeekCalculator, DataError> {
WeekCalculator::try_new_with_any_provider(self.0, locale).map_err(|e| match e {
CalendarError::Data(e) => e,
_ => unreachable!(),
})
}
}
impl<P> AnyCalendarLoader for ExternalLoaderAny<'_, P>
where
P: ?Sized + AnyProvider,
{
#[inline]
fn load(&self, locale: &DataLocale) -> Result<AnyCalendar, DataError> {
AnyCalendar::try_new_for_locale_with_any_provider(self.0, locale).map_err(|e| match e {
CalendarError::Data(e) => e,
_ => unreachable!(),
})
}
}
#[cfg(feature = "serde")]
pub(crate) struct ExternalLoaderBuffer<'a, P: ?Sized>(pub &'a P);
#[cfg(feature = "serde")]
impl<P> FixedDecimalFormatterLoader for ExternalLoaderBuffer<'_, P>
where
P: ?Sized + BufferProvider,
{
#[inline]
fn load(
&self,
locale: &DataLocale,
options: FixedDecimalFormatterOptions,
) -> Result<FixedDecimalFormatter, DataError> {
FixedDecimalFormatter::try_new_with_buffer_provider(self.0, locale, options).map_err(|e| {
match e {
DecimalError::Data(e) => e,
_ => unreachable!(),
}
})
}
}
#[cfg(feature = "serde")]
impl<P> WeekCalculatorLoader for ExternalLoaderBuffer<'_, P>
where
P: ?Sized + BufferProvider,
{
#[inline]
fn load(&self, locale: &DataLocale) -> Result<WeekCalculator, DataError> {
WeekCalculator::try_new_with_buffer_provider(self.0, locale).map_err(|e| match e {
CalendarError::Data(e) => e,
_ => unreachable!(),
})
}
}
#[cfg(feature = "serde")]
impl<P> AnyCalendarLoader for ExternalLoaderBuffer<'_, P>
where
P: ?Sized + BufferProvider,
{
#[inline]
fn load(&self, locale: &DataLocale) -> Result<AnyCalendar, DataError> {
AnyCalendar::try_new_for_locale_with_buffer_provider(self.0, locale).map_err(|e| match e {
CalendarError::Data(e) => e,
_ => unreachable!(),
})
}
}
pub(crate) struct ExternalLoaderUnstable<'a, P: ?Sized>(pub &'a P);
impl<P> FixedDecimalFormatterLoader for ExternalLoaderUnstable<'_, P>
where
P: ?Sized + DataProvider<icu_decimal::provider::DecimalSymbolsV1Marker>,
{
#[inline]
fn load(
&self,
locale: &DataLocale,
options: FixedDecimalFormatterOptions,
) -> Result<FixedDecimalFormatter, DataError> {
FixedDecimalFormatter::try_new_unstable(self.0, locale, options).map_err(|e| match e {
DecimalError::Data(e) => e,
_ => unreachable!(),
})
}
}
impl<P> WeekCalculatorLoader for ExternalLoaderUnstable<'_, P>
where
P: ?Sized + DataProvider<icu_calendar::provider::WeekDataV2Marker>,
{
#[inline]
fn load(&self, locale: &DataLocale) -> Result<WeekCalculator, DataError> {
WeekCalculator::try_new_unstable(self.0, locale).map_err(|e| match e {
CalendarError::Data(e) => e,
_ => unreachable!(),
})
}
}
impl<P> AnyCalendarLoader for ExternalLoaderUnstable<'_, P>
where
P: DataProvider<icu_calendar::provider::JapaneseErasV1Marker>
+ DataProvider<icu_calendar::provider::JapaneseExtendedErasV1Marker>
+ DataProvider<icu_calendar::provider::ChineseCacheV1Marker>
+ DataProvider<icu_calendar::provider::DangiCacheV1Marker>
+ DataProvider<icu_calendar::provider::IslamicObservationalCacheV1Marker>
+ DataProvider<icu_calendar::provider::IslamicUmmAlQuraCacheV1Marker>
+ ?Sized,
{
#[inline]
fn load(&self, locale: &DataLocale) -> Result<AnyCalendar, DataError> {
AnyCalendar::try_new_for_locale_unstable(self.0, locale).map_err(|e| match e {
CalendarError::Data(e) => e,
_ => unreachable!(),
})
}
}