Skip to main content

SlidingWindowPivot

Struct SlidingWindowPivot 

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

The pivot point for a TwoDigitYearExpansion::SlidingWindow.

A valid pivot is in the range 1–99. A pivot of p means two-digit values 0..(p-1) map to the upper (more recent) part of the window, and values p..99 map to the lower (earlier) part.

§Construction

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

use partial_date::models::SlidingWindowPivot;

// Known-valid — panics if the value is invalid.
let pivot = SlidingWindowPivot::new(50);

// Dynamic input — returns Result.
let pivot = SlidingWindowPivot::try_new(50).unwrap();

// Via TryFrom.
let pivot: SlidingWindowPivot = 50_u8.try_into().unwrap();

Implementations§

Source§

impl SlidingWindowPivot

Source

pub fn new(pivot: u8) -> Self

Create a new SlidingWindowPivot.

§Panics

Panics if pivot is 0 or greater than 99. Use SlidingWindowPivot::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 664)
599fn example_22_all_component_orders() {
600    println!("2️⃣2️⃣  ALL COMPONENT ORDERS");
601    println!("   Same input '01 06 24', different interpretations based on order\n");
602
603    let input_str = "01 06 24";
604    let orders = vec![
605        (
606            "DMY",
607            ComponentOrder {
608                first: DateComponent::Day,
609                second: DateComponent::Month,
610                third: DateComponent::Year,
611            },
612        ),
613        (
614            "MDY",
615            ComponentOrder {
616                first: DateComponent::Month,
617                second: DateComponent::Day,
618                third: DateComponent::Year,
619            },
620        ),
621        (
622            "YMD",
623            ComponentOrder {
624                first: DateComponent::Year,
625                second: DateComponent::Month,
626                third: DateComponent::Day,
627            },
628        ),
629        (
630            "YDM",
631            ComponentOrder {
632                first: DateComponent::Year,
633                second: DateComponent::Day,
634                third: DateComponent::Month,
635            },
636        ),
637        (
638            "MYD",
639            ComponentOrder {
640                first: DateComponent::Month,
641                second: DateComponent::Year,
642                third: DateComponent::Day,
643            },
644        ),
645        (
646            "DYM",
647            ComponentOrder {
648                first: DateComponent::Day,
649                second: DateComponent::Year,
650                third: DateComponent::Month,
651            },
652        ),
653    ];
654
655    for (order_name, order) in orders {
656        let input = Input {
657            utterance: input_str.to_string(),
658            config: Some(Config {
659                component_order: order,
660                year: YearConfig {
661                    expected: IsExpected::Yes,
662                    two_digit_expansion: TwoDigitYearExpansion::SlidingWindow {
663                        earliest_year: 1950,
664                        pivot: SlidingWindowPivot::new(50),
665                    },
666                    ..Default::default()
667                },
668                ..Default::default()
669            }),
670        };
671
672        let result = extract(input);
673        println!(
674            "   {} → Day: {:?}, Month: {:?}, Year: {:?}",
675            order_name, result.day.value, result.month.number, result.year.value
676        );
677    }
678    println!();
679}
More examples
Hide additional examples
examples/console_entry_config.rs (line 126)
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(pivot: u8) -> Result<Self, SlidingWindowPivotError>

Create a new SlidingWindowPivot, returning Err if pivot is 0 or greater than 99.

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

Trait Implementations§

Source§

impl Clone for SlidingWindowPivot

Source§

fn clone(&self) -> SlidingWindowPivot

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 SlidingWindowPivot

Source§

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

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

impl From<SlidingWindowPivot> for u8

Source§

fn from(pivot: SlidingWindowPivot) -> u8

Converts to this type from the input type.
Source§

impl PartialEq for SlidingWindowPivot

Source§

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

Source§

type Error = SlidingWindowPivotError

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

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

Performs the conversion.
Source§

impl Copy for SlidingWindowPivot

Source§

impl Eq for SlidingWindowPivot

Source§

impl StructuralPartialEq for SlidingWindowPivot

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.