From 7f6cf5e5048a4049c265938ce75c14714e7c142f Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 21 Feb 2025 10:32:44 +0100 Subject: [PATCH] Switch to 2024 style --- bench/benches/chrono.rs | 8 +-- bench/benches/serde.rs | 2 +- rustfmt.toml | 1 + src/datetime/mod.rs | 20 +++----- src/datetime/serde.rs | 6 ++- src/datetime/tests.rs | 11 ++-- src/format/formatting.rs | 2 +- src/format/locales.rs | 2 +- src/format/mod.rs | 4 +- src/format/parse.rs | 16 +++--- src/format/parsed.rs | 18 ++----- src/format/scan.rs | 10 ++-- src/format/strftime.rs | 12 ++--- src/month.rs | 6 +-- src/naive/date/mod.rs | 6 +-- src/naive/date/tests.rs | 4 +- src/naive/datetime/mod.rs | 6 +-- src/naive/datetime/tests.rs | 9 ++-- src/naive/internals.rs | 2 +- src/naive/isoweek.rs | 2 +- src/naive/mod.rs | 6 +-- src/naive/time/mod.rs | 6 +-- src/offset/fixed.rs | 2 +- src/offset/local/mod.rs | 6 +-- src/offset/local/tz_info/parser.rs | 4 +- src/offset/local/tz_info/rule.rs | 10 ++-- src/offset/local/tz_info/timezone.rs | 6 +-- src/offset/local/windows.rs | 4 +- src/offset/mod.rs | 4 +- src/round.rs | 2 +- src/time_delta.rs | 76 +++++++++++++++------------- src/traits.rs | 6 +-- src/weekday.rs | 10 ++-- 33 files changed, 135 insertions(+), 154 deletions(-) diff --git a/bench/benches/chrono.rs b/bench/benches/chrono.rs index 91c4e45520..c74d6ae382 100644 --- a/bench/benches/chrono.rs +++ b/bench/benches/chrono.rs @@ -1,12 +1,12 @@ //! Benchmarks for chrono that just depend on std -use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; +use criterion::{BenchmarkId, Criterion, black_box, criterion_group, criterion_main}; -use chrono::format::StrftimeItems; -use chrono::prelude::*; #[cfg(feature = "unstable-locales")] use chrono::Locale; -use chrono::{DateTime, FixedOffset, Local, TimeDelta, Utc, __BenchYearFlags}; +use chrono::format::StrftimeItems; +use chrono::prelude::*; +use chrono::{__BenchYearFlags, DateTime, FixedOffset, Local, TimeDelta, Utc}; fn bench_date_from_ymd(c: &mut Criterion) { c.bench_function("bench_date_from_ymd", |b| { diff --git a/bench/benches/serde.rs b/bench/benches/serde.rs index ee4deb5a49..8791019f60 100644 --- a/bench/benches/serde.rs +++ b/bench/benches/serde.rs @@ -1,4 +1,4 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use criterion::{Criterion, black_box, criterion_group, criterion_main}; use chrono::NaiveDateTime; diff --git a/rustfmt.toml b/rustfmt.toml index 2a35f0230c..d6b90a3179 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1,2 @@ +style_edition = "2024" use_small_heuristics = "Max" diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index db91561450..ccb6c953b2 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -14,22 +14,22 @@ use core::{fmt, hash, str}; #[cfg(feature = "std")] use std::time::{SystemTime, UNIX_EPOCH}; +#[allow(deprecated)] +use crate::Date; #[cfg(all(feature = "unstable-locales", feature = "alloc"))] use crate::format::Locale; +#[cfg(feature = "alloc")] +use crate::format::{DelayedFormat, SecondsFormat, write_rfc2822, write_rfc3339}; use crate::format::{ - parse, parse_and_remainder, parse_rfc3339, Fixed, Item, ParseError, ParseResult, Parsed, - StrftimeItems, TOO_LONG, + Fixed, Item, ParseError, ParseResult, Parsed, StrftimeItems, TOO_LONG, parse, + parse_and_remainder, parse_rfc3339, }; -#[cfg(feature = "alloc")] -use crate::format::{write_rfc2822, write_rfc3339, DelayedFormat, SecondsFormat}; use crate::naive::{Days, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime}; #[cfg(feature = "clock")] use crate::offset::Local; use crate::offset::{FixedOffset, LocalResult, Offset, TimeZone, Utc}; -#[allow(deprecated)] -use crate::Date; -use crate::{expect, try_opt}; use crate::{Datelike, Months, TimeDelta, Timelike, Weekday}; +use crate::{expect, try_opt}; #[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))] use rkyv::{Archive, Deserialize, Serialize}; @@ -1849,11 +1849,7 @@ impl From for DateTime { // unlikely but should be handled let dur = e.duration(); let (sec, nsec) = (dur.as_secs() as i64, dur.subsec_nanos()); - if nsec == 0 { - (-sec, 0) - } else { - (-sec - 1, 1_000_000_000 - nsec) - } + if nsec == 0 { (-sec, 0) } else { (-sec - 1, 1_000_000_000 - nsec) } } }; Utc.timestamp_opt(sec, nsec).unwrap() diff --git a/src/datetime/serde.rs b/src/datetime/serde.rs index e3c6bf57db..1492fe3f0b 100644 --- a/src/datetime/serde.rs +++ b/src/datetime/serde.rs @@ -2,7 +2,7 @@ use core::fmt; use serde::{de, ser}; use super::DateTime; -use crate::format::{write_rfc3339, SecondsFormat}; +use crate::format::{SecondsFormat, write_rfc3339}; #[cfg(feature = "clock")] use crate::offset::Local; use crate::offset::{FixedOffset, Offset, TimeZone, Utc}; @@ -1289,7 +1289,9 @@ mod tests { } assert!(serde_json::from_str::>(r#""2014-07-32T12:34:06Z""#).is_err()); - assert!(serde_json::from_str::>(r#""2014-07-32T12:34:06Z""#).is_err()); + assert!( + serde_json::from_str::>(r#""2014-07-32T12:34:06Z""#).is_err() + ); } #[test] diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index c890863d8b..b7f7106928 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -734,10 +734,7 @@ fn test_datetime_rfc2822() { "Thu,\n\t13\n Feb\n 1969\n 23:32\n -0330 (Newfoundland Time)" ), Ok( - ymdhms( - &FixedOffset::east_opt(-3 * 60 * 60 - 30 * 60).unwrap(), - 1969, 2, 13, 23, 32, 0, - ) + ymdhms(&FixedOffset::east_opt(-3 * 60 * 60 - 30 * 60).unwrap(), 1969, 2, 13, 23, 32, 0,) ) ); // example from RFC 2822 Appendix A.5. without trailing " (Newfoundland Time)" @@ -1099,8 +1096,10 @@ fn test_parse_from_str() { Ok(ymdhms(&edt, 2014, 5, 7, 12, 34, 56)) ); // ignore offset assert!(DateTime::parse_from_str("20140507000000", "%Y%m%d%H%M%S").is_err()); // no offset - assert!(DateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT") - .is_err()); + assert!( + DateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT") + .is_err() + ); assert_eq!( DateTime::parse_from_str("0", "%s").unwrap(), DateTime::from_timestamp(0, 0).unwrap().fixed_offset() diff --git a/src/format/formatting.rs b/src/format/formatting.rs index a0e156c130..1b7df6db0c 100644 --- a/src/format/formatting.rs +++ b/src/format/formatting.rs @@ -638,8 +638,8 @@ mod tests { #[cfg(all(feature = "std", feature = "unstable-locales", feature = "alloc"))] #[test] fn test_with_locale_delayed_write_to() { - use crate::format::locales::Locale; use crate::DateTime; + use crate::format::locales::Locale; let dt = DateTime::from_timestamp(1643723400, 123456789).unwrap(); let df = dt.format_localized("%A, %B %d, %Y", Locale::ja_JP); diff --git a/src/format/locales.rs b/src/format/locales.rs index 4cf4d4149a..c615476c0c 100644 --- a/src/format/locales.rs +++ b/src/format/locales.rs @@ -1,6 +1,6 @@ #[cfg(feature = "unstable-locales")] mod localized { - use pure_rust_locales::{locale_match, Locale}; + use pure_rust_locales::{Locale, locale_match}; pub(crate) const fn default_locale() -> Locale { Locale::POSIX diff --git a/src/format/mod.rs b/src/format/mod.rs index cd4520c217..fb608cc8a7 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -54,15 +54,15 @@ pub mod strftime; // not require `alloc`. pub(crate) mod locales; +pub use formatting::SecondsFormat; pub(crate) use formatting::write_hundreds; #[cfg(feature = "alloc")] pub(crate) use formatting::write_rfc2822; #[cfg(any(feature = "alloc", feature = "serde"))] pub(crate) use formatting::write_rfc3339; -pub use formatting::SecondsFormat; #[cfg(feature = "alloc")] #[allow(deprecated)] -pub use formatting::{format, format_item, DelayedFormat}; +pub use formatting::{DelayedFormat, format, format_item}; #[cfg(feature = "unstable-locales")] pub use locales::Locale; pub(crate) use parse::parse_rfc3339; diff --git a/src/format/parse.rs b/src/format/parse.rs index 5a3a702734..91a586548d 100644 --- a/src/format/parse.rs +++ b/src/format/parse.rs @@ -8,9 +8,9 @@ use core::borrow::Borrow; use core::str; use super::scan; +use super::{BAD_FORMAT, INVALID, OUT_OF_RANGE, TOO_LONG, TOO_SHORT}; use super::{Fixed, InternalFixed, InternalInternal, Item, Numeric, Pad, Parsed}; use super::{ParseError, ParseResult}; -use super::{BAD_FORMAT, INVALID, OUT_OF_RANGE, TOO_LONG, TOO_SHORT}; use crate::{DateTime, FixedOffset, Weekday}; fn set_weekday_with_num_days_from_sunday(p: &mut Parsed, v: i64) -> ParseResult<()> { @@ -640,15 +640,17 @@ mod tests { // most unicode whitespace characters parses( "\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}", - &[Space("\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}")] + &[Space( + "\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}", + )], ); // most unicode whitespace characters parses( "\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}", &[ Space("\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}"), - Space("\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}") - ] + Space("\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}"), + ], ); check("a", &[Space("")], Err(TOO_LONG)); check("a", &[Space(" ")], Err(TOO_LONG)); @@ -1835,8 +1837,10 @@ mod tests { #[test] fn test_issue_1010() { - let dt = crate::NaiveDateTime::parse_from_str("\u{c}SUN\u{e}\u{3000}\0m@J\u{3000}\0\u{3000}\0m\u{c}!\u{c}\u{b}\u{c}\u{c}\u{c}\u{c}%A\u{c}\u{b}\0SU\u{c}\u{c}", - "\u{c}\u{c}%A\u{c}\u{b}\0SUN\u{c}\u{c}\u{c}SUNN\u{c}\u{c}\u{c}SUN\u{c}\u{c}!\u{c}\u{b}\u{c}\u{c}\u{c}\u{c}%A\u{c}\u{b}%a"); + let dt = crate::NaiveDateTime::parse_from_str( + "\u{c}SUN\u{e}\u{3000}\0m@J\u{3000}\0\u{3000}\0m\u{c}!\u{c}\u{b}\u{c}\u{c}\u{c}\u{c}%A\u{c}\u{b}\0SU\u{c}\u{c}", + "\u{c}\u{c}%A\u{c}\u{b}\0SUN\u{c}\u{c}\u{c}SUNN\u{c}\u{c}\u{c}SUN\u{c}\u{c}!\u{c}\u{b}\u{c}\u{c}\u{c}\u{c}%A\u{c}\u{b}%a", + ); assert_eq!(dt, Err(ParseError(ParseErrorKind::Invalid))); } } diff --git a/src/format/parsed.rs b/src/format/parsed.rs index e14f03f425..b6a5291390 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -4,7 +4,7 @@ //! A collection of parsed date and time items. //! They can be constructed incrementally while being checked for consistency. -use super::{ParseResult, IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE}; +use super::{IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE, ParseResult}; use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime}; use crate::offset::{FixedOffset, MappedLocalTime, Offset, TimeZone}; use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday}; @@ -176,11 +176,7 @@ pub struct Parsed { #[inline] fn set_if_consistent(old: &mut Option, new: T) -> ParseResult<()> { if let Some(ref old) = *old { - if *old == new { - Ok(()) - } else { - Err(IMPOSSIBLE) - } + if *old == new { Ok(()) } else { Err(IMPOSSIBLE) } } else { *old = Some(new); Ok(()) @@ -701,11 +697,7 @@ impl Parsed { (_, _, _) => return Err(NOT_ENOUGH), }; - if verified { - Ok(parsed_date) - } else { - Err(IMPOSSIBLE) - } + if verified { Ok(parsed_date) } else { Err(IMPOSSIBLE) } } /// Returns a parsed naive time out of given fields. @@ -1166,10 +1158,10 @@ fn resolve_week_date( mod tests { use super::super::{IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE}; use super::Parsed; - use crate::naive::{NaiveDate, NaiveTime}; - use crate::offset::{FixedOffset, TimeZone, Utc}; use crate::Datelike; use crate::Weekday::*; + use crate::naive::{NaiveDate, NaiveTime}; + use crate::offset::{FixedOffset, TimeZone, Utc}; #[test] fn test_parsed_set_fields() { diff --git a/src/format/scan.rs b/src/format/scan.rs index 1ab87b9dd5..68958fd25f 100644 --- a/src/format/scan.rs +++ b/src/format/scan.rs @@ -5,7 +5,7 @@ * Various scanning routines for the parser. */ -use super::{ParseResult, INVALID, OUT_OF_RANGE, TOO_SHORT}; +use super::{INVALID, OUT_OF_RANGE, ParseResult, TOO_SHORT}; use crate::Weekday; /// Tries to parse the non-negative number from `min` to `max` digits. @@ -217,11 +217,7 @@ where const fn digits(s: &str) -> ParseResult<(u8, u8)> { let b = s.as_bytes(); - if b.len() < 2 { - Err(TOO_SHORT) - } else { - Ok((b[0], b[1])) - } + if b.len() < 2 { Err(TOO_SHORT) } else { Ok((b[0], b[1])) } } let negative = match s.chars().next() { Some('+') => { @@ -359,8 +355,8 @@ mod tests { comment_2822, nanosecond, nanosecond_fixed, short_or_long_month0, short_or_long_weekday, timezone_offset_2822, }; - use crate::format::{INVALID, TOO_SHORT}; use crate::Weekday; + use crate::format::{INVALID, TOO_SHORT}; #[test] fn test_rfc2822_comments() { diff --git a/src/format/strftime.rs b/src/format/strftime.rs index e446c68c02..e2bd4f5b57 100644 --- a/src/format/strftime.rs +++ b/src/format/strftime.rs @@ -161,12 +161,12 @@ Notes: #[cfg(feature = "alloc")] extern crate alloc; -use super::{fixed, internal_fixed, num, num0, nums}; -#[cfg(feature = "unstable-locales")] -use super::{locales, Locale}; -use super::{Fixed, InternalInternal, Item, Numeric, Pad}; #[cfg(any(feature = "alloc", feature = "std"))] -use super::{ParseError, BAD_FORMAT}; +use super::{BAD_FORMAT, ParseError}; +use super::{Fixed, InternalInternal, Item, Numeric, Pad}; +#[cfg(feature = "unstable-locales")] +use super::{Locale, locales}; +use super::{fixed, internal_fixed, num, num0, nums}; #[cfg(all(feature = "alloc", not(feature = "std"), not(test)))] use alloc::vec::Vec; @@ -686,8 +686,8 @@ mod tests { use crate::format::Item::{self, Literal, Space}; #[cfg(feature = "unstable-locales")] use crate::format::Locale; - use crate::format::{fixed, internal_fixed, num, num0, nums}; use crate::format::{Fixed, InternalInternal, Numeric::*}; + use crate::format::{fixed, internal_fixed, num, num0, nums}; #[cfg(feature = "alloc")] use crate::{DateTime, FixedOffset, NaiveDate, TimeZone, Timelike, Utc}; diff --git a/src/month.rs b/src/month.rs index ee5aa910f1..1b4dbfd7b8 100644 --- a/src/month.rs +++ b/src/month.rs @@ -3,8 +3,8 @@ use core::fmt; #[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))] use rkyv::{Archive, Deserialize, Serialize}; -use crate::naive::NaiveDate; use crate::OutOfRange; +use crate::naive::NaiveDate; /// The month of the year. /// @@ -395,8 +395,8 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_serde_serialize() { - use serde_json::to_string; use Month::*; + use serde_json::to_string; let cases: Vec<(Month, &str)> = vec![ (January, "\"January\""), @@ -422,8 +422,8 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_serde_deserialize() { - use serde_json::from_str; use Month::*; + use serde_json::from_str; let cases: Vec<(&str, Month)> = vec![ ("\"january\"", January), diff --git a/src/naive/date/mod.rs b/src/naive/date/mod.rs index 4977b7daa8..62efd45459 100644 --- a/src/naive/date/mod.rs +++ b/src/naive/date/mod.rs @@ -30,13 +30,13 @@ use pure_rust_locales::Locale; #[cfg(feature = "alloc")] use crate::format::DelayedFormat; use crate::format::{ - parse, parse_and_remainder, write_hundreds, Item, Numeric, Pad, ParseError, ParseResult, - Parsed, StrftimeItems, + Item, Numeric, Pad, ParseError, ParseResult, Parsed, StrftimeItems, parse, parse_and_remainder, + write_hundreds, }; use crate::month::Months; use crate::naive::{Days, IsoWeek, NaiveDateTime, NaiveTime, NaiveWeek}; -use crate::{expect, try_opt}; use crate::{Datelike, TimeDelta, Weekday}; +use crate::{expect, try_opt}; use super::internals::{Mdf, YearFlags}; diff --git a/src/naive/date/tests.rs b/src/naive/date/tests.rs index 2e2c2c396b..5c944d59db 100644 --- a/src/naive/date/tests.rs +++ b/src/naive/date/tests.rs @@ -1,5 +1,5 @@ -use super::{Days, Months, NaiveDate, MAX_YEAR, MIN_YEAR}; -use crate::naive::internals::{YearFlags, A, AG, B, BA, C, CB, D, DC, E, ED, F, FE, G, GF}; +use super::{Days, MAX_YEAR, MIN_YEAR, Months, NaiveDate}; +use crate::naive::internals::{A, AG, B, BA, C, CB, D, DC, E, ED, F, FE, G, GF, YearFlags}; use crate::{Datelike, TimeDelta, Weekday}; // as it is hard to verify year flags in `NaiveDate::MIN` and `NaiveDate::MAX`, diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index a2ffc69674..b634f8f432 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -15,14 +15,14 @@ use rkyv::{Archive, Deserialize, Serialize}; #[cfg(feature = "alloc")] use crate::format::DelayedFormat; -use crate::format::{parse, parse_and_remainder, ParseError, ParseResult, Parsed, StrftimeItems}; use crate::format::{Fixed, Item, Numeric, Pad}; +use crate::format::{ParseError, ParseResult, Parsed, StrftimeItems, parse, parse_and_remainder}; use crate::naive::{Days, IsoWeek, NaiveDate, NaiveTime}; use crate::offset::Utc; use crate::time_delta::NANOS_PER_SEC; use crate::{ - expect, try_opt, DateTime, Datelike, FixedOffset, MappedLocalTime, Months, TimeDelta, TimeZone, - Timelike, Weekday, + DateTime, Datelike, FixedOffset, MappedLocalTime, Months, TimeDelta, TimeZone, Timelike, + Weekday, expect, try_opt, }; /// Tools to help serializing/deserializing `NaiveDateTime`s diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index 161245813d..33eb57bdaf 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -199,11 +199,10 @@ fn test_datetime_parse_from_str() { NaiveDateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT"), Ok(ymdhms(2013, 8, 9, 23, 54, 35)) ); - assert!(NaiveDateTime::parse_from_str( - "Sat, 09 Aug 2013 23:54:35 GMT", - "%a, %d %b %Y %H:%M:%S GMT" - ) - .is_err()); + assert!( + NaiveDateTime::parse_from_str("Sat, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT") + .is_err() + ); assert!(NaiveDateTime::parse_from_str("2014-5-7 12:3456", "%Y-%m-%d %H:%M:%S").is_err()); assert!(NaiveDateTime::parse_from_str("12:34:56", "%H:%M:%S").is_err()); // insufficient assert_eq!( diff --git a/src/naive/internals.rs b/src/naive/internals.rs index da591f8d08..765a930be8 100644 --- a/src/naive/internals.rs +++ b/src/naive/internals.rs @@ -381,7 +381,7 @@ impl fmt::Debug for Mdf { #[cfg(test)] mod tests { use super::Mdf; - use super::{YearFlags, A, AG, B, BA, C, CB, D, DC, E, ED, F, FE, G, GF}; + use super::{A, AG, B, BA, C, CB, D, DC, E, ED, F, FE, G, GF, YearFlags}; const NONLEAP_FLAGS: [YearFlags; 7] = [A, B, C, D, E, F, G]; const LEAP_FLAGS: [YearFlags; 7] = [AG, BA, CB, DC, ED, FE, GF]; diff --git a/src/naive/isoweek.rs b/src/naive/isoweek.rs index 93d0dc423f..c572011aed 100644 --- a/src/naive/isoweek.rs +++ b/src/naive/isoweek.rs @@ -164,8 +164,8 @@ impl fmt::Debug for IsoWeek { mod tests { #[cfg(feature = "rkyv-validation")] use super::IsoWeek; - use crate::naive::date::{self, NaiveDate}; use crate::Datelike; + use crate::naive::date::{self, NaiveDate}; #[test] fn test_iso_week_extremes() { diff --git a/src/naive/mod.rs b/src/naive/mod.rs index 83ab69c4ce..5be9a50858 100644 --- a/src/naive/mod.rs +++ b/src/naive/mod.rs @@ -6,8 +6,8 @@ use core::ops::RangeInclusive; -use crate::expect; use crate::Weekday; +use crate::expect; pub(crate) mod date; pub(crate) mod datetime; @@ -15,11 +15,11 @@ mod internals; pub(crate) mod isoweek; pub(crate) mod time; -pub use self::date::{NaiveDate, NaiveDateDaysIterator, NaiveDateWeeksIterator}; #[allow(deprecated)] pub use self::date::{MAX_DATE, MIN_DATE}; +pub use self::date::{NaiveDate, NaiveDateDaysIterator, NaiveDateWeeksIterator}; #[allow(deprecated)] -pub use self::datetime::{NaiveDateTime, MAX_DATETIME, MIN_DATETIME}; +pub use self::datetime::{MAX_DATETIME, MIN_DATETIME, NaiveDateTime}; pub use self::isoweek::IsoWeek; pub use self::time::NaiveTime; diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index f32d828080..a0b92836f1 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -15,11 +15,11 @@ use rkyv::{Archive, Deserialize, Serialize}; #[cfg(feature = "alloc")] use crate::format::DelayedFormat; use crate::format::{ - parse, parse_and_remainder, write_hundreds, Fixed, Item, Numeric, Pad, ParseError, ParseResult, - Parsed, StrftimeItems, + Fixed, Item, Numeric, Pad, ParseError, ParseResult, Parsed, StrftimeItems, parse, + parse_and_remainder, write_hundreds, }; -use crate::{expect, try_opt}; use crate::{FixedOffset, TimeDelta, Timelike}; +use crate::{expect, try_opt}; #[cfg(feature = "serde")] mod serde; diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index e7382bed1d..f32f0f05e9 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -10,7 +10,7 @@ use core::str::FromStr; use rkyv::{Archive, Deserialize, Serialize}; use super::{MappedLocalTime, Offset, TimeZone}; -use crate::format::{scan, ParseError, OUT_OF_RANGE}; +use crate::format::{OUT_OF_RANGE, ParseError, scan}; use crate::naive::{NaiveDate, NaiveDateTime}; /// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59. diff --git a/src/offset/local/mod.rs b/src/offset/local/mod.rs index 611fe18d8d..a1ba3fac65 100644 --- a/src/offset/local/mod.rs +++ b/src/offset/local/mod.rs @@ -11,9 +11,9 @@ use rkyv::{Archive, Deserialize, Serialize}; use super::fixed::FixedOffset; use super::{MappedLocalTime, TimeZone}; -use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime}; #[allow(deprecated)] use crate::Date; +use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime}; use crate::{DateTime, Utc}; #[cfg(unix)] @@ -274,9 +274,9 @@ fn lookup_with_dst_transitions( #[cfg(test)] mod tests { use super::Local; - #[cfg(windows)] - use crate::offset::local::{lookup_with_dst_transitions, Transition}; use crate::offset::TimeZone; + #[cfg(windows)] + use crate::offset::local::{Transition, lookup_with_dst_transitions}; use crate::{Datelike, Days, Utc}; #[cfg(windows)] use crate::{FixedOffset, MappedLocalTime, NaiveDate, NaiveDateTime}; diff --git a/src/offset/local/tz_info/parser.rs b/src/offset/local/tz_info/parser.rs index 509c4fef99..84c99063c1 100644 --- a/src/offset/local/tz_info/parser.rs +++ b/src/offset/local/tz_info/parser.rs @@ -3,9 +3,9 @@ use std::iter; use std::num::ParseIntError; use std::str::{self, FromStr}; +use super::Error; use super::rule::TransitionRule; use super::timezone::{LeapSecond, LocalTimeType, TimeZone, Transition}; -use super::Error; pub(super) fn parse(bytes: &[u8]) -> Result { let mut cursor = Cursor::new(bytes); @@ -14,7 +14,7 @@ pub(super) fn parse(bytes: &[u8]) -> Result { Version::V1 => match cursor.is_empty() { true => (state, None), false => { - return Err(Error::InvalidTzFile("remaining data after end of TZif v1 data block")) + return Err(Error::InvalidTzFile("remaining data after end of TZif v1 data block")); } }, Version::V2 | Version::V3 => { diff --git a/src/offset/local/tz_info/rule.rs b/src/offset/local/tz_info/rule.rs index a99fe253af..d428395db0 100644 --- a/src/offset/local/tz_info/rule.rs +++ b/src/offset/local/tz_info/rule.rs @@ -1,7 +1,7 @@ use super::parser::Cursor; use super::timezone::{LocalTimeType, SECONDS_PER_WEEK}; use super::{ - Error, CUMUL_DAY_IN_MONTHS_NORMAL_YEAR, DAYS_PER_WEEK, DAY_IN_MONTHS_NORMAL_YEAR, + CUMUL_DAY_IN_MONTHS_NORMAL_YEAR, DAY_IN_MONTHS_NORMAL_YEAR, DAYS_PER_WEEK, Error, SECONDS_PER_DAY, }; use crate::{Datelike, NaiveDateTime}; @@ -39,7 +39,7 @@ impl TransitionRule { Some(&b',') => std_offset - 3600, Some(_) => parse_offset(&mut cursor)?, None => { - return Err(Error::UnsupportedTzString("DST start and end rules must be provided")) + return Err(Error::UnsupportedTzString("DST start and end rules must be provided")); } }; @@ -219,11 +219,7 @@ impl AlternateTime { } }; - if is_dst { - Ok(&self.dst) - } else { - Ok(&self.std) - } + if is_dst { Ok(&self.dst) } else { Ok(&self.std) } } fn find_local_time_type_from_local( diff --git a/src/offset/local/tz_info/timezone.rs b/src/offset/local/tz_info/timezone.rs index d33608d4aa..61d4498a94 100644 --- a/src/offset/local/tz_info/timezone.rs +++ b/src/offset/local/tz_info/timezone.rs @@ -1,13 +1,13 @@ //! Types related to a time zone. -use super::rule::{AlternateTime, TransitionRule}; -use super::{parser, Error, DAYS_PER_WEEK, SECONDS_PER_DAY}; -use crate::NaiveDateTime; use std::fs::{self, File}; use std::io::{self, Read}; use std::path::{Path, PathBuf}; use std::{cmp::Ordering, fmt, str}; +use super::rule::{AlternateTime, TransitionRule}; +use super::{DAYS_PER_WEEK, Error, SECONDS_PER_DAY, parser}; +use crate::NaiveDateTime; #[cfg(target_env = "ohos")] use crate::offset::local::tz_info::parser::Cursor; diff --git a/src/offset/local/windows.rs b/src/offset/local/windows.rs index 100b1f1eef..dcab6c44e6 100644 --- a/src/offset/local/windows.rs +++ b/src/offset/local/windows.rs @@ -14,7 +14,7 @@ use std::ptr; use super::win_bindings::{GetTimeZoneInformationForYear, SYSTEMTIME, TIME_ZONE_INFORMATION}; -use crate::offset::local::{lookup_with_dst_transitions, Transition}; +use crate::offset::local::{Transition, lookup_with_dst_transitions}; use crate::{Datelike, FixedOffset, MappedLocalTime, NaiveDate, NaiveDateTime, NaiveTime, Weekday}; // We don't use `SystemTimeToTzSpecificLocalTime` because it doesn't support the same range of dates @@ -213,7 +213,7 @@ fn naive_date_time_from_system_time( #[cfg(test)] mod tests { use crate::offset::local::win_bindings::{ - SystemTimeToFileTime, TzSpecificLocalTimeToSystemTime, FILETIME, SYSTEMTIME, + FILETIME, SYSTEMTIME, SystemTimeToFileTime, TzSpecificLocalTimeToSystemTime, }; use crate::{DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, TimeDelta}; use crate::{Datelike, TimeZone, Timelike}; diff --git a/src/offset/mod.rs b/src/offset/mod.rs index 346f809732..09f6821dce 100644 --- a/src/offset/mod.rs +++ b/src/offset/mod.rs @@ -20,9 +20,9 @@ use core::fmt; -use crate::format::{parse, ParseResult, Parsed, StrftimeItems}; -use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime}; use crate::Weekday; +use crate::format::{ParseResult, Parsed, StrftimeItems, parse}; +use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime}; #[allow(deprecated)] use crate::{Date, DateTime}; diff --git a/src/round.rs b/src/round.rs index 6751562a62..f5ca2d0634 100644 --- a/src/round.rs +++ b/src/round.rs @@ -365,8 +365,8 @@ impl std::error::Error for RoundingError { #[cfg(test)] mod tests { use super::{DurationRound, RoundingError, SubsecRound, TimeDelta}; - use crate::offset::{FixedOffset, TimeZone, Utc}; use crate::Timelike; + use crate::offset::{FixedOffset, TimeZone, Utc}; use crate::{DateTime, NaiveDate}; #[test] diff --git a/src/time_delta.rs b/src/time_delta.rs index 3eb041ad57..d891d8b6b2 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -301,22 +301,14 @@ impl TimeDelta { /// Returns the total number of whole seconds in the `TimeDelta`. pub const fn num_seconds(&self) -> i64 { // If secs is negative, nanos should be subtracted from the duration. - if self.secs < 0 && self.nanos > 0 { - self.secs + 1 - } else { - self.secs - } + if self.secs < 0 && self.nanos > 0 { self.secs + 1 } else { self.secs } } /// Returns the number of nanoseconds such that /// `subsec_nanos() + num_seconds() * NANOS_PER_SEC` is the total number of /// nanoseconds in the `TimeDelta`. pub const fn subsec_nanos(&self) -> i32 { - if self.secs < 0 && self.nanos > 0 { - self.nanos - NANOS_PER_SEC - } else { - self.nanos - } + if self.secs < 0 && self.nanos > 0 { self.nanos - NANOS_PER_SEC } else { self.nanos } } /// Returns the total number of whole milliseconds in the `TimeDelta`. @@ -642,7 +634,7 @@ impl arbitrary::Arbitrary<'_> for TimeDelta { #[cfg(feature = "serde")] mod serde { use super::TimeDelta; - use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; + use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error}; impl Serialize for TimeDelta { fn serialize(&self, serializer: S) -> Result { @@ -683,7 +675,7 @@ mod serde { #[cfg(test)] mod tests { use super::OutOfRangeError; - use super::{TimeDelta, MAX, MIN}; + use super::{MAX, MIN, TimeDelta}; use crate::expect; use core::time::Duration; @@ -803,10 +795,12 @@ mod tests { fn test_duration_milliseconds_max_overflow() { // Here we ensure that trying to add one millisecond to the maximum storable // value will fail. - assert!(TimeDelta::try_milliseconds(i64::MAX) - .unwrap() - .checked_add(&TimeDelta::try_milliseconds(1).unwrap()) - .is_none()); + assert!( + TimeDelta::try_milliseconds(i64::MAX) + .unwrap() + .checked_add(&TimeDelta::try_milliseconds(1).unwrap()) + .is_none() + ); } #[test] @@ -826,10 +820,12 @@ mod tests { fn test_duration_milliseconds_min_underflow() { // Here we ensure that trying to subtract one millisecond from the minimum // storable value will fail. - assert!(TimeDelta::try_milliseconds(-i64::MAX) - .unwrap() - .checked_sub(&TimeDelta::try_milliseconds(1).unwrap()) - .is_none()); + assert!( + TimeDelta::try_milliseconds(-i64::MAX) + .unwrap() + .checked_sub(&TimeDelta::try_milliseconds(1).unwrap()) + .is_none() + ); } #[test] @@ -905,10 +901,12 @@ mod tests { ); // Here we ensure that trying to add one microsecond to the maximum storable // value will fail. - assert!(TimeDelta::try_milliseconds(i64::MAX) - .unwrap() - .checked_add(&TimeDelta::microseconds(1)) - .is_none()); + assert!( + TimeDelta::try_milliseconds(i64::MAX) + .unwrap() + .checked_add(&TimeDelta::microseconds(1)) + .is_none() + ); } #[test] fn test_duration_microseconds_min_allowed() { @@ -944,10 +942,12 @@ mod tests { ); // Here we ensure that trying to subtract one microsecond from the minimum // storable value will fail. - assert!(TimeDelta::try_milliseconds(-i64::MAX) - .unwrap() - .checked_sub(&TimeDelta::microseconds(1)) - .is_none()); + assert!( + TimeDelta::try_milliseconds(-i64::MAX) + .unwrap() + .checked_sub(&TimeDelta::microseconds(1)) + .is_none() + ); } #[test] @@ -1009,10 +1009,12 @@ mod tests { ); // Here we ensure that trying to add one nanosecond to the maximum storable // value will fail. - assert!(TimeDelta::try_milliseconds(i64::MAX) - .unwrap() - .checked_add(&TimeDelta::nanoseconds(1)) - .is_none()); + assert!( + TimeDelta::try_milliseconds(i64::MAX) + .unwrap() + .checked_add(&TimeDelta::nanoseconds(1)) + .is_none() + ); } #[test] @@ -1049,10 +1051,12 @@ mod tests { ); // Here we ensure that trying to subtract one nanosecond from the minimum // storable value will fail. - assert!(TimeDelta::try_milliseconds(-i64::MAX) - .unwrap() - .checked_sub(&TimeDelta::nanoseconds(1)) - .is_none()); + assert!( + TimeDelta::try_milliseconds(-i64::MAX) + .unwrap() + .checked_sub(&TimeDelta::nanoseconds(1)) + .is_none() + ); } #[test] diff --git a/src/traits.rs b/src/traits.rs index e582093b0b..0771b5c1a5 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -37,11 +37,7 @@ pub trait Datelike: Sized { #[inline] fn year_ce(&self) -> (bool, u32) { let year = self.year(); - if year < 1 { - (false, (1 - year) as u32) - } else { - (true, year as u32) - } + if year < 1 { (false, (1 - year) as u32) } else { (true, year as u32) } } /// Returns the month number starting from 1. diff --git a/src/weekday.rs b/src/weekday.rs index 92644e8f6a..d2ffa4b624 100644 --- a/src/weekday.rs +++ b/src/weekday.rs @@ -161,11 +161,7 @@ impl Weekday { pub const fn days_since(&self, other: Weekday) -> u32 { let lhs = *self as u32; let rhs = other as u32; - if lhs < rhs { - 7 + lhs - rhs - } else { - lhs - rhs - } + if lhs < rhs { 7 + lhs - rhs } else { lhs - rhs } } } @@ -344,8 +340,8 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_serde_serialize() { - use serde_json::to_string; use Weekday::*; + use serde_json::to_string; let cases: Vec<(Weekday, &str)> = vec![ (Mon, "\"Mon\""), @@ -366,8 +362,8 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_serde_deserialize() { - use serde_json::from_str; use Weekday::*; + use serde_json::from_str; let cases: Vec<(&str, Weekday)> = vec![ ("\"mon\"", Mon),