-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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!
- Loading branch information
Showing
25 changed files
with
423 additions
and
1,644 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.