Skip to main content

MonthName

Enum MonthName 

Source
pub enum MonthName {
    January,
    February,
    March,
    April,
    May,
    June,
    July,
    August,
    September,
    October,
    November,
    December,
}
Expand description

The name of a calendar month, as extracted from natural language input.

§Conversions

MonthName can be constructed from either a string or a number:

use partial_date::models::{MonthName, MonthNameError};
use std::convert::TryFrom;

// From a name string (full, abbreviated, or unambiguous prefix)
assert_eq!(MonthName::try_from("October"), Ok(MonthName::October));
assert_eq!(MonthName::try_from("oct"),     Ok(MonthName::October));
assert_eq!(MonthName::try_from("Octo"),    Ok(MonthName::October));

// From a numeric string
assert_eq!(MonthName::try_from("10"), Ok(MonthName::October));

// From a u8
assert_eq!(MonthName::try_from(10_u8), Ok(MonthName::October));

// Errors
assert_eq!(MonthName::try_from(0_u8),  Err(MonthNameError::NumberOutOfRange(0)));
assert_eq!(MonthName::try_from(13_u8), Err(MonthNameError::NumberOutOfRange(13)));
assert_eq!(MonthName::try_from("Xyz"), Err(MonthNameError::UnrecognisedName));
assert_eq!(MonthName::try_from("5x"),  Err(MonthNameError::NotAMonth));

Variants§

§

January

§

February

§

March

§

April

§

May

§

June

§

July

§

August

§

September

§

October

§

November

§

December

Implementations§

Source§

impl MonthName

Source

pub fn number(self) -> u8

Return the calendar number of this month (1 = January … 12 = December).

use partial_date::models::MonthName;
assert_eq!(MonthName::January.number(), 1);
assert_eq!(MonthName::December.number(), 12);

Trait Implementations§

Source§

impl Clone for MonthName

Source§

fn clone(&self) -> MonthName

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MonthName

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for MonthName

Source§

fn eq(&self, other: &MonthName) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<&str> for MonthName

Convert a string into a MonthName.

Three strategies are tried in order:

  1. Alphabetic match — if every character is ASCII alphabetic (after stripping a trailing .), the lowercased string is compared against all full names, standard 3-letter abbreviations, and unambiguous longer prefixes.

  2. Fuzzy match — if no exact or prefix match was found, the Levenshtein ratio is computed against every full month name. The closest match is accepted when its ratio is ≥ 0.6 and it is unambiguously the best candidate (no tie). Returns MonthNameError::UnrecognisedName when no candidate passes.

  3. Numeric match — if every character is an ASCII digit, the value is parsed as a u8 and forwarded to TryFrom<u8>. Returns MonthNameError::NumberOutOfRange when the number is outside 1–12.

If the string is neither purely alphabetic nor purely numeric (e.g. "jan2" or "5x"), MonthNameError::NotAMonth is returned.

Source§

type Error = MonthNameError

The type returned in the event of a conversion error.
Source§

fn try_from(s: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<u8> for MonthName

Convert a month number (1 = January … 12 = December) into a MonthName.

Returns MonthNameError::NumberOutOfRange for any value outside 1–12.

Source§

type Error = MonthNameError

The type returned in the event of a conversion error.
Source§

fn try_from(n: u8) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for MonthName

Source§

impl Eq for MonthName

Source§

impl StructuralPartialEq for MonthName

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.