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§
Implementations§
Trait Implementations§
Source§impl TryFrom<&str> for MonthName
Convert a string into a MonthName.
impl TryFrom<&str> for MonthName
Convert a string into a MonthName.
Three strategies are tried in order:
-
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. -
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::UnrecognisedNamewhen no candidate passes. -
Numeric match — if every character is an ASCII digit, the value is parsed as a
u8and forwarded toTryFrom<u8>. ReturnsMonthNameError::NumberOutOfRangewhen 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§impl TryFrom<u8> for MonthName
Convert a month number (1 = January … 12 = December) into a
MonthName.
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.