|
2 | 2 | #[diplomat::abi_rename = "temporal_rs_{0}"]
|
3 | 3 | #[diplomat::attr(auto, namespace = "temporal_rs")]
|
4 | 4 | pub mod ffi {
|
| 5 | + use crate::duration::ffi::Duration; |
5 | 6 | use crate::error::ffi::TemporalError;
|
| 7 | + use crate::iso::ffi::IsoDate; |
| 8 | + use crate::options::ffi::{ArithmeticOverflow, TemporalUnit}; |
| 9 | + use crate::plain_date::ffi::{PartialDate, PlainDate}; |
| 10 | + use crate::plain_month_day::ffi::PlainMonthDay; |
| 11 | + use crate::plain_year_month::ffi::PlainYearMonth; |
6 | 12 | use diplomat_runtime::DiplomatStr;
|
| 13 | + use std::fmt::Write; |
7 | 14 |
|
8 | 15 | #[diplomat::enum_convert(icu_calendar::any_calendar::AnyCalendarKind, needs_wildcard)]
|
9 | 16 | pub enum AnyCalendarKind {
|
@@ -56,6 +63,127 @@ pub mod ffi {
|
56 | 63 | self.0.identifier()
|
57 | 64 | }
|
58 | 65 |
|
59 |
| - // TODO the rest of calendar (needs all the date/time types) |
| 66 | + pub fn date_from_partial( |
| 67 | + &self, |
| 68 | + partial: PartialDate, |
| 69 | + overflow: ArithmeticOverflow, |
| 70 | + ) -> Result<Box<PlainDate>, TemporalError> { |
| 71 | + self.0 |
| 72 | + .date_from_partial(&partial.try_into()?, overflow.into()) |
| 73 | + .map(|c| Box::new(PlainDate(c))) |
| 74 | + .map_err(Into::into) |
| 75 | + } |
| 76 | + |
| 77 | + pub fn month_day_from_partial( |
| 78 | + &self, |
| 79 | + partial: PartialDate, |
| 80 | + overflow: ArithmeticOverflow, |
| 81 | + ) -> Result<Box<PlainMonthDay>, TemporalError> { |
| 82 | + self.0 |
| 83 | + .month_day_from_partial(&partial.try_into()?, overflow.into()) |
| 84 | + .map(|c| Box::new(PlainMonthDay(c))) |
| 85 | + .map_err(Into::into) |
| 86 | + } |
| 87 | + pub fn year_month_from_partial( |
| 88 | + &self, |
| 89 | + partial: PartialDate, |
| 90 | + overflow: ArithmeticOverflow, |
| 91 | + ) -> Result<Box<PlainYearMonth>, TemporalError> { |
| 92 | + self.0 |
| 93 | + .year_month_from_partial(&partial.try_into()?, overflow.into()) |
| 94 | + .map(|c| Box::new(PlainYearMonth(c))) |
| 95 | + .map_err(Into::into) |
| 96 | + } |
| 97 | + pub fn date_add( |
| 98 | + &self, |
| 99 | + date: IsoDate, |
| 100 | + duration: &Duration, |
| 101 | + overflow: ArithmeticOverflow, |
| 102 | + ) -> Result<Box<PlainDate>, TemporalError> { |
| 103 | + self.0 |
| 104 | + .date_add(&date.into(), &duration.0, overflow.into()) |
| 105 | + .map(|c| Box::new(PlainDate(c))) |
| 106 | + .map_err(Into::into) |
| 107 | + } |
| 108 | + pub fn date_until( |
| 109 | + &self, |
| 110 | + one: IsoDate, |
| 111 | + two: IsoDate, |
| 112 | + largest_unit: TemporalUnit, |
| 113 | + ) -> Result<Box<Duration>, TemporalError> { |
| 114 | + self.0 |
| 115 | + .date_until(&one.into(), &two.into(), largest_unit.into()) |
| 116 | + .map(|c| Box::new(Duration(c))) |
| 117 | + .map_err(Into::into) |
| 118 | + } |
| 119 | + |
| 120 | + // Writes an empty string for no era |
| 121 | + pub fn era(&self, date: IsoDate, write: &mut DiplomatWrite) -> Result<(), TemporalError> { |
| 122 | + let era = self |
| 123 | + .0 |
| 124 | + .era(&date.into()) |
| 125 | + .map_err(Into::<TemporalError>::into)?; |
| 126 | + if let Some(era) = era { |
| 127 | + // throw away the error, this should always succeed |
| 128 | + let _ = write.write_str(&era); |
| 129 | + } |
| 130 | + Ok(()) |
| 131 | + } |
| 132 | + |
| 133 | + pub fn era_year(&self, date: IsoDate) -> Result<Option<i32>, TemporalError> { |
| 134 | + self.0.era_year(&date.into()).map_err(Into::into) |
| 135 | + } |
| 136 | + |
| 137 | + pub fn year(&self, date: IsoDate) -> Result<i32, TemporalError> { |
| 138 | + self.0.year(&date.into()).map_err(Into::into) |
| 139 | + } |
| 140 | + pub fn month(&self, date: IsoDate) -> Result<u8, TemporalError> { |
| 141 | + self.0.month(&date.into()).map_err(Into::into) |
| 142 | + } |
| 143 | + pub fn month_code( |
| 144 | + &self, |
| 145 | + date: IsoDate, |
| 146 | + write: &mut DiplomatWrite, |
| 147 | + ) -> Result<(), TemporalError> { |
| 148 | + let code = self |
| 149 | + .0 |
| 150 | + .month_code(&date.into()) |
| 151 | + .map_err(Into::<TemporalError>::into)?; |
| 152 | + // throw away the error, this should always succeed |
| 153 | + let _ = write.write_str(&code); |
| 154 | + Ok(()) |
| 155 | + } |
| 156 | + pub fn day(&self, date: IsoDate) -> Result<u8, TemporalError> { |
| 157 | + self.0.day(&date.into()).map_err(Into::into) |
| 158 | + } |
| 159 | + pub fn day_of_week(&self, date: IsoDate) -> Result<u16, TemporalError> { |
| 160 | + self.0.day_of_week(&date.into()).map_err(Into::into) |
| 161 | + } |
| 162 | + pub fn day_of_year(&self, date: IsoDate) -> Result<u16, TemporalError> { |
| 163 | + self.0.day_of_year(&date.into()).map_err(Into::into) |
| 164 | + } |
| 165 | + pub fn week_of_year(&self, date: IsoDate) -> Result<Option<u16>, TemporalError> { |
| 166 | + self.0.week_of_year(&date.into()).map_err(Into::into) |
| 167 | + } |
| 168 | + pub fn year_of_week(&self, date: IsoDate) -> Result<Option<i32>, TemporalError> { |
| 169 | + self.0.year_of_week(&date.into()).map_err(Into::into) |
| 170 | + } |
| 171 | + pub fn days_in_week(&self, date: IsoDate) -> Result<u16, TemporalError> { |
| 172 | + self.0.days_in_week(&date.into()).map_err(Into::into) |
| 173 | + } |
| 174 | + pub fn days_in_month(&self, date: IsoDate) -> Result<u16, TemporalError> { |
| 175 | + self.0.days_in_month(&date.into()).map_err(Into::into) |
| 176 | + } |
| 177 | + pub fn days_in_year(&self, date: IsoDate) -> Result<u16, TemporalError> { |
| 178 | + self.0.days_in_year(&date.into()).map_err(Into::into) |
| 179 | + } |
| 180 | + pub fn months_in_year(&self, date: IsoDate) -> Result<u16, TemporalError> { |
| 181 | + self.0.months_in_year(&date.into()).map_err(Into::into) |
| 182 | + } |
| 183 | + pub fn in_leap_year(&self, date: IsoDate) -> Result<bool, TemporalError> { |
| 184 | + self.0.in_leap_year(&date.into()).map_err(Into::into) |
| 185 | + } |
| 186 | + |
| 187 | + // TODO .fields() (need to pick a convenient way to return vectors or iterators, depending on how the API gets used) |
60 | 188 | }
|
61 | 189 | }
|
0 commit comments