whichtime_sys/parsers/de/
weekday.rs1use crate::components::Component;
10use crate::context::ParsingContext;
11use crate::dictionaries::RelativeModifier;
12use crate::dictionaries::de::{get_relative_modifier, get_weekday};
13use crate::error::Result;
14use crate::parsers::Parser;
15use crate::results::ParsedResult;
16use chrono::{Datelike, Duration};
17use fancy_regex::Regex;
18use std::sync::LazyLock;
19
20static PATTERN: LazyLock<Regex> = LazyLock::new(|| {
23 Regex::new(
24 r"(?ix)
25 (?<![a-zA-ZäöüÄÖÜß])
26 (?:am\s+)?
27 (?:
28 (?P<prefix>diese[nmrs]?|nächste[nmrs]?|naechste[nmrs]?|kommende[nmrs]?|letzte[nmrs]?|vergangene[nmrs]?|vorige[nmrs]?)
29 \s+
30 )?
31 (?P<weekday>sonntag|so|montag|mo|dienstag|di|mittwoch|mi|donnerstag|do|freitag|fr|samstag|sa)
32 (?:
33 \s+
34 (?P<suffix>diese|nächste|naechste|kommende|letzte|vergangene|vorige)
35 \s+woche
36 )?
37 (?=\W|$)
38 "
39 ).unwrap()
40});
41
42pub struct DEWeekdayParser;
44
45impl DEWeekdayParser {
46 pub fn new() -> Self {
47 Self
48 }
49}
50
51impl Default for DEWeekdayParser {
52 fn default() -> Self {
53 Self::new()
54 }
55}
56
57impl Parser for DEWeekdayParser {
58 fn name(&self) -> &'static str {
59 "DEWeekdayParser"
60 }
61
62 fn should_apply(&self, _context: &ParsingContext) -> bool {
63 true
64 }
65
66 fn parse(&self, context: &ParsingContext) -> Result<Vec<ParsedResult>> {
67 let mut results = Vec::new();
68 let ref_date = context.reference.instant;
69 let ref_weekday = ref_date.weekday().num_days_from_sunday() as i64;
70
71 let mut start = 0;
72 while start < context.text.len() {
73 let search_text = &context.text[start..];
74 let captures = match PATTERN.captures(search_text) {
75 Ok(Some(caps)) => caps,
76 Ok(None) => break,
77 Err(_) => break,
78 };
79
80 let full_match = match captures.get(0) {
81 Some(m) => m,
82 None => break,
83 };
84
85 let match_start = start + full_match.start();
86 let match_end = start + full_match.end();
87 let matched_text = full_match.as_str();
88
89 let weekday_str = captures
90 .name("weekday")
91 .map(|m| m.as_str().to_lowercase())
92 .unwrap_or_default();
93 let prefix_str = captures.name("prefix").map(|m| m.as_str().to_lowercase());
94 let suffix_str = captures.name("suffix").map(|m| m.as_str().to_lowercase());
95
96 let Some(weekday) = get_weekday(&weekday_str) else {
97 start = match_end;
98 continue;
99 };
100
101 let target_weekday = weekday as i64;
102
103 let modifier = prefix_str
105 .as_ref()
106 .and_then(|s| get_relative_modifier(s))
107 .or_else(|| suffix_str.as_ref().and_then(|s| get_relative_modifier(s)));
108
109 let days_offset = match modifier {
111 Some(RelativeModifier::Next) => {
112 let diff = target_weekday - ref_weekday;
113 if diff <= 0 { diff + 7 } else { diff }
114 }
115 Some(RelativeModifier::Last) => {
116 let diff = target_weekday - ref_weekday;
117 if diff >= 0 { diff - 7 } else { diff }
118 }
119 Some(RelativeModifier::This) | None => {
120 let diff = target_weekday - ref_weekday;
122 if diff == 0 {
123 0 } else if diff > 0 {
125 if diff <= 3 {
127 diff } else {
129 diff - 7 }
131 } else {
132 if diff >= -3 {
134 diff } else {
136 diff + 7 }
138 }
139 }
140 };
141
142 let final_offset = if suffix_str.is_some() && modifier == Some(RelativeModifier::Next) {
144 let diff = target_weekday - ref_weekday;
146 if diff > 0 {
147 diff } else {
149 diff + 7 }
151 } else {
152 days_offset
153 };
154
155 let target_date = ref_date + Duration::days(final_offset);
156
157 let mut components = context.create_components();
158 components.assign(Component::Year, target_date.year());
159 components.assign(Component::Month, target_date.month() as i32);
160 components.assign(Component::Day, target_date.day() as i32);
161 components.assign(Component::Weekday, target_weekday as i32);
162
163 let actual_start = matched_text
165 .find(|c: char| c.is_alphanumeric())
166 .unwrap_or(0);
167 let actual_end = matched_text
168 .rfind(|c: char| c.is_alphanumeric())
169 .map(|i| i + matched_text[i..].chars().next().map_or(1, char::len_utf8))
170 .unwrap_or(matched_text.len());
171
172 results.push(context.create_result(
173 match_start + actual_start,
174 match_start + actual_end,
175 components,
176 None,
177 ));
178
179 start = match_end;
180 }
181
182 Ok(results)
183 }
184}