Skip to content

Commit 035296f

Browse files
authored
Rework library restructure to remove wrapper types (#181)
This PR fixes #169 and is related to #165, and a couple of other issues / discussions that popped up after the restructure merge. This mainly restructures the codebase to remove the wrapping types and flag the non "with_provider" methods behind a non-default "compiled_data" flag. Let me know what you think!
1 parent f1154b0 commit 035296f

File tree

25 files changed

+423
-1644
lines changed

25 files changed

+423
-1644
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ combine = { workspace = true, optional = true }
6767
web-time = { workspace = true, optional = true }
6868

6969
[features]
70-
default = ["full"]
70+
default = ["now"]
7171
log = ["dep:log"]
72-
full = ["tzdb", "now"]
72+
compiled_data = ["tzdb"]
7373
now = ["std", "dep:web-time"]
7474
tzdb = ["dep:tzif", "std", "dep:jiff-tzdb", "dep:combine"]
7575
std = []

src/builtins/compiled/duration.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use crate::{
2+
builtins::TZ_PROVIDER,
3+
options::{RelativeTo, RoundingOptions},
4+
Duration, TemporalError, TemporalResult,
5+
};
6+
7+
#[cfg(test)]
8+
mod tests;
9+
10+
impl Duration {
11+
/// Rounds the current [`Duration`] according to the provided [`RoundingOptions`] and an optional
12+
/// [`RelativeTo`]
13+
///
14+
/// Enable with the `compiled_data` feature flag.
15+
pub fn round(
16+
&self,
17+
options: RoundingOptions,
18+
relative_to: Option<RelativeTo>,
19+
) -> TemporalResult<Self> {
20+
let provider = TZ_PROVIDER
21+
.lock()
22+
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
23+
self.round_with_provider(options, relative_to, &*provider)
24+
.map(Into::into)
25+
}
26+
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use crate::builtins::native::PlainDate;
21
use crate::{
3-
builtins::{core::calendar::Calendar, native::zoneddatetime::ZonedDateTime},
42
options::{RelativeTo, RoundingIncrement, RoundingOptions, TemporalRoundingMode, TemporalUnit},
53
primitive::FiniteF64,
6-
DateDuration, TimeDuration, TimeZone,
4+
Calendar, DateDuration, PlainDate, TimeDuration, TimeZone, ZonedDateTime,
75
};
86
use alloc::vec::Vec;
97
use core::str::FromStr;
@@ -18,7 +16,6 @@ fn get_round_result(
1816
test_duration
1917
.round(options, Some(relative_to))
2018
.unwrap()
21-
.0
2219
.fields()
2320
.iter()
2421
.map(|f| f.as_date_value().unwrap())
@@ -463,7 +460,7 @@ fn rounding_increment_non_integer() {
463460
.unwrap();
464461

465462
assert_eq!(
466-
result.0.fields(),
463+
result.fields(),
467464
&[
468465
FiniteF64::default(),
469466
FiniteF64::default(),
@@ -483,7 +480,7 @@ fn rounding_increment_non_integer() {
483480
.insert(RoundingIncrement::try_from(1e9 + 0.5).unwrap());
484481
let result = test_duration.round(options, Some(relative_to)).unwrap();
485482
assert_eq!(
486-
result.0.fields(),
483+
result.fields(),
487484
&[
488485
FiniteF64::default(),
489486
FiniteF64::default(),

src/builtins/compiled/instant.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::{
2+
builtins::TZ_PROVIDER, options::ToStringRoundingOptions, Instant, TemporalError,
3+
TemporalResult, TimeZone,
4+
};
5+
use alloc::string::String;
6+
7+
impl Instant {
8+
/// Returns the RFC9557 (IXDTF) string for this `Instant` with the
9+
/// provided options
10+
///
11+
/// Enable with the `compiled_data` feature flag.
12+
pub fn as_ixdtf_string(
13+
&self,
14+
timezone: Option<&TimeZone>,
15+
options: ToStringRoundingOptions,
16+
) -> TemporalResult<String> {
17+
let provider = TZ_PROVIDER
18+
.lock()
19+
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
20+
21+
self.as_ixdtf_string_with_provider(timezone, options, &*provider)
22+
}
23+
}

src/builtins/compiled/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! This module implements native Rust wrappers for the Temporal builtins.
2+
3+
mod duration;
4+
mod instant;
5+
mod now;
6+
mod timezone;
7+
mod zoneddatetime;
8+
9+
mod options {
10+
use crate::{builtins::TZ_PROVIDER, options::RelativeTo, TemporalError, TemporalResult};
11+
12+
impl RelativeTo {
13+
pub fn try_from_str(source: &str) -> TemporalResult<Self> {
14+
let provider = TZ_PROVIDER
15+
.lock()
16+
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
17+
18+
Self::try_from_str_with_provider(source, &*provider)
19+
}
20+
}
21+
}

src/builtins/compiled/now.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::builtins::{
2+
core::{Now, PlainDate, PlainDateTime, PlainTime},
3+
TZ_PROVIDER,
4+
};
5+
use crate::{TemporalError, TemporalResult, TimeZone};
6+
7+
impl Now {
8+
/// Returns the current system time as a [`PlainDateTime`] with an optional
9+
/// [`TimeZone`].
10+
///
11+
/// Enable with the `compiled_data` feature flag.
12+
pub fn plain_datetime_iso(timezone: Option<TimeZone>) -> TemporalResult<PlainDateTime> {
13+
let provider = TZ_PROVIDER
14+
.lock()
15+
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
16+
Now::plain_datetime_iso_with_provider(timezone, &*provider).map(Into::into)
17+
}
18+
19+
/// Returns the current system time as a [`PlainDate`] with an optional
20+
/// [`TimeZone`].
21+
///
22+
/// Enable with the `compiled_data` feature flag.
23+
pub fn plain_date_iso(timezone: Option<TimeZone>) -> TemporalResult<PlainDate> {
24+
let provider = TZ_PROVIDER
25+
.lock()
26+
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
27+
Now::plain_date_iso_with_provider(timezone, &*provider).map(Into::into)
28+
}
29+
30+
/// Returns the current system time as a [`PlainTime`] with an optional
31+
/// [`TimeZone`].
32+
///
33+
/// Enable with the `compiled_data` feature flag.
34+
pub fn plain_time_iso(timezone: Option<TimeZone>) -> TemporalResult<PlainTime> {
35+
let provider = TZ_PROVIDER
36+
.lock()
37+
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
38+
Now::plain_time_iso_with_provider(timezone, &*provider).map(Into::into)
39+
}
40+
}

src/builtins/compiled/timezone.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use crate::{builtins::TZ_PROVIDER, TemporalError, TemporalResult, TimeZone};
2+
3+
impl TimeZone {
4+
/// Attempts to parse `TimeZone` from either a UTC offset or IANA identifier.
5+
///
6+
/// Enable with the `compiled_data` feature flag.
7+
pub fn try_from_str(source: &str) -> TemporalResult<Self> {
8+
let provider = TZ_PROVIDER
9+
.lock()
10+
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
11+
12+
Self::try_from_str_with_provider(source, &*provider)
13+
}
14+
}

0 commit comments

Comments
 (0)