#[diplomat::bridge]
pub mod ffi {
use alloc::boxed::Box;
use alloc::sync::Arc;
use core::convert::TryInto;
use core::fmt::Write;
use icu_calendar::{AnyCalendar, DateTime, Iso, Time};
use tinystr::TinyAsciiStr;
use crate::calendar::ffi::ICU4XCalendar;
use crate::date::ffi::{ICU4XDate, ICU4XIsoDate, ICU4XIsoWeekday};
use crate::errors::ffi::ICU4XError;
use crate::time::ffi::ICU4XTime;
#[cfg(feature = "icu_calendar")]
use crate::week::ffi::ICU4XWeekCalculator;
#[diplomat::opaque]
#[diplomat::rust_link(icu::calendar::DateTime, Struct)]
pub struct ICU4XIsoDateTime(pub DateTime<Iso>);
impl ICU4XIsoDateTime {
#[diplomat::rust_link(icu::calendar::DateTime::try_new_iso_datetime, FnInStruct)]
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
pub fn create(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
nanosecond: u32,
) -> Result<Box<ICU4XIsoDateTime>, ICU4XError> {
let mut dt = DateTime::try_new_iso_datetime(year, month, day, hour, minute, second)?;
dt.time.nanosecond = nanosecond.try_into()?;
Ok(Box::new(ICU4XIsoDateTime(dt)))
}
#[diplomat::rust_link(icu::calendar::DateTime::new, FnInStruct)]
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "from_date_and_time")]
pub fn crate_from_date_and_time(
date: &ICU4XIsoDate,
time: &ICU4XTime,
) -> Box<ICU4XIsoDateTime> {
let dt = DateTime::new(date.0, time.0);
Box::new(ICU4XIsoDateTime(dt))
}
#[diplomat::rust_link(icu::calendar::DateTime::local_unix_epoch, FnInStruct)]
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "local_unix_epoch")]
pub fn local_unix_epoch() -> Box<ICU4XIsoDateTime> {
let dt = DateTime::local_unix_epoch();
Box::new(ICU4XIsoDateTime(dt))
}
#[diplomat::rust_link(
icu::calendar::DateTime::from_minutes_since_local_unix_epoch,
FnInStruct
)]
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "from_minutes_since_local_unix_epoch")]
pub fn create_from_minutes_since_local_unix_epoch(minutes: i32) -> Box<ICU4XIsoDateTime> {
Box::new(ICU4XIsoDateTime(
DateTime::from_minutes_since_local_unix_epoch(minutes),
))
}
#[diplomat::rust_link(icu::calendar::DateTime::date, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn date(&self) -> Box<ICU4XIsoDate> {
Box::new(ICU4XIsoDate(self.0.date))
}
#[diplomat::rust_link(icu::calendar::DateTime::time, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn time(&self) -> Box<ICU4XTime> {
Box::new(ICU4XTime(self.0.time))
}
#[diplomat::rust_link(icu::calendar::DateTime::to_any, FnInStruct)]
#[diplomat::rust_link(icu::calendar::DateTime::new_from_iso, FnInStruct, hidden)]
pub fn to_any(&self) -> Box<ICU4XDateTime> {
Box::new(ICU4XDateTime(self.0.to_any().wrap_calendar_in_arc()))
}
#[diplomat::rust_link(icu::calendar::DateTime::minutes_since_local_unix_epoch, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn minutes_since_local_unix_epoch(&self) -> i32 {
self.0.minutes_since_local_unix_epoch()
}
#[diplomat::rust_link(icu::calendar::DateTime::to_calendar, FnInStruct)]
pub fn to_calendar(&self, calendar: &ICU4XCalendar) -> Box<ICU4XDateTime> {
Box::new(ICU4XDateTime(self.0.to_calendar(calendar.0.clone())))
}
#[diplomat::rust_link(icu::calendar::Time::hour, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn hour(&self) -> u8 {
self.0.time.hour.into()
}
#[diplomat::rust_link(icu::calendar::Time::minute, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn minute(&self) -> u8 {
self.0.time.minute.into()
}
#[diplomat::rust_link(icu::calendar::Time::second, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn second(&self) -> u8 {
self.0.time.second.into()
}
#[diplomat::rust_link(icu::calendar::Time::nanosecond, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn nanosecond(&self) -> u32 {
self.0.time.nanosecond.into()
}
#[diplomat::rust_link(icu::calendar::Date::day_of_year_info, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn day_of_year(&self) -> u16 {
self.0.date.day_of_year_info().day_of_year
}
#[diplomat::rust_link(icu::calendar::Date::day_of_month, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn day_of_month(&self) -> u32 {
self.0.date.day_of_month().0
}
#[diplomat::rust_link(icu::calendar::Date::day_of_week, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn day_of_week(&self) -> ICU4XIsoWeekday {
self.0.date.day_of_week().into()
}
#[diplomat::rust_link(icu::calendar::Date::week_of_month, FnInStruct)]
#[diplomat::rust_link(
icu::calendar::week::WeekCalculator::week_of_month,
FnInStruct,
hidden
)]
pub fn week_of_month(&self, first_weekday: ICU4XIsoWeekday) -> u32 {
self.0.date.week_of_month(first_weekday.into()).0
}
#[diplomat::rust_link(icu::calendar::Date::week_of_year, FnInStruct)]
#[diplomat::rust_link(
icu::calendar::week::WeekCalculator::week_of_year,
FnInStruct,
hidden
)]
#[cfg(feature = "icu_calendar")]
pub fn week_of_year(
&self,
calculator: &ICU4XWeekCalculator,
) -> Result<crate::week::ffi::ICU4XWeekOf, ICU4XError> {
Ok(self.0.date.week_of_year(&calculator.0)?.into())
}
#[diplomat::rust_link(icu::calendar::Date::month, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn month(&self) -> u32 {
self.0.date.month().ordinal
}
#[diplomat::rust_link(icu::calendar::Date::year, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn year(&self) -> i32 {
self.0.date.year().number
}
#[diplomat::rust_link(icu::calendar::Date::is_in_leap_year, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn is_in_leap_year(&self) -> bool {
self.0.date.is_in_leap_year()
}
#[diplomat::rust_link(icu::calendar::Date::months_in_year, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn months_in_year(&self) -> u8 {
self.0.date.months_in_year()
}
#[diplomat::rust_link(icu::calendar::Date::days_in_month, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn days_in_month(&self) -> u8 {
self.0.date.days_in_month()
}
#[diplomat::rust_link(icu::calendar::Date::days_in_year, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn days_in_year(&self) -> u16 {
self.0.date.days_in_year()
}
}
#[diplomat::opaque]
#[diplomat::rust_link(icu::calendar::DateTime, Struct)]
pub struct ICU4XDateTime(pub DateTime<Arc<AnyCalendar>>);
impl ICU4XDateTime {
#[diplomat::rust_link(icu::DateTime::new_from_iso, FnInStruct)]
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "from_iso_in_calendar")]
#[allow(clippy::too_many_arguments)]
pub fn create_from_iso_in_calendar(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
nanosecond: u32,
calendar: &ICU4XCalendar,
) -> Result<Box<ICU4XDateTime>, ICU4XError> {
let cal = calendar.0.clone();
let mut dt = DateTime::try_new_iso_datetime(year, month, day, hour, minute, second)?;
dt.time.nanosecond = nanosecond.try_into()?;
Ok(Box::new(ICU4XDateTime(dt.to_calendar(cal))))
}
#[diplomat::rust_link(icu::calendar::DateTime::try_new_from_codes, FnInStruct)]
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "from_codes_in_calendar")]
#[allow(clippy::too_many_arguments)]
pub fn create_from_codes_in_calendar(
era_code: &DiplomatStr,
year: i32,
month_code: &DiplomatStr,
day: u8,
hour: u8,
minute: u8,
second: u8,
nanosecond: u32,
calendar: &ICU4XCalendar,
) -> Result<Box<ICU4XDateTime>, ICU4XError> {
let era = TinyAsciiStr::from_bytes(era_code)
.map_err(|_| ICU4XError::CalendarUnknownEraError)?
.into();
let month = TinyAsciiStr::from_bytes(month_code)
.map_err(|_| ICU4XError::CalendarUnknownMonthCodeError)?
.into();
let cal = calendar.0.clone();
let hour = hour.try_into()?;
let minute = minute.try_into()?;
let second = second.try_into()?;
let nanosecond = nanosecond.try_into()?;
let time = Time {
hour,
minute,
second,
nanosecond,
};
Ok(Box::new(ICU4XDateTime(DateTime::try_new_from_codes(
era, year, month, day, time, cal,
)?)))
}
#[diplomat::rust_link(icu::calendar::DateTime::new, FnInStruct)]
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "from_date_and_time")]
pub fn create_from_date_and_time(date: &ICU4XDate, time: &ICU4XTime) -> Box<ICU4XDateTime> {
let dt = DateTime::new(date.0.clone(), time.0);
Box::new(ICU4XDateTime(dt))
}
#[diplomat::rust_link(icu::calendar::DateTime::date, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn date(&self) -> Box<ICU4XDate> {
Box::new(ICU4XDate(self.0.date.clone()))
}
#[diplomat::rust_link(icu::calendar::DateTime::time, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn time(&self) -> Box<ICU4XTime> {
Box::new(ICU4XTime(self.0.time))
}
#[diplomat::rust_link(icu::calendar::DateTime::to_iso, FnInStruct)]
pub fn to_iso(&self) -> Box<ICU4XIsoDateTime> {
Box::new(ICU4XIsoDateTime(self.0.to_iso()))
}
#[diplomat::rust_link(icu::calendar::DateTime::to_calendar, FnInStruct)]
pub fn to_calendar(&self, calendar: &ICU4XCalendar) -> Box<ICU4XDateTime> {
Box::new(ICU4XDateTime(self.0.to_calendar(calendar.0.clone())))
}
#[diplomat::rust_link(icu::calendar::Time::hour, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn hour(&self) -> u8 {
self.0.time.hour.into()
}
#[diplomat::rust_link(icu::calendar::Time::minute, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn minute(&self) -> u8 {
self.0.time.minute.into()
}
#[diplomat::rust_link(icu::calendar::Time::second, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn second(&self) -> u8 {
self.0.time.second.into()
}
#[diplomat::rust_link(icu::calendar::Time::nanosecond, StructField)]
#[diplomat::attr(supports = accessors, getter)]
pub fn nanosecond(&self) -> u32 {
self.0.time.nanosecond.into()
}
#[diplomat::rust_link(icu::calendar::Date::day_of_year_info, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn day_of_year(&self) -> u16 {
self.0.date.day_of_year_info().day_of_year
}
#[diplomat::rust_link(icu::calendar::Date::day_of_month, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn day_of_month(&self) -> u32 {
self.0.date.day_of_month().0
}
#[diplomat::rust_link(icu::calendar::Date::day_of_week, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn day_of_week(&self) -> ICU4XIsoWeekday {
self.0.date.day_of_week().into()
}
#[diplomat::rust_link(icu::calendar::Date::week_of_month, FnInStruct)]
#[diplomat::rust_link(
icu::calendar::week::WeekCalculator::week_of_month,
FnInStruct,
hidden
)]
pub fn week_of_month(&self, first_weekday: ICU4XIsoWeekday) -> u32 {
self.0.date.week_of_month(first_weekday.into()).0
}
#[diplomat::rust_link(icu::calendar::Date::week_of_year, FnInStruct)]
#[diplomat::rust_link(
icu::calendar::week::WeekCalculator::week_of_year,
FnInStruct,
hidden
)]
#[cfg(feature = "icu_calendar")]
pub fn week_of_year(
&self,
calculator: &ICU4XWeekCalculator,
) -> Result<crate::week::ffi::ICU4XWeekOf, ICU4XError> {
Ok(self.0.date.week_of_year(&calculator.0)?.into())
}
#[diplomat::rust_link(icu::calendar::Date::month, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn ordinal_month(&self) -> u32 {
self.0.date.month().ordinal
}
#[diplomat::rust_link(icu::calendar::Date::month, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn month_code(
&self,
write: &mut diplomat_runtime::DiplomatWriteable,
) -> Result<(), ICU4XError> {
let code = self.0.date.month().code;
write.write_str(&code.0)?;
Ok(())
}
#[diplomat::rust_link(icu::calendar::Date::year, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn year_in_era(&self) -> i32 {
self.0.date.year().number
}
#[diplomat::rust_link(icu::calendar::Date::year, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn era(
&self,
write: &mut diplomat_runtime::DiplomatWriteable,
) -> Result<(), ICU4XError> {
let era = self.0.date.year().era;
write.write_str(&era.0)?;
Ok(())
}
#[diplomat::rust_link(icu::calendar::Date::months_in_year, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn months_in_year(&self) -> u8 {
self.0.date.months_in_year()
}
#[diplomat::rust_link(icu::calendar::Date::days_in_month, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn days_in_month(&self) -> u8 {
self.0.date.days_in_month()
}
#[diplomat::rust_link(icu::calendar::Date::days_in_year, FnInStruct)]
#[diplomat::attr(supports = accessors, getter)]
pub fn days_in_year(&self) -> u16 {
self.0.date.days_in_year()
}
#[diplomat::rust_link(icu::calendar::Date::calendar, FnInStruct)]
#[diplomat::rust_link(icu::calendar::Date::calendar_wrapper, FnInStruct, hidden)]
#[diplomat::attr(supports = accessors, getter)]
pub fn calendar(&self) -> Box<ICU4XCalendar> {
Box::new(ICU4XCalendar(self.0.date.calendar_wrapper().clone()))
}
}
}