#[diplomat::bridge]
#[diplomat::abi_rename = "icu4x_{0}_mv1"]
#[diplomat::attr(auto, namespace = "icu4x")]
pub mod ffi {
use alloc::boxed::Box;
use icu_datetime::{
fieldset::{T, YMD, YMDT},
options::NeoSkeletonLength,
};
use crate::{
date::ffi::{Date, IsoDate},
datetime::ffi::{DateTime, IsoDateTime},
errors::ffi::{DateTimeFormatError, PatternLoadError},
locale_core::ffi::Locale,
provider::ffi::DataProvider,
time::ffi::Time,
};
use writeable::TryWriteable;
#[diplomat::opaque]
#[diplomat::rust_link(icu::datetime, Mod)]
pub struct TimeFormatter(pub icu_datetime::FixedCalendarDateTimeFormatter<(), T>);
#[diplomat::enum_convert(icu_datetime::options::NeoSkeletonLength, needs_wildcard)]
#[diplomat::rust_link(icu::datetime::options::NeoSkeletonLength, Enum)]
pub enum DateTimeLength {
Long,
Medium,
Short,
}
impl TimeFormatter {
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_length")]
#[diplomat::demo(default_constructor)]
pub fn create_with_length(
provider: &DataProvider,
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<TimeFormatter>, PatternLoadError> {
let locale = locale.to_datalocale();
let options = T::with_length(NeoSkeletonLength::from(length)).hm();
Ok(Box::new(TimeFormatter(call_constructor!(
icu_datetime::FixedCalendarDateTimeFormatter::try_new,
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_any_provider,
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider,
provider,
&locale,
options
)?)))
}
pub fn format_time(&self, value: &Time, write: &mut diplomat_runtime::DiplomatWrite) {
let _lossy = self.0.format(&value.0).try_write_to(write);
}
pub fn format_datetime(
&self,
value: &DateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) {
let _lossy = self.0.format(&value.0.time).try_write_to(write);
}
pub fn format_iso_datetime(
&self,
value: &IsoDateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) {
let _lossy = self.0.format(&value.0.time).try_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)]
pub fn create_with_length(
provider: &DataProvider,
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<GregorianDateFormatter>, PatternLoadError> {
let locale = locale.to_datalocale();
let options = YMD::with_length(NeoSkeletonLength::from(length));
Ok(Box::new(GregorianDateFormatter(call_constructor!(
icu_datetime::FixedCalendarDateTimeFormatter::try_new,
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_any_provider,
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider,
provider,
&locale,
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 _lossy = self.0.format(&greg).try_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 _lossy = self.0.format(&greg).try_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)]
pub fn create_with_length(
provider: &DataProvider,
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<GregorianDateTimeFormatter>, PatternLoadError> {
let locale = locale.to_datalocale();
let options = YMDT::with_length(NeoSkeletonLength::from(length)).hm();
Ok(Box::new(GregorianDateTimeFormatter(call_constructor!(
icu_datetime::FixedCalendarDateTimeFormatter::try_new,
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_any_provider,
icu_datetime::FixedCalendarDateTimeFormatter::try_new_with_buffer_provider,
provider,
&locale,
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 _lossy = self.0.format(&greg).try_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)]
pub fn create_with_length(
provider: &DataProvider,
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<DateFormatter>, PatternLoadError> {
let locale = locale.to_datalocale();
let options = YMD::with_length(NeoSkeletonLength::from(length));
Ok(Box::new(DateFormatter(call_constructor!(
icu_datetime::DateTimeFormatter::try_new,
icu_datetime::DateTimeFormatter::try_new_with_any_provider,
icu_datetime::DateTimeFormatter::try_new_with_buffer_provider,
provider,
&locale,
options
)?)))
}
pub fn format_date(
&self,
value: &Date,
write: &mut diplomat_runtime::DiplomatWrite,
) -> Result<(), DateTimeFormatError> {
let _lossy = self.0.convert_and_format(&value.0).try_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 _lossy = self.0.convert_and_format(&any).try_write_to(write);
Ok(())
}
pub fn format_datetime(
&self,
value: &DateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) -> Result<(), DateTimeFormatError> {
let _lossy = self.0.convert_and_format(&value.0).try_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 _lossy = self.0.convert_and_format(&any).try_write_to(write);
Ok(())
}
}
#[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)]
pub fn create_with_length(
provider: &DataProvider,
locale: &Locale,
length: DateTimeLength,
) -> Result<Box<DateTimeFormatter>, PatternLoadError> {
let locale = locale.to_datalocale();
let options = YMDT::with_length(NeoSkeletonLength::from(length)).hm();
Ok(Box::new(DateTimeFormatter(call_constructor!(
icu_datetime::DateTimeFormatter::try_new,
icu_datetime::DateTimeFormatter::try_new_with_any_provider,
icu_datetime::DateTimeFormatter::try_new_with_buffer_provider,
provider,
&locale,
options,
)?)))
}
pub fn format_datetime(
&self,
value: &DateTime,
write: &mut diplomat_runtime::DiplomatWrite,
) -> Result<(), DateTimeFormatError> {
let _lossy = self.0.convert_and_format(&value.0).try_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 _lossy = self.0.convert_and_format(&any).try_write_to(write);
Ok(())
}
}
}