#[diplomat::bridge]
#[diplomat::abi_rename = "icu4x_{0}_mv1"]
#[diplomat::attr(auto, namespace = "icu4x")]
pub mod ffi {
use alloc::boxed::Box;
use icu_datetime::fieldsets::{T, YMD, YMDT};
#[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
use icu_datetime::options::Length;
#[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
use crate::errors::ffi::DateTimeFormatterLoadError;
#[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
use crate::locale_core::ffi::Locale;
#[cfg(feature = "buffer_provider")]
use crate::provider::ffi::DataProvider;
use crate::{
calendar::ffi::AnyCalendarKind,
date::ffi::{Date, IsoDate},
datetime::ffi::{DateTime, IsoDateTime},
errors::ffi::DateTimeFormatError,
time::ffi::Time,
};
use writeable::Writeable;
#[diplomat::opaque]
#[diplomat::rust_link(icu::datetime, Mod)]
pub struct TimeFormatter(pub icu_datetime::FixedCalendarDateTimeFormatter<(), T>);
#[diplomat::enum_convert(icu_datetime::options::Length, needs_wildcard)]
#[diplomat::rust_link(icu::datetime::options::Length, Enum)]
pub enum DateTimeLength {
Long,
Medium,
Short,
}
impl TimeFormatter {
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_length")]
#[diplomat::demo(default_constructor)]
#[cfg(feature = "compiled_data")]
pub fn create_with_length(
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<TimeFormatter>, DateTimeFormatterLoadError> {
let prefs = (&locale.0).into();
let options = T::with_length(Length::from(length)).hm();
Ok(Box::new(TimeFormatter(
icu_datetime::FixedCalendarDateTimeFormatter::try_new(prefs, options)?,
)))
}
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_length_and_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_length_and_provider(
provider: &DataProvider,
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<TimeFormatter>, DateTimeFormatterLoadError> {
let prefs = (&locale.0).into();
let options = T::with_length(Length::from(length)).hm();
Ok(Box::new(TimeFormatter(
provider.call_constructor_custom_err(move |provider| {
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider(
provider, prefs, options,
)
})?,
)))
}
pub fn format_time(&self, value: &Time, write: &mut diplomat_runtime::DiplomatWrite) {
let _infallible = self.0.format(&value.0).write_to(write);
}
pub fn format_datetime(
&self,
value: &DateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) {
let _infallible = self.0.format(&value.0.time).write_to(write);
}
pub fn format_iso_datetime(
&self,
value: &IsoDateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) {
let _infallible = self.0.format(&value.0.time).write_to(write);
}
}
#[diplomat::opaque]
#[diplomat::rust_link(icu::datetime, Mod)]
pub struct GregorianDateFormatter(
pub icu_datetime::FixedCalendarDateTimeFormatter<icu_calendar::Gregorian, YMD>,
);
impl GregorianDateFormatter {
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_length")]
#[diplomat::demo(default_constructor)]
#[cfg(feature = "compiled_data")]
pub fn create_with_length(
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<GregorianDateFormatter>, DateTimeFormatterLoadError> {
let prefs = (&locale.0).into();
let options = YMD::with_length(Length::from(length));
Ok(Box::new(GregorianDateFormatter(
icu_datetime::FixedCalendarDateTimeFormatter::try_new(prefs, options)?,
)))
}
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_length_and_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_length_and_provider(
provider: &DataProvider,
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<GregorianDateFormatter>, DateTimeFormatterLoadError> {
let prefs = (&locale.0).into();
let options = YMD::with_length(Length::from(length));
Ok(Box::new(GregorianDateFormatter(
provider.call_constructor_custom_err(move |provider| {
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider(
provider, prefs, options,
)
})?,
)))
}
pub fn format_iso_date(
&self,
value: &IsoDate,
write: &mut diplomat_runtime::DiplomatWrite,
) {
let greg = icu_calendar::Date::new_from_iso(value.0, icu_calendar::Gregorian);
let _infallible = self.0.format(&greg).write_to(write);
}
pub fn format_iso_datetime(
&self,
value: &IsoDateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) {
let greg = icu_calendar::DateTime::new_from_iso(value.0, icu_calendar::Gregorian);
let _infallible = self.0.format(&greg).write_to(write);
}
}
#[diplomat::opaque]
#[diplomat::rust_link(icu::datetime, Mod)]
pub struct GregorianDateTimeFormatter(
pub icu_datetime::FixedCalendarDateTimeFormatter<icu_calendar::Gregorian, YMDT>,
);
impl GregorianDateTimeFormatter {
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_length")]
#[diplomat::demo(default_constructor)]
#[cfg(feature = "compiled_data")]
pub fn create_with_length(
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<GregorianDateTimeFormatter>, DateTimeFormatterLoadError> {
let prefs = (&locale.0).into();
let options = YMDT::with_length(Length::from(length)).hm();
Ok(Box::new(GregorianDateTimeFormatter(
icu_datetime::FixedCalendarDateTimeFormatter::try_new(prefs, options)?,
)))
}
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_length_and_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_length_and_provider(
provider: &DataProvider,
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<GregorianDateTimeFormatter>, DateTimeFormatterLoadError> {
let prefs = (&locale.0).into();
let options = YMDT::with_length(Length::from(length)).hm();
Ok(Box::new(GregorianDateTimeFormatter(
provider.call_constructor_custom_err(move |provider| {
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider(
provider, prefs, options,
)
})?,
)))
}
pub fn format_iso_datetime(
&self,
value: &IsoDateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) {
let greg = icu_calendar::DateTime::new_from_iso(value.0, icu_calendar::Gregorian);
let _infallible = self.0.format(&greg).write_to(write);
}
}
#[diplomat::opaque]
#[diplomat::rust_link(icu::datetime, Mod)]
pub struct DateFormatter(pub icu_datetime::DateTimeFormatter<YMD>);
impl DateFormatter {
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_length")]
#[diplomat::demo(default_constructor)]
#[cfg(feature = "compiled_data")]
pub fn create_with_length(
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<DateFormatter>, DateTimeFormatterLoadError> {
let prefs = (&locale.0).into();
let options = YMD::with_length(Length::from(length));
Ok(Box::new(DateFormatter(
icu_datetime::DateTimeFormatter::try_new(prefs, options)?,
)))
}
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_length_and_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_length_and_provider(
provider: &DataProvider,
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<DateFormatter>, DateTimeFormatterLoadError> {
let prefs = (&locale.0).into();
let options = YMD::with_length(Length::from(length));
Ok(Box::new(DateFormatter(
provider.call_constructor_custom_err(move |provider| {
icu_datetime::DateTimeFormatter::try_new_with_buffer_provider(
provider, prefs, options,
)
})?,
)))
}
pub fn format_date(
&self,
value: &Date,
write: &mut diplomat_runtime::DiplomatWrite,
) -> Result<(), DateTimeFormatError> {
let _infallible = self.0.format_any_calendar(&value.0).write_to(write);
Ok(())
}
pub fn format_iso_date(
&self,
value: &IsoDate,
write: &mut diplomat_runtime::DiplomatWrite,
) -> Result<(), DateTimeFormatError> {
let any = value.0.to_any();
let _infallible = self.0.format_any_calendar(&any).write_to(write);
Ok(())
}
pub fn format_datetime(
&self,
value: &DateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) -> Result<(), DateTimeFormatError> {
let _infallible = self.0.format_any_calendar(&value.0).write_to(write);
Ok(())
}
pub fn format_iso_datetime(
&self,
value: &IsoDateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) -> Result<(), DateTimeFormatError> {
let any = value.0.to_any();
let _infallible = self.0.format_any_calendar(&any).write_to(write);
Ok(())
}
#[diplomat::rust_link(icu::datetime::DateTimeFormatter::calendar_kind, FnInStruct)]
pub fn calendar_kind(&self) -> AnyCalendarKind {
self.0.calendar_kind().into()
}
}
#[diplomat::opaque]
#[diplomat::rust_link(icu::datetime, Mod)]
pub struct DateTimeFormatter(pub icu_datetime::DateTimeFormatter<YMDT>);
impl DateTimeFormatter {
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_length")]
#[diplomat::demo(default_constructor)]
#[cfg(feature = "compiled_data")]
pub fn create_with_length(
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<DateTimeFormatter>, DateTimeFormatterLoadError> {
let prefs = (&locale.0).into();
let options = YMDT::with_length(Length::from(length)).hm();
Ok(Box::new(DateTimeFormatter(
icu_datetime::DateTimeFormatter::try_new(prefs, options)?,
)))
}
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_length_and_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_length_and_provider(
provider: &DataProvider,
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<DateTimeFormatter>, DateTimeFormatterLoadError> {
let prefs = (&locale.0).into();
let options = YMDT::with_length(Length::from(length)).hm();
Ok(Box::new(DateTimeFormatter(
provider.call_constructor_custom_err(move |provider| {
icu_datetime::DateTimeFormatter::try_new_with_buffer_provider(
provider, prefs, options,
)
})?,
)))
}
pub fn format_datetime(
&self,
value: &DateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) -> Result<(), DateTimeFormatError> {
let _infallible = self.0.format_any_calendar(&value.0).write_to(write);
Ok(())
}
pub fn format_iso_datetime(
&self,
value: &IsoDateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) -> Result<(), DateTimeFormatError> {
let any = value.0.to_any();
let _infallible = self.0.format_any_calendar(&any).write_to(write);
Ok(())
}
#[diplomat::rust_link(icu::datetime::DateTimeFormatter::calendar_kind, FnInStruct)]
pub fn calendar_kind(&self) -> AnyCalendarKind {
self.0.calendar_kind().into()
}
}
}