Skip to content

Commit 63aa66e

Browse files
committed
Update FFI for Duration changes
1 parent a1b6a96 commit 63aa66e

10 files changed

Lines changed: 56 additions & 36 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ include = [
2020
[dependencies]
2121
diplomat = "0.10.0"
2222
diplomat-runtime = "0.10.0"
23+
num-traits.workspace = true
2324
temporal_rs = { workspace = true, default-features = false }
2425
icu_calendar = { version = "2.0.0-beta2", default-features = false }

temporal_capi/bindings/cpp/temporal_rs/Duration.d.hpp

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/bindings/cpp/temporal_rs/Duration.hpp

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/bindings/cpp/temporal_rs/PartialDuration.d.hpp

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/bindings/cpp/temporal_rs/PartialDuration.hpp

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/bindings/cpp/temporal_rs/TimeDuration.d.hpp

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/bindings/cpp/temporal_rs/TimeDuration.hpp

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/src/duration.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::error::ffi::TemporalError;
66
pub mod ffi {
77
use crate::error::ffi::TemporalError;
88
use diplomat_runtime::DiplomatOption;
9+
use num_traits::FromPrimitive;
910

1011
#[diplomat::opaque]
1112
pub struct Duration(pub(crate) temporal_rs::Duration);
@@ -27,8 +28,8 @@ pub mod ffi {
2728
pub minutes: DiplomatOption<i64>,
2829
pub seconds: DiplomatOption<i64>,
2930
pub milliseconds: DiplomatOption<i64>,
30-
pub microseconds: DiplomatOption<i128>,
31-
pub nanoseconds: DiplomatOption<i128>,
31+
pub microseconds: DiplomatOption<f64>,
32+
pub nanoseconds: DiplomatOption<f64>,
3233
}
3334

3435
#[diplomat::enum_convert(temporal_rs::Sign)]
@@ -52,16 +53,16 @@ pub mod ffi {
5253
minutes: i64,
5354
seconds: i64,
5455
milliseconds: i64,
55-
microseconds: i128,
56-
nanoseconds: i128,
56+
microseconds: f64,
57+
nanoseconds: f64,
5758
) -> Result<Box<Self>, TemporalError> {
5859
temporal_rs::TimeDuration::new(
5960
hours,
6061
minutes,
6162
seconds,
6263
milliseconds,
63-
microseconds,
64-
nanoseconds,
64+
i128::from_f64(microseconds).ok_or(TemporalError::range())?,
65+
i128::from_f64(nanoseconds).ok_or(TemporalError::range())?,
6566
)
6667
.map(|x| Box::new(TimeDuration(x)))
6768
.map_err(Into::into)
@@ -115,8 +116,8 @@ pub mod ffi {
115116
minutes: i64,
116117
seconds: i64,
117118
milliseconds: i64,
118-
microseconds: i128,
119-
nanoseconds: i128,
119+
microseconds: f64,
120+
nanoseconds: f64,
120121
) -> Result<Box<Self>, TemporalError> {
121122
temporal_rs::Duration::new(
122123
years,
@@ -127,8 +128,8 @@ pub mod ffi {
127128
minutes,
128129
seconds,
129130
milliseconds,
130-
microseconds,
131-
nanoseconds,
131+
i128::from_f64(microseconds).ok_or(TemporalError::range())?,
132+
i128::from_f64(nanoseconds).ok_or(TemporalError::range())?,
132133
)
133134
.map(|x| Box::new(Duration(x)))
134135
.map_err(Into::into)
@@ -186,11 +187,11 @@ pub mod ffi {
186187
pub fn milliseconds(&self) -> i64 {
187188
self.0.milliseconds()
188189
}
189-
pub fn microseconds(&self) -> i128 {
190-
self.0.microseconds()
190+
pub fn microseconds(&self) -> Option<f64> {
191+
f64::from_i128(self.0.microseconds())
191192
}
192-
pub fn nanoseconds(&self) -> i128 {
193-
self.0.nanoseconds()
193+
pub fn nanoseconds(&self) -> Option<f64> {
194+
f64::from_i128(self.0.nanoseconds())
194195
}
195196

196197
pub fn sign(&self) -> Sign {
@@ -230,6 +231,7 @@ pub mod ffi {
230231
impl TryFrom<ffi::PartialDuration> for temporal_rs::partial::PartialDuration {
231232
type Error = TemporalError;
232233
fn try_from(other: ffi::PartialDuration) -> Result<Self, TemporalError> {
234+
use num_traits::FromPrimitive;
233235
Ok(Self {
234236
years: other.years.into_option(),
235237
months: other.months.into_option(),
@@ -239,8 +241,16 @@ impl TryFrom<ffi::PartialDuration> for temporal_rs::partial::PartialDuration {
239241
minutes: other.minutes.into_option(),
240242
seconds: other.seconds.into_option(),
241243
milliseconds: other.milliseconds.into_option(),
242-
microseconds: other.microseconds.into_option(),
243-
nanoseconds: other.nanoseconds.into_option(),
244+
microseconds: other
245+
.microseconds
246+
.into_option()
247+
.map(|v| i128::from_f64(v).ok_or(TemporalError::range()))
248+
.transpose()?,
249+
nanoseconds: other
250+
.nanoseconds
251+
.into_option()
252+
.map(|v| i128::from_f64(v).ok_or(TemporalError::range()))
253+
.transpose()?,
244254
})
245255
}
246256
}

temporal_capi/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ pub mod ffi {
2424
kind: ErrorKind::Syntax,
2525
}
2626
}
27+
28+
pub(crate) fn range() -> Self {
29+
TemporalError {
30+
kind: ErrorKind::Range,
31+
}
32+
}
2733
}
2834
}
2935

0 commit comments

Comments
 (0)