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
impl SlidingWindowPivot
Sourcepub fn new(pivot: u8) -> Self
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?
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
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 }Sourcepub fn try_new(pivot: u8) -> Result<Self, SlidingWindowPivotError>
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
impl Clone for SlidingWindowPivot
Source§fn clone(&self) -> SlidingWindowPivot
fn clone(&self) -> SlidingWindowPivot
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more