Skip to content

Commit d4f38de

Browse files
authored
Add some compiled_data FFI APIs (#273)
Progress on #272
1 parent 6784ba4 commit d4f38de

8 files changed

Lines changed: 191 additions & 1 deletion

File tree

temporal_capi/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ num-traits.workspace = true
2424
temporal_rs = { workspace = true, default-features = false }
2525
icu_calendar = { version = "2.0.0-beta2", default-features = false }
2626

27+
[features]
28+
compiled_data = ["temporal_rs/compiled_data"]
29+
2730
[package.metadata.docs.rs]
2831
all-features = true

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

Lines changed: 5 additions & 0 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/Instant.hpp

Lines changed: 15 additions & 0 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/TimeZone.d.hpp

Lines changed: 49 additions & 0 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/TimeZone.hpp

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

temporal_capi/src/instant.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ pub mod ffi {
1010
use core::str::{self, FromStr};
1111
use diplomat_runtime::{DiplomatStr, DiplomatStr16};
1212

13+
#[cfg(feature = "compiled_data")]
14+
use crate::options::ffi::ToStringRoundingOptions;
15+
#[cfg(feature = "compiled_data")]
16+
use crate::time_zone::ffi::TimeZone;
17+
1318
#[diplomat::opaque]
1419
pub struct Instant(pub temporal_rs::Instant);
1520

@@ -132,6 +137,22 @@ pub mod ffi {
132137

133138
I128Nanoseconds { high, low }
134139
}
135-
// TODO timezone APIs
140+
141+
#[cfg(feature = "compiled_data")]
142+
pub fn to_ixdtf_string_with_compiled_data(
143+
&self,
144+
zone: Option<&TimeZone>,
145+
options: ToStringRoundingOptions,
146+
write: &mut DiplomatWrite,
147+
) -> Result<(), TemporalError> {
148+
use core::fmt::Write;
149+
let string = self.0.to_ixdtf_string(zone.map(|x| &x.0), options.into())?;
150+
// throw away the error, this should always succeed
151+
let _ = write.write_str(&string);
152+
153+
Ok(())
154+
}
155+
156+
// TODO non-compiled data timezone APIs
136157
}
137158
}

temporal_capi/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ pub mod plain_date_time;
2929
pub mod plain_month_day;
3030
pub mod plain_time;
3131
pub mod plain_year_month;
32+
33+
pub mod time_zone;

temporal_capi/src/time_zone.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#[diplomat::bridge]
2+
#[diplomat::abi_rename = "temporal_rs_{0}"]
3+
#[diplomat::attr(auto, namespace = "temporal_rs")]
4+
pub mod ffi {
5+
use crate::error::ffi::TemporalError;
6+
use alloc::boxed::Box;
7+
use core::str;
8+
9+
#[diplomat::opaque]
10+
pub struct TimeZone(pub temporal_rs::TimeZone);
11+
12+
impl TimeZone {
13+
pub fn try_from_identifier_str(ident: &DiplomatStr) -> Result<Box<Self>, TemporalError> {
14+
let Ok(ident) = str::from_utf8(ident) else {
15+
return Err(temporal_rs::TemporalError::range().into());
16+
};
17+
temporal_rs::TimeZone::try_from_identifier_str(ident)
18+
.map(|x| Box::new(TimeZone(x)))
19+
.map_err(Into::into)
20+
}
21+
pub fn try_from_str(ident: &DiplomatStr) -> Result<Box<Self>, TemporalError> {
22+
let Ok(ident) = str::from_utf8(ident) else {
23+
return Err(temporal_rs::TemporalError::range().into());
24+
};
25+
temporal_rs::TimeZone::try_from_str(ident)
26+
.map(|x| Box::new(TimeZone(x)))
27+
.map_err(Into::into)
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)