Skip to content

Commit

Permalink
Rework library restructure to remove wrapper types (#181)
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
nekevss authored Jan 23, 2025
1 parent f1154b0 commit 035296f
Show file tree
Hide file tree
Showing 25 changed files with 423 additions and 1,644 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ combine = { workspace = true, optional = true }
web-time = { workspace = true, optional = true }

[features]
default = ["full"]
default = ["now"]
log = ["dep:log"]
full = ["tzdb", "now"]
compiled_data = ["tzdb"]
now = ["std", "dep:web-time"]
tzdb = ["dep:tzif", "std", "dep:jiff-tzdb", "dep:combine"]
std = []
26 changes: 26 additions & 0 deletions src/builtins/compiled/duration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::{
builtins::TZ_PROVIDER,
options::{RelativeTo, RoundingOptions},
Duration, TemporalError, TemporalResult,
};

#[cfg(test)]
mod tests;

impl Duration {
/// Rounds the current [`Duration`] according to the provided [`RoundingOptions`] and an optional
/// [`RelativeTo`]
///
/// Enable with the `compiled_data` feature flag.
pub fn round(
&self,
options: RoundingOptions,
relative_to: Option<RelativeTo>,
) -> TemporalResult<Self> {
let provider = TZ_PROVIDER
.lock()
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
self.round_with_provider(options, relative_to, &*provider)
.map(Into::into)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::builtins::native::PlainDate;
use crate::{
builtins::{core::calendar::Calendar, native::zoneddatetime::ZonedDateTime},
options::{RelativeTo, RoundingIncrement, RoundingOptions, TemporalRoundingMode, TemporalUnit},
primitive::FiniteF64,
DateDuration, TimeDuration, TimeZone,
Calendar, DateDuration, PlainDate, TimeDuration, TimeZone, ZonedDateTime,
};
use alloc::vec::Vec;
use core::str::FromStr;
Expand All @@ -18,7 +16,6 @@ fn get_round_result(
test_duration
.round(options, Some(relative_to))
.unwrap()
.0
.fields()
.iter()
.map(|f| f.as_date_value().unwrap())
Expand Down Expand Up @@ -463,7 +460,7 @@ fn rounding_increment_non_integer() {
.unwrap();

assert_eq!(
result.0.fields(),
result.fields(),
&[
FiniteF64::default(),
FiniteF64::default(),
Expand All @@ -483,7 +480,7 @@ fn rounding_increment_non_integer() {
.insert(RoundingIncrement::try_from(1e9 + 0.5).unwrap());
let result = test_duration.round(options, Some(relative_to)).unwrap();
assert_eq!(
result.0.fields(),
result.fields(),
&[
FiniteF64::default(),
FiniteF64::default(),
Expand Down
23 changes: 23 additions & 0 deletions src/builtins/compiled/instant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::{
builtins::TZ_PROVIDER, options::ToStringRoundingOptions, Instant, TemporalError,
TemporalResult, TimeZone,
};
use alloc::string::String;

impl Instant {
/// Returns the RFC9557 (IXDTF) string for this `Instant` with the
/// provided options
///
/// Enable with the `compiled_data` feature flag.
pub fn as_ixdtf_string(
&self,
timezone: Option<&TimeZone>,
options: ToStringRoundingOptions,
) -> TemporalResult<String> {
let provider = TZ_PROVIDER
.lock()
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;

self.as_ixdtf_string_with_provider(timezone, options, &*provider)
}
}
21 changes: 21 additions & 0 deletions src/builtins/compiled/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! This module implements native Rust wrappers for the Temporal builtins.
mod duration;
mod instant;
mod now;
mod timezone;
mod zoneddatetime;

mod options {
use crate::{builtins::TZ_PROVIDER, options::RelativeTo, TemporalError, TemporalResult};

impl RelativeTo {
pub fn try_from_str(source: &str) -> TemporalResult<Self> {
let provider = TZ_PROVIDER
.lock()
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;

Self::try_from_str_with_provider(source, &*provider)
}
}
}
40 changes: 40 additions & 0 deletions src/builtins/compiled/now.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::builtins::{
core::{Now, PlainDate, PlainDateTime, PlainTime},
TZ_PROVIDER,
};
use crate::{TemporalError, TemporalResult, TimeZone};

impl Now {
/// Returns the current system time as a [`PlainDateTime`] with an optional
/// [`TimeZone`].
///
/// Enable with the `compiled_data` feature flag.
pub fn plain_datetime_iso(timezone: Option<TimeZone>) -> TemporalResult<PlainDateTime> {
let provider = TZ_PROVIDER
.lock()
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
Now::plain_datetime_iso_with_provider(timezone, &*provider).map(Into::into)
}

/// Returns the current system time as a [`PlainDate`] with an optional
/// [`TimeZone`].
///
/// Enable with the `compiled_data` feature flag.
pub fn plain_date_iso(timezone: Option<TimeZone>) -> TemporalResult<PlainDate> {
let provider = TZ_PROVIDER
.lock()
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
Now::plain_date_iso_with_provider(timezone, &*provider).map(Into::into)
}

/// Returns the current system time as a [`PlainTime`] with an optional
/// [`TimeZone`].
///
/// Enable with the `compiled_data` feature flag.
pub fn plain_time_iso(timezone: Option<TimeZone>) -> TemporalResult<PlainTime> {
let provider = TZ_PROVIDER
.lock()
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
Now::plain_time_iso_with_provider(timezone, &*provider).map(Into::into)
}
}
14 changes: 14 additions & 0 deletions src/builtins/compiled/timezone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::{builtins::TZ_PROVIDER, TemporalError, TemporalResult, TimeZone};

impl TimeZone {
/// Attempts to parse `TimeZone` from either a UTC offset or IANA identifier.
///
/// Enable with the `compiled_data` feature flag.
pub fn try_from_str(source: &str) -> TemporalResult<Self> {
let provider = TZ_PROVIDER
.lock()
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;

Self::try_from_str_with_provider(source, &*provider)
}
}
Loading

0 comments on commit 035296f

Please sign in to comment.