Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
esheppa committed Nov 19, 2022
1 parent 7e2604e commit 9a0585a
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 197 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- run: cargo test --doc --all-features --color=always -- --color=always

# later this may be able to be included with the below
# kept seperate for now as the following don't compile on 1.38.0
# kept seperate for now as the following don't compile on 1.47.0
# * rkyv
# * criterion
rust_msrv:
Expand All @@ -49,7 +49,7 @@ jobs:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.38.0
toolchain: 1.47.0
- uses: Swatinem/rust-cache@v1
# run --lib and --doc to avoid the long running integration tests which are run elsewhere
- run: cargo test --lib --features unstable-locales,wasmbind,clock,serde,winapi --color=always -- --color=always
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ __doctest = []
const-validation = []

[dependencies]
num-integer = { version = "0.1.36", default-features = false }
serde = { version = "1.0.99", default-features = false, optional = true }
pure-rust-locales = { version = "0.5.2", optional = true }
criterion = { version = "0.4.0", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions ci/github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ meaningful in the github actions feature matrix UI.

runv cargo --version

if [[ ${RUST_VERSION:-} != 1.38.0 ]]; then
if [[ ${RUST_VERSION:-} != 1.47.0 ]]; then
if [[ ${WASM:-} == yes_wasm ]]; then
test_wasm
elif [[ ${WASM:-} == wasm_simple ]]; then
Expand All @@ -51,7 +51,7 @@ meaningful in the github actions feature matrix UI.
else
test_regular UTC0
fi
elif [[ ${RUST_VERSION:-} == 1.38.0 ]]; then
elif [[ ${RUST_VERSION:-} == 1.47.0 ]]; then
test_132
else
echo "ERROR: didn't run any tests"
Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.38"
msrv = "1.47"
25 changes: 14 additions & 11 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ impl ParseError {

/// The category of parse error
#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
#[allow(clippy::manual_non_exhaustive)]
pub enum ParseErrorKind {
/// Given field is out of permitted range.
OutOfRange,
Expand Down Expand Up @@ -488,8 +489,8 @@ fn format_inner<'a>(
)
};

use crate::naive::internals::{const_div_floor_i64, const_mod_floor_i64};
use core::fmt::Write;
use num_integer::{div_floor, mod_floor};

match *item {
Item::Literal(s) | Item::Space(s) => result.push_str(s),
Expand All @@ -508,31 +509,33 @@ fn format_inner<'a>(

let (width, v) = match *spec {
Year => (4, date.map(|d| i64::from(d.year()))),
YearDiv100 => (2, date.map(|d| div_floor(i64::from(d.year()), 100))),
YearMod100 => (2, date.map(|d| mod_floor(i64::from(d.year()), 100))),
YearDiv100 => (2, date.map(|d| const_div_floor_i64(i64::from(d.year()), 100))),
YearMod100 => (2, date.map(|d| const_mod_floor_i64(i64::from(d.year()), 100))),
IsoYear => (
4,
date.map(|d| (d.iso_week().map(|w| i64::from(w.year()))))
.ok_or_else(|| fmt::Error)?,
date.map(|d| (d.iso_week().map(|w| i64::from(w.year())))).ok_or(fmt::Error)?,
),
IsoYearDiv100 => (
2,
date.map(|d| (d.iso_week().map(|w| div_floor(i64::from(w.year()), 100))))
.ok_or_else(|| fmt::Error)?,
date.map(|d| {
(d.iso_week().map(|w| const_div_floor_i64(i64::from(w.year()), 100)))
})
.ok_or(fmt::Error)?,
),
IsoYearMod100 => (
2,
date.map(|d| (d.iso_week().map(|w| mod_floor(i64::from(w.year()), 100))))
.ok_or_else(|| fmt::Error)?,
date.map(|d| {
(d.iso_week().map(|w| const_mod_floor_i64(i64::from(w.year()), 100)))
})
.ok_or(fmt::Error)?,
),
Month => (2, date.map(|d| i64::from(d.month()))),
Day => (2, date.map(|d| i64::from(d.day()))),
WeekFromSun => (2, date.map(|d| i64::from(week_from_sun(d)))),
WeekFromMon => (2, date.map(|d| i64::from(week_from_mon(d)))),
IsoWeek => (
2,
date.map(|d| (d.iso_week().map(|w| i64::from(w.week()))))
.ok_or_else(|| fmt::Error)?,
date.map(|d| (d.iso_week().map(|w| i64::from(w.week())))).ok_or(fmt::Error)?,
),
NumDaysFromSun => (1, date.map(|d| i64::from(d.weekday().num_days_from_sunday()))),
WeekdayFromMon => (1, date.map(|d| i64::from(d.weekday().number_from_monday()))),
Expand Down
38 changes: 10 additions & 28 deletions src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use core::convert::TryFrom;

use num_integer::div_rem;

use super::{ParseResult, IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE};
use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
use crate::offset::{FixedOffset, LocalResult, Offset, TimeZone};
Expand All @@ -21,6 +19,7 @@ use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};
/// - `to_*` methods try to make a concrete date and time value out of set fields.
/// It fully checks any remaining out-of-range conditions and inconsistent/impossible fields.
#[derive(Clone, PartialEq, Eq, Debug, Default, Hash)]
#[allow(clippy::manual_non_exhaustive)]
pub struct Parsed {
/// Year.
///
Expand Down Expand Up @@ -299,7 +298,6 @@ impl Parsed {
q: Option<i16>,
r: Option<i16>,
) -> ParseResult<Option<i16>> {
dbg!(y, q, r);
match (y, q, r) {
// if there is no further information, simply return the given full year.
// this is a common case, so let's avoid division here.
Expand All @@ -313,7 +311,7 @@ impl Parsed {
if y < 0 {
return Err(OUT_OF_RANGE);
}
let (q_, r_) = div_rem(y, 100);
let (q_, r_) = (y / 100, y % 100);
if q.unwrap_or(q_) == q_ && r.unwrap_or(r_) == r_ {
Ok(Some(y))
} else {
Expand Down Expand Up @@ -345,7 +343,6 @@ impl Parsed {
q: Option<i32>,
r: Option<i32>,
) -> ParseResult<Option<i32>> {
dbg!(y, q, r);
match (y, q, r) {
// if there is no further information, simply return the given full year.
// this is a common case, so let's avoid division here.
Expand All @@ -359,7 +356,7 @@ impl Parsed {
if y < 0 {
return Err(OUT_OF_RANGE);
}
let (q_, r_) = div_rem(y, 100);
let (q_, r_) = (y / 100, y % 100);
if q.unwrap_or(q_) == q_ && r.unwrap_or(r_) == r_ {
Ok(Some(y))
} else {
Expand Down Expand Up @@ -391,13 +388,11 @@ impl Parsed {
let given_isoyear =
resolve_year_isoweek(self.isoyear, self.isoyear_div_100, self.isoyear_mod_100)?;

dbg!(given_year, given_isoyear);

// verify the normal year-month-day date.
let verify_ymd = |date: NaiveDate| {
let year = date.year();
let (year_div_100, year_mod_100) = if year >= 0 {
let (q, r) = div_rem(year, 100);
let (q, r) = (year / 100, year % 100);
(Some(q), Some(r))
} else {
(None, None) // they should be empty to be consistent
Expand All @@ -414,22 +409,20 @@ impl Parsed {
// verify the ISO week date.
let verify_isoweekdate = |date: NaiveDate| {
let week = date.iso_week().ok_or(OUT_OF_RANGE)?;
dbg!(week);
let isoyear = week.year();
let isoweek = week.week();
let weekday = date.weekday();
dbg!(isoyear, isoweek, weekday);
let (isoyear_div_100, isoyear_mod_100) = if isoyear >= 0 {
let (q, r) = div_rem(isoyear, 100);
let (q, r) = (isoyear / 100, isoyear % 100);
(Some(q), Some(r))
} else {
(None, None) // they should be empty to be consistent
};
Ok(self.isoyear.unwrap_or(isoyear) == isoyear
&& dbg!(self.isoyear_div_100.or(isoyear_div_100)) == isoyear_div_100
&& dbg!(self.isoyear_mod_100.or(isoyear_mod_100)) == isoyear_mod_100
&& dbg!(self.isoweek.unwrap_or(isoweek)) == isoweek
&& dbg!(self.weekday.unwrap_or(weekday)) == weekday)
&& self.isoyear_div_100.or(isoyear_div_100) == isoyear_div_100
&& self.isoyear_mod_100.or(isoyear_mod_100) == isoyear_mod_100
&& self.isoweek.unwrap_or(isoweek) == isoweek
&& self.weekday.unwrap_or(weekday) == weekday)
};

// verify the ordinal and other (non-ISO) week dates.
Expand All @@ -448,31 +441,22 @@ impl Parsed {
// it is consistent with other given fields.
let (verified, parsed_date) = match (given_year, given_isoyear, self) {
(Some(year), _, &Parsed { month: Some(month), day: Some(day), .. }) => {
dbg!(year, month, day);
// year, month, day
let date = NaiveDate::from_ymd_opt(year, month, day).ok_or(OUT_OF_RANGE)?;
(verify_isoweekdate(date)? && verify_ordinal(date), date)
}

(Some(year), _, &Parsed { ordinal: Some(ordinal), .. }) => {
dbg!(year, ordinal);
// year, day of the year
let date = NaiveDate::from_yo_opt(year, ordinal).ok_or(OUT_OF_RANGE)?;
dbg!(date);
(
dbg!(verify_ymd(date))
&& dbg!(verify_isoweekdate(date))?
&& dbg!(verify_ordinal(date)),
date,
)
(verify_ymd(date) && verify_isoweekdate(date)? && verify_ordinal(date), date)
}

(
Some(year),
_,
&Parsed { week_from_sun: Some(week_from_sun), weekday: Some(weekday), .. },
) => {
dbg!(year, week_from_sun, weekday);
// year, week (starting at 1st Sunday), day of the week
let newyear = NaiveDate::from_yo_opt(year, 1).ok_or(OUT_OF_RANGE)?;
let firstweek = match newyear.weekday() {
Expand Down Expand Up @@ -507,7 +491,6 @@ impl Parsed {
_,
&Parsed { week_from_mon: Some(week_from_mon), weekday: Some(weekday), .. },
) => {
dbg!(year, week_from_mon, weekday);
// year, week (starting at 1st Monday), day of the week
let newyear = NaiveDate::from_yo_opt(year, 1).ok_or(OUT_OF_RANGE)?;
let firstweek = match newyear.weekday() {
Expand Down Expand Up @@ -538,7 +521,6 @@ impl Parsed {
}

(_, Some(isoyear), &Parsed { isoweek: Some(isoweek), weekday: Some(weekday), .. }) => {
dbg!(isoyear, isoweek, weekday);
// ISO year, week, day of the week
let date = NaiveDate::from_isoywd_opt(isoyear, isoweek, weekday);
let date = date.ok_or(OUT_OF_RANGE)?;
Expand Down
20 changes: 20 additions & 0 deletions src/month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@ impl Month {
}
}

/// a
#[cfg(feature = "const-validation")]
pub const fn try_from_u8_validated(val: u8) -> Self {
match val {
1 => Month::January,
2 => Month::February,
3 => Month::March,
4 => Month::April,
5 => Month::May,
6 => Month::June,
7 => Month::July,
8 => Month::August,
9 => Month::September,
10 => Month::October,
11 => Month::November,
12 => Month::December,
_ => panic!("Invalid month value"),
}
}

/// a
pub const fn try_from_u16(val: u16) -> Option<Self> {
match val {
Expand Down
Loading

0 comments on commit 9a0585a

Please sign in to comment.