Skip to content

Commit

Permalink
include in prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
hhamana authored and botahamec committed May 26, 2022
1 parent ce0a2d7 commit 7603b2e
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ pub mod prelude {
#[doc(no_inline)]
pub use {DateTime, SecondsFormat};
#[doc(no_inline)]
pub use {Datelike, Timelike, Weekday};
pub use {Datelike, Month, Timelike, Weekday};
#[doc(no_inline)]
pub use {FixedOffset, Utc};
#[doc(no_inline)]
Expand Down Expand Up @@ -898,20 +898,23 @@ mod weekday_serde {
}

/// The month of the year.
///
/// This enum is just a convenience implementation.
///
/// This enum is just a convenience implementation.
/// The month in dates created by DateLike objects does not return this enum.
///
///
/// It is possible to convert from a date to a month independently
/// ```
/// # extern crate num_traits;
/// use num_traits::FromPrimitive;
/// let date = Utc.ymd(2019, 10, 28).and_hms(9, 10, 11);
/// use chrono::prelude::*;
/// let date = Utc.ymd(2019, 10, 28).and_hms(9, 10, 11);
/// // `2019-10-28T09:10:11Z`
/// let month = Month::from_u32(date.month());
/// assert_eq!(month, Some(Month::October))
/// ```
/// Or from a Month to an integer usable by dates
/// ```
/// # use chrono::prelude::*;
/// let month = Month::January;
/// let dt = Utc.ymd(2019, month.number_from_month(), 28).and_hms(9, 10, 11);
/// assert_eq!((dt.year(), dt.month(), dt.day()), (2019, 1, 28));
Expand All @@ -931,24 +934,23 @@ pub enum Month {
/// April
April = 3,
/// May
May = 4,
May = 4,
/// June
June = 5,
/// July
July = 6,
/// August
August = 7,
August = 7,
/// September
September = 8,
/// October
October = 9,
October = 9,
/// November
November = 10,
/// December
December = 11
December = 11,
}


impl Month {
/// The next month.
///
Expand Down Expand Up @@ -1022,11 +1024,11 @@ impl Month {

impl num_traits::FromPrimitive for Month {
/// Returns an Option<Month> from a i64, assuming a 1-index, January = 1.
///
///
/// `Month::from_i64(n: i64)`: | `1` | `2` | ... | `12`
/// ---------------------------| -------------------- | --------------------- | ... | -----
/// ``: | Some(Month::January) | Some(Month::February) | ... | Some(Month::December)
#[inline]
fn from_u64(n: u64) -> Option<Month> {
Self::from_u32(n as u32)
Expand Down Expand Up @@ -1071,12 +1073,13 @@ impl fmt::Debug for ParseMonthError {
#[cfg(feature = "serde")]
mod month_serde {
use super::Month;
use serdelib::{de, ser};
use std::fmt;
use serdelib::{ser, de};

impl ser::Serialize for Month {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: ser::Serializer
where
S: ser::Serializer,
{
serializer.serialize_str(&format!("{:?}", self))
}
Expand All @@ -1092,15 +1095,17 @@ mod month_serde {
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where E: de::Error
where
E: de::Error,
{
value.parse().map_err(|_| E::custom("short (3-letter) or full month names expected"))
}
}

impl<'de> de::Deserialize<'de> for Month {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: de::Deserializer<'de>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_str(MonthVisitor)
}
Expand Down Expand Up @@ -1162,21 +1167,15 @@ mod month_serde {
assert_eq!(month, expected_month);
}

let errors: Vec<&str> = vec![
"\"not a month\"",
"\"ja\"",
"\"Dece\"",
"Dec",
"\"Augustin\""
];
let errors: Vec<&str> =
vec!["\"not a month\"", "\"ja\"", "\"Dece\"", "Dec", "\"Augustin\""];

for string in errors {
from_str::<Month>(string).unwrap_err();
}
}
}


/// The common set of methods for date component.
pub trait Datelike: Sized {
/// Returns the year number in the [calendar date](./naive/struct.NaiveDate.html#calendar-date).
Expand Down Expand Up @@ -1388,7 +1387,6 @@ mod test {
}
}


#[test]
fn test_month_enum_primitive_parse() {
use num_traits::FromPrimitive;
Expand All @@ -1402,7 +1400,7 @@ mod test {
assert_eq!(dec_opt, Some(Month::December));
assert_eq!(no_month, None);

let date = Utc.ymd(2019, 10, 28).and_hms(9, 10, 11);
let date = Utc.ymd(2019, 10, 28).and_hms(9, 10, 11);
assert_eq!(Month::from_u32(date.month()), Some(Month::October));

let month = Month::January;
Expand Down

0 comments on commit 7603b2e

Please sign in to comment.