use crate::components::FastComponents;
use crate::dictionaries::Locale;
use crate::results::{ParsedResult, ReferenceWithTimezone};
use crate::scanner::TokenScanner;
pub struct ParsingContext<'a> {
pub text: &'a str,
lower_text: String,
pub reference: &'a ReferenceWithTimezone,
pub tokens: Vec<crate::scanner::Token>,
pub locale: Locale,
}
impl<'a> ParsingContext<'a> {
pub fn new(text: &'a str, reference: &'a ReferenceWithTimezone) -> Self {
Self::with_locale(text, reference, Locale::En)
}
pub fn with_locale(
text: &'a str,
reference: &'a ReferenceWithTimezone,
locale: Locale,
) -> Self {
let lower_text = text.to_lowercase();
let tokens = TokenScanner::scan_locale(&lower_text, locale);
Self {
text,
lower_text,
reference,
tokens,
locale,
}
}
#[inline]
pub fn lower_text(&self) -> &str {
&self.lower_text
}
#[inline]
pub fn create_components(&self) -> FastComponents {
FastComponents::with_defaults(self.reference)
}
pub fn create_result(
&self,
index: usize,
end_index: usize,
start: FastComponents,
end: Option<FastComponents>,
) -> ParsedResult {
let text = &self.text[index..end_index];
ParsedResult::new(self.reference, index, text, start, end)
}
pub fn has_token_type(&self, token_type: crate::scanner::TokenType) -> bool {
self.tokens.iter().any(|t| t.token_type == token_type)
}
}