1use crate::components::FastComponents;
4use crate::dictionaries::Locale;
5use crate::results::{ParsedResult, ReferenceWithTimezone};
6use crate::scanner::TokenScanner;
7
8pub struct ParsingContext<'a> {
13 pub text: &'a str,
15 lower_text: String,
17 pub reference: &'a ReferenceWithTimezone,
19 pub tokens: Vec<crate::scanner::Token>,
21 pub locale: Locale,
23}
24
25impl<'a> ParsingContext<'a> {
26 pub fn new(text: &'a str, reference: &'a ReferenceWithTimezone) -> Self {
28 Self::with_locale(text, reference, Locale::En)
29 }
30
31 pub fn with_locale(
33 text: &'a str,
34 reference: &'a ReferenceWithTimezone,
35 locale: Locale,
36 ) -> Self {
37 let lower_text = text.to_lowercase();
38 let tokens = TokenScanner::scan_locale(&lower_text, locale);
39
40 Self {
41 text,
42 lower_text,
43 reference,
44 tokens,
45 locale,
46 }
47 }
48
49 #[inline]
51 pub fn lower_text(&self) -> &str {
52 &self.lower_text
53 }
54
55 #[inline]
57 pub fn create_components(&self) -> FastComponents {
58 FastComponents::with_defaults(self.reference)
59 }
60
61 pub fn create_result(
63 &self,
64 index: usize,
65 end_index: usize,
66 start: FastComponents,
67 end: Option<FastComponents>,
68 ) -> ParsedResult {
69 let text = &self.text[index..end_index];
70 ParsedResult::new(self.reference, index, text, start, end)
71 }
72
73 pub fn has_token_type(&self, token_type: crate::scanner::TokenType) -> bool {
75 self.tokens.iter().any(|t| t.token_type == token_type)
76 }
77}