Skip to content

Commit 28f24a9

Browse files
authored
More FFI: Finish Calendar, add Instant (#198)
Most of what is left is timezone stuff, I think
1 parent f047da7 commit 28f24a9

File tree

6 files changed

+316
-225
lines changed

6 files changed

+316
-225
lines changed

temporal_capi/bindings/cpp/diplomat_runtime.hpp

Lines changed: 0 additions & 215 deletions
This file was deleted.

temporal_capi/bindings/cpp/temporal_rs/.gitkeep

Whitespace-only changes.

temporal_capi/src/calendar.rs

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
#[diplomat::abi_rename = "temporal_rs_{0}"]
33
#[diplomat::attr(auto, namespace = "temporal_rs")]
44
pub mod ffi {
5+
use crate::duration::ffi::Duration;
56
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;
612
use diplomat_runtime::DiplomatStr;
13+
use std::fmt::Write;
714

815
#[diplomat::enum_convert(icu_calendar::any_calendar::AnyCalendarKind, needs_wildcard)]
916
pub enum AnyCalendarKind {
@@ -56,6 +63,127 @@ pub mod ffi {
5663
self.0.identifier()
5764
}
5865

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)
60188
}
61189
}

0 commit comments

Comments
 (0)