Skip to content

Commit

Permalink
add const validation
Browse files Browse the repository at this point in the history
  • Loading branch information
esheppa committed Nov 19, 2022
1 parent 002fb35 commit 7e2604e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use crate::month::Months;
use crate::naive::{IsoWeek, NaiveDateTime, NaiveTime};
use crate::{Datelike, Month, TimeDelta, Weekday};


use crate::try_opt;
use super::internals::DateImpl;
use super::isoweek;

Expand Down Expand Up @@ -129,7 +131,7 @@ pub struct Days(pub(crate) u64);

impl Days {
/// Construct a new `Days` from a number of months
pub fn new(num: u64) -> Self {
pub const fn new(num: u64) -> Self {
Self(num)
}
}
Expand Down Expand Up @@ -188,10 +190,10 @@ pub struct NaiveDate {
inner: DateImpl,
}

/// The minimum possible `NaiveDate` (January 1, 262145 BCE).
/// The minimum possible `NaiveDate` (January 1, -32769 BCE).
#[deprecated(since = "0.4.20", note = "Use NaiveDate::MIN instead")]
pub const MIN_DATE: NaiveDate = NaiveDate::MIN;
/// The maximum possible `NaiveDate` (December 31, 262143 CE).
/// The maximum possible `NaiveDate` (December 31, 32767 CE).
#[deprecated(since = "0.4.20", note = "Use NaiveDate::MAX instead")]
pub const MAX_DATE: NaiveDate = NaiveDate::MAX;

Expand Down Expand Up @@ -269,16 +271,22 @@ impl NaiveDate {
/// assert!(from_ymd_opt(400000, 1, 1).is_none());
/// assert!(from_ymd_opt(-400000, 1, 1).is_none());
/// ```
pub fn from_ymd_opt(year: i16, month: u8, day: u8) -> Option<NaiveDate> {
Some(NaiveDate { inner: DateImpl::from_ymd(year, Month::try_from(month).ok()?, day)? })
pub const fn from_ymd_opt(year: i16, month: u8, day: u8) -> Option<NaiveDate> {
Some(NaiveDate { inner: try_opt!(DateImpl::from_ymd(year, try_opt!(Month::try_from_u8(month)), day)) })
}

#[cfg(feature = "const-validation")]
///
pub const fn from_ymd_validated(year: i16, month: Month, day: u8) -> NaiveDate {
NaiveDate { inner:DateImpl::from_ymd_validated(year, month, day) }
}

/// Makes a new `NaiveDate` from the [ordinal date](#ordinal-date)
/// (year and day of the year).
///
/// Panics on the out-of-range date and/or invalid day of year.
#[cfg(feature = "const-validation")]
pub fn from_yo_validated(year: i16, ordinal: u16) -> NaiveDate {
pub const fn from_yo_validated(year: i16, ordinal: u16) -> NaiveDate {
NaiveDate { inner: DateImpl::from_yo_validated(year, ordinal) }
}

Expand Down
2 changes: 2 additions & 0 deletions src/naive/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub(super) const MIN_YEAR: i16 = i16::MIN;
const DAYS_IN_CYCLE: i32 = 146_097;

// useful in const fns
/// some docs
#[macro_export]
macro_rules! try_opt {
($e:expr) => {
match $e {
Expand Down
1 change: 1 addition & 0 deletions src/naive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

mod date;
pub(crate) mod datetime;
#[macro_use]
mod internals;
mod isoweek;
mod time;
Expand Down

0 comments on commit 7e2604e

Please sign in to comment.