From 750db68814001d09ede9f7af714deb28ffd5f412 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 1 Mar 2024 06:48:16 +0000 Subject: [PATCH] Format Rust code using rustfmt --- build.rs | 2 +- src/binary_search.rs | 3 +- src/ext.rs | 28 +++++++++--------- src/interface.rs | 16 +++++------ src/lib.rs | 63 ++++++++++++++++++++++++++-------------- src/posix_tz/mod.rs | 6 ++-- src/system.rs | 12 ++++---- src/timezones.rs | 2 +- src/zoned.rs | 68 +++++++++++++++++++++++++++++++------------- 9 files changed, 122 insertions(+), 78 deletions(-) diff --git a/build.rs b/build.rs index f586959..2838519 100644 --- a/build.rs +++ b/build.rs @@ -79,7 +79,7 @@ fn parse_win_cldr_db() -> phf_codegen::Map { while let Some(item) = split.next() { if item.trim().is_empty() { continue; - } + } str += "&internal_tz_new(&"; str += item; str.push(')'); diff --git a/src/binary_search.rs b/src/binary_search.rs index 10de82f..1b1b6b6 100644 --- a/src/binary_search.rs +++ b/src/binary_search.rs @@ -59,5 +59,4 @@ mod tests { None ); } - -} \ No newline at end of file +} diff --git a/src/ext.rs b/src/ext.rs index 8e9e4ce..84c8d9b 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -28,7 +28,7 @@ use time::{OffsetDateTime, PrimitiveDateTime}; -use crate::{TimeZone, zoned, OffsetResult, Offset, timezones, ToTimezone, OffsetError}; +use crate::{timezones, zoned, Offset, OffsetError, OffsetResult, TimeZone, ToTimezone}; mod sealing { use crate::OffsetResult; @@ -120,15 +120,13 @@ impl PrimitiveDateTimeExt for PrimitiveDateTime { fn assume_timezone(&self, tz: &T) -> OffsetResult { match tz.get_offset_local(&self.assume_utc()) { Ok(a) => Ok(self.assume_offset(a.to_utc())), - Err(e) => { - match e { - OffsetError::Ambiguous(a, b) => Err(OffsetError::Ambiguous( - self.assume_offset(a.to_utc()), - self.assume_offset(b.to_utc()) - )), - OffsetError::None => Err(OffsetError::None) - } - } + Err(e) => match e { + OffsetError::Ambiguous(a, b) => Err(OffsetError::Ambiguous( + self.assume_offset(a.to_utc()), + self.assume_offset(b.to_utc()), + )), + OffsetError::None => Err(OffsetError::None), + }, } } @@ -175,7 +173,7 @@ impl OffsetResultExt for OffsetResult { fn map_all R>(&self, f: F) -> OffsetResult { match self { Ok(a) => Ok(f(a)), - Err(e) => Err(e.map(f)) + Err(e) => Err(e.map(f)), } } @@ -190,14 +188,14 @@ impl OffsetResultExt for OffsetResult { fn take_first(self) -> Option { match self { Ok(a) => Some(a), - Err(e) => e.take_first() + Err(e) => e.take_first(), } } fn take_second(self) -> Option { match self { Ok(a) => Some(a), - Err(e) => e.take_second() + Err(e) => e.take_second(), } } @@ -208,14 +206,14 @@ impl OffsetResultExt for OffsetResult { fn is_none(&self) -> bool { match self { Ok(_) => false, - Err(e) => e.is_none() + Err(e) => e.is_none(), } } fn is_ambiguous(&self) -> bool { match self { Ok(_) => false, - Err(e) => e.is_ambiguous() + Err(e) => e.is_ambiguous(), } } } diff --git a/src/interface.rs b/src/interface.rs index b710a02..ec1ed2d 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -85,7 +85,7 @@ pub enum OffsetError { Ambiguous(T, T), /// The date time is invalid. - None + None, } impl OffsetError { @@ -93,7 +93,7 @@ impl OffsetError { pub fn is_none(&self) -> bool { match self { OffsetError::Ambiguous(_, _) => false, - OffsetError::None => true + OffsetError::None => true, } } @@ -101,7 +101,7 @@ impl OffsetError { pub fn is_ambiguous(&self) -> bool { match self { OffsetError::Ambiguous(_, _) => true, - OffsetError::None => false + OffsetError::None => false, } } @@ -109,7 +109,7 @@ impl OffsetError { pub fn unwrap_first(self) -> T { match self { OffsetError::Ambiguous(a, _) => a, - OffsetError::None => panic!("Attempt to unwrap an invalid offset") + OffsetError::None => panic!("Attempt to unwrap an invalid offset"), } } @@ -117,7 +117,7 @@ impl OffsetError { pub fn unwrap_second(self) -> T { match self { OffsetError::Ambiguous(_, b) => b, - OffsetError::None => panic!("Attempt to unwrap an invalid offset") + OffsetError::None => panic!("Attempt to unwrap an invalid offset"), } } @@ -125,7 +125,7 @@ impl OffsetError { pub fn take_first(self) -> Option { match self { OffsetError::Ambiguous(a, _) => Some(a), - OffsetError::None => None + OffsetError::None => None, } } @@ -133,7 +133,7 @@ impl OffsetError { pub fn take_second(self) -> Option { match self { OffsetError::Ambiguous(_, b) => Some(b), - OffsetError::None => None + OffsetError::None => None, } } @@ -141,7 +141,7 @@ impl OffsetError { pub fn map R>(&self, f: F) -> OffsetError { match self { OffsetError::Ambiguous(a, b) => OffsetError::Ambiguous(f(a), f(b)), - OffsetError::None => OffsetError::None + OffsetError::None => OffsetError::None, } } } diff --git a/src/lib.rs b/src/lib.rs index 67eb941..f57b1c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,8 +43,8 @@ mod timezone_impl; #[cfg(feature = "db")] pub mod timezones; -pub use interface::*; pub use ext::*; +pub use interface::*; #[cfg(feature = "system")] pub mod system; @@ -57,13 +57,13 @@ pub use timezone_impl::Tz; #[cfg(test)] mod tests { - use crate::{OffsetResultExt, ToTimezone}; use crate::timezones; + use crate::zoned::Duration; use crate::Offset; use crate::OffsetDateTimeExt; use crate::PrimitiveDateTimeExt; use crate::TimeZone; - use crate::zoned::Duration; + use crate::{OffsetResultExt, ToTimezone}; use time::macros::{datetime, offset}; use time::OffsetDateTime; @@ -184,7 +184,8 @@ mod tests { assert_eq!( datetime!(2023-03-26 6:00 UTC) .with_timezone(timezones::db::europe::STOCKHOLM) - .replace_time(time::Time::MIDNIGHT).unwrap_first() + .replace_time(time::Time::MIDNIGHT) + .unwrap_first() .offset_date_time(), datetime!(2023-03-25 23:00 UTC) ); @@ -194,39 +195,57 @@ mod tests { fn zoned_date_time_add_duration() { assert_eq!( (datetime!(2023-01-01 22:00) - .with_timezone(timezones::db::europe::STOCKHOLM).unwrap_first() + Duration::days(1)) - .offset_date_time(), - datetime!(2023-01-02 22:00).with_timezone(timezones::db::europe::STOCKHOLM) - .unwrap_first().offset_date_time() + .with_timezone(timezones::db::europe::STOCKHOLM) + .unwrap_first() + + Duration::days(1)) + .offset_date_time(), + datetime!(2023-01-02 22:00) + .with_timezone(timezones::db::europe::STOCKHOLM) + .unwrap_first() + .offset_date_time() ); assert_eq!( (datetime!(2023-03-25 22:00) - .with_timezone(timezones::db::europe::STOCKHOLM).unwrap_first() + Duration::days(1)) - .offset_date_time(), - datetime!(2023-03-26 22:00).with_timezone(timezones::db::europe::STOCKHOLM) - .unwrap_first().offset_date_time() + .with_timezone(timezones::db::europe::STOCKHOLM) + .unwrap_first() + + Duration::days(1)) + .offset_date_time(), + datetime!(2023-03-26 22:00) + .with_timezone(timezones::db::europe::STOCKHOLM) + .unwrap_first() + .offset_date_time() ); assert_eq!( (datetime!(2023-03-25 22:00) - .with_timezone(timezones::db::europe::STOCKHOLM).unwrap_first() + Duration::hours(24)) - .offset_date_time(), - datetime!(2023-03-26 23:00).with_timezone(timezones::db::europe::STOCKHOLM) - .unwrap_first().offset_date_time() + .with_timezone(timezones::db::europe::STOCKHOLM) + .unwrap_first() + + Duration::hours(24)) + .offset_date_time(), + datetime!(2023-03-26 23:00) + .with_timezone(timezones::db::europe::STOCKHOLM) + .unwrap_first() + .offset_date_time() ); assert_eq!( (datetime!(2023-03-26 1:00) - .with_timezone(timezones::db::europe::STOCKHOLM).unwrap_first() + Duration::hours(1)) - .offset_date_time(), - datetime!(2023-03-26 3:00).with_timezone(timezones::db::europe::STOCKHOLM) - .unwrap_first().offset_date_time() + .with_timezone(timezones::db::europe::STOCKHOLM) + .unwrap_first() + + Duration::hours(1)) + .offset_date_time(), + datetime!(2023-03-26 3:00) + .with_timezone(timezones::db::europe::STOCKHOLM) + .unwrap_first() + .offset_date_time() ); } #[test] fn errors() { - let datetime = datetime!(2024-03-31 02:30:00).assume_timezone(timezones::db::europe::BUDAPEST); + let datetime = + datetime!(2024-03-31 02:30:00).assume_timezone(timezones::db::europe::BUDAPEST); assert!(datetime.is_none()); - let datetime = datetime!(2024-03-31 02:29:00).assume_timezone(timezones::db::europe::BUDAPEST); + let datetime = + datetime!(2024-03-31 02:29:00).assume_timezone(timezones::db::europe::BUDAPEST); assert!(datetime.is_none()); } } diff --git a/src/posix_tz/mod.rs b/src/posix_tz/mod.rs index 5ecdfc3..38d0692 100644 --- a/src/posix_tz/mod.rs +++ b/src/posix_tz/mod.rs @@ -26,7 +26,7 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use crate::{Offset, TimeZone, timezone_impl::Tz, ToTimezone}; +use crate::{timezone_impl::Tz, Offset, TimeZone, ToTimezone}; use std::fmt::{Display, Formatter}; use thiserror::Error; use time::{OffsetDateTime, UtcOffset}; @@ -91,7 +91,7 @@ pub enum Error { /// The resulting date time would be out of range. #[error("resulting date time would be out of range")] - OutOfRange + OutOfRange, } /// A POSIX "timezone" offset. @@ -184,7 +184,7 @@ impl<'a> PosixTz<'a> { let offset = self.get_offset(date_time)?; match date_time.checked_to_offset(offset.to_utc()) { Some(v) => Ok(v), - None => Err(Error::OutOfRange) + None => Err(Error::OutOfRange), } } diff --git a/src/system.rs b/src/system.rs index 040f335..de1ff61 100644 --- a/src/system.rs +++ b/src/system.rs @@ -27,15 +27,15 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. //! Support for getting time zone information from the target system. -//! +//! //! Currently only supported for Windows, Unix, and WASM targets. -use crate::timezones::get_by_name; use crate::timezone_impl::Tz; +use crate::timezones::get_by_name; use thiserror::Error; #[cfg(target_family = "wasm")] -use js_sys::{ Intl, Reflect, Array, Object }; +use js_sys::{Array, Intl, Object, Reflect}; #[cfg(target_family = "wasm")] use wasm_bindgen::JsValue; @@ -61,16 +61,16 @@ pub enum Error { /// The timezone doesn't exist in the crate's database. #[error("unknown timezone name")] Unknown, - + /// The target platform is not supported. Windows, Unix, and WASM targets are the only supported for the system feature at this moment. #[error("unsupported platform")] Unsupported, } /// Gets the current timezone from the system. -/// +/// /// Currently only supported for Windows, Unix, and WASM targets. -/// +/// /// # Errors /// Returns an [Error](enum@Error) if the timezone cannot be determined. pub fn get_timezone() -> Result<&'static Tz, Error> { diff --git a/src/timezones.rs b/src/timezones.rs index 5c200ac..0e1313c 100644 --- a/src/timezones.rs +++ b/src/timezones.rs @@ -26,10 +26,10 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use crate::ToTimezone; use crate::timezone_impl::internal_tz_new; use crate::timezone_impl::FixedTimespan; use crate::timezone_impl::FixedTimespanSet; +use crate::ToTimezone; use crate::timezone_impl::Tz; use phf::Map; diff --git a/src/zoned.rs b/src/zoned.rs index aef9e44..a7ca3e5 100644 --- a/src/zoned.rs +++ b/src/zoned.rs @@ -28,33 +28,46 @@ use std::ops::{Add, Sub}; -use time::{UtcOffset, PrimitiveDateTime, OffsetDateTime, Date, Time}; +use time::{Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset}; -use crate::{TimeZone, OffsetResult, PrimitiveDateTimeExt, ToTimezone, OffsetResultExt}; +use crate::{OffsetResult, OffsetResultExt, PrimitiveDateTimeExt, TimeZone, ToTimezone}; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd)] pub struct ZonedDateTime<'a, T: TimeZone> { date_time: OffsetDateTime, - timezone: &'a T + timezone: &'a T, } impl<'a, T: TimeZone> ZonedDateTime<'a, T> { - pub fn from_local(date_time: PrimitiveDateTime, timezone: &'a T) -> OffsetResult> { - date_time.assume_timezone(timezone).map_all(|v| ZonedDateTime { date_time: *v, timezone }) + pub fn from_local( + date_time: PrimitiveDateTime, + timezone: &'a T, + ) -> OffsetResult> { + date_time + .assume_timezone(timezone) + .map_all(|v| ZonedDateTime { + date_time: *v, + timezone, + }) } pub fn from_utc(date_time: OffsetDateTime, timezone: &'a T) -> ZonedDateTime<'a, T> { let converted = date_time.to_timezone(timezone); - ZonedDateTime { date_time: converted, timezone } + ZonedDateTime { + date_time: converted, + timezone, + } } - fn from_local_offset(date_time: OffsetDateTime, timezone: &'a T) -> OffsetResult> { + fn from_local_offset( + date_time: OffsetDateTime, + timezone: &'a T, + ) -> OffsetResult> { let dt = PrimitiveDateTime::new(date_time.date(), date_time.time()); - dt.assume_timezone(timezone) - .map_all(|v| ZonedDateTime { - date_time: *v, - timezone - }) + dt.assume_timezone(timezone).map_all(|v| ZonedDateTime { + date_time: *v, + timezone, + }) } /// Returns the date component of this ZonedDateTime. @@ -69,7 +82,10 @@ impl<'a, T: TimeZone> ZonedDateTime<'a, T> { /// Replaces the date component of this ZonedDateTime. pub fn replace_date(self, date: Date) -> ZonedDateTime<'a, T> { - ZonedDateTime { date_time: self.date_time.replace_date(date), timezone: self.timezone } + ZonedDateTime { + date_time: self.date_time.replace_date(date), + timezone: self.timezone, + } } /// Replaces the time component of this ZonedDateTime. @@ -91,17 +107,17 @@ impl<'a, T: TimeZone> ZonedDateTime<'a, T> { pub fn timezone(self) -> &'a T { self.timezone } - + /// Replaces the timezone component of this ZonedDateTime. pub fn replace_timezone<'b, T1: TimeZone>(self, timezone: &'b T1) -> ZonedDateTime<'b, T1> { ZonedDateTime::from_utc(self.date_time, timezone) - } + } } #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum Duration { Date(time::Duration), - Time(time::Duration) + Time(time::Duration), } impl From for Duration { @@ -142,10 +158,16 @@ impl<'a, T: TimeZone> Add for ZonedDateTime<'a, T> { fn add(self, rhs: Duration) -> Self::Output { match rhs { - Duration::Date(v) => ZonedDateTime::from_local_offset(self.date_time + v, self.timezone).unwrap_first(), + Duration::Date(v) => { + ZonedDateTime::from_local_offset(self.date_time + v, self.timezone).unwrap_first() + } Duration::Time(v) => { let offset = self.offset(); - ZonedDateTime::from_local_offset(self.date_time + v + time::Duration::seconds(offset.whole_seconds() as _), self.timezone).unwrap_first() + ZonedDateTime::from_local_offset( + self.date_time + v + time::Duration::seconds(offset.whole_seconds() as _), + self.timezone, + ) + .unwrap_first() } } } @@ -156,10 +178,16 @@ impl<'a, T: TimeZone> Sub for ZonedDateTime<'a, T> { fn sub(self, rhs: Duration) -> Self::Output { match rhs { - Duration::Date(v) => ZonedDateTime::from_local_offset(self.date_time - v, self.timezone).unwrap_first(), + Duration::Date(v) => { + ZonedDateTime::from_local_offset(self.date_time - v, self.timezone).unwrap_first() + } Duration::Time(v) => { let offset = self.offset(); - ZonedDateTime::from_local_offset(self.date_time - v - time::Duration::seconds(offset.whole_seconds() as _), self.timezone).unwrap_first() + ZonedDateTime::from_local_offset( + self.date_time - v - time::Duration::seconds(offset.whole_seconds() as _), + self.timezone, + ) + .unwrap_first() } } }