Skip to main content

Century

Struct Century 

Source
pub struct Century(/* private fields */);
Expand description

A year that falls exactly on a century boundary (divisible by 100).

Used with TwoDigitYearExpansion::Always to express that all two-digit values should map into a specific century. For example, Century::new(1800) means 00 → 1800, 34 → 1834, 99 → 1899.

§Construction

Use Century::new for known-valid static values (panics on invalid input) or Century::try_new when the value comes from dynamic input and you need to handle the error:

use partial_date::models::Century;

// Known-valid — panics if the value is not a century boundary.
let century = Century::new(1800);

// Dynamic input — returns Result.
let century = Century::try_new(2000).unwrap();

// Via TryFrom.
let century: Century = 2000_i32.try_into().unwrap();

Implementations§

Source§

impl Century

Source

pub fn new(year: i32) -> Self

Create a new Century.

§Panics

Panics if year is not divisible by 100. Use Century::try_new when the value comes from dynamic input and you need to handle the error instead of panicking.

Examples found in repository?
examples/all_examples.rs (line 313)
298fn example_11_year_two_digit_always_2000s() {
299    println!("1️⃣1️⃣  TWO-DIGIT YEAR: Always(Century(2000))");
300    println!("   Input: '25/12/99'");
301    println!("   Config: Two-digit expansion = Always(Century(2000)) (all become 2000-2099)\n");
302
303    let input = Input {
304        utterance: "25/12/99".to_string(),
305        config: Some(Config {
306            component_order: ComponentOrder {
307                first: DateComponent::Day,
308                second: DateComponent::Month,
309                third: DateComponent::Year,
310            },
311            year: YearConfig {
312                expected: IsExpected::Yes,
313                two_digit_expansion: TwoDigitYearExpansion::Always(Century::new(2000)),
314                ..Default::default()
315            },
316            ..Default::default()
317        }),
318    };
319
320    let result = extract(input);
321    println!("   ✓ Input '99' expands to: {:?}", result.year.value);
322    println!("   (Note: normally 99 → 1999, here → 2099)\n");
323}
More examples
Hide additional examples
examples/console_entry_config.rs (line 145)
75    pub fn get_config(&self) -> Config {
76        match self {
77            // Strictly day-first numeric dates with all three components
78            // required. Letter-O substitution is disabled since this scenario
79            // expects clean numeric input only.
80            PreDefinedConfigs::StrictDMY => Config::default()
81                .with_day(
82                    DayConfig::default()
83                        .with_expected(IsExpected::Yes),
84                )
85                .with_month(
86                    MonthConfig::default()
87                        .with_expected(IsExpected::Yes)
88                )
89                .with_year(
90                    YearConfig::default()
91                        .with_range(1, 3000)
92                        .with_expected(IsExpected::Yes)
93                        .with_two_digit_expansion(TwoDigitYearExpansion::Literal),
94                )
95                .with_component_order(
96                    ComponentOrder::new(
97                        DateComponent::Day,
98                        DateComponent::Month,
99                        DateComponent::Year,
100                    )
101                    .unwrap(),
102                )
103                .with_letter_o_substitution(false),
104
105            // Wide ranging historical dates that cannot use 2-digit year
106            // expansion as there is no way to know which centuries the dates
107            // will be from. Minimal assumptions about the data when there is
108            // a lot of uncertainty.
109            PreDefinedConfigs::AllHistoricalDates => Config::default().with_year(
110                YearConfig::default()
111                    .with_range(1, 3000)
112                    .with_expected(IsExpected::Yes)
113                    .with_two_digit_expansion(TwoDigitYearExpansion::Literal),
114            ),
115
116            // Constrains the year range to the Industrial Revolution period and
117            // uses a sliding window for 2-digit years centred on 1800.
118            // Component order matches Great Britain's common DD/MM/YYYY format.
119            PreDefinedConfigs::IndustrialRevolutionDates => Config::default()
120                .with_year(
121                    YearConfig::default()
122                        .with_range(1760, 1840)
123                        .with_expected(IsExpected::Yes)
124                        .with_two_digit_expansion(TwoDigitYearExpansion::SlidingWindow {
125                            earliest_year: 1750,
126                            pivot: SlidingWindowPivot::new(50),
127                        }),
128                )
129                .with_component_order(
130                    ComponentOrder::new(
131                        DateComponent::Day,
132                        DateComponent::Month,
133                        DateComponent::Year,
134                    )
135                    .unwrap(),
136                ),
137
138            // Children under 18 — birth years must be in the 2000s and not in
139            // the future. Always(Century(2000)) maps all 2-digit values to the
140            // 2000s; the max of 2026 rejects future years.
141            PreDefinedConfigs::ChildrenBirthdays => Config::default().with_year(
142                YearConfig::default()
143                    .with_range(2000, 2026)
144                    .with_expected(IsExpected::Yes)
145                    .with_two_digit_expansion(TwoDigitYearExpansion::Always(Century::new(2000)))
146                    .with_single_digit_expansion(true),
147            ),
148        }
149    }
Source

pub fn try_new(year: i32) -> Result<Self, CenturyError>

Create a new Century, returning Err if year is not divisible by 100.

Use this when the year value comes from dynamic input (e.g. user configuration, a config file). For known-valid static values, prefer Century::new.

Trait Implementations§

Source§

impl Clone for Century

Source§

fn clone(&self) -> Century

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 Century

Source§

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

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

impl From<Century> for i32

Source§

fn from(century: Century) -> i32

Converts to this type from the input type.
Source§

impl PartialEq for Century

Source§

fn eq(&self, other: &Century) -> 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<i32> for Century

Source§

type Error = CenturyError

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

fn try_from(value: i32) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for Century

Source§

impl Eq for Century

Source§

impl StructuralPartialEq for Century

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.