use std::sync::OnceLock;
use regex::Regex;
use crate::detail::token::{is_free_token, is_numeric_token, Token};
use crate::element::{Element, ElementKind};
fn pattern() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| {
crate::detail::regex_util::compile(r"^[0-9]{3,4}(?:[ipP]|[xX\u{00D7}][0-9]{3,4}[ipP]?)$")
})
}
pub(super) fn parse_video_resolution(tokens: &mut [Token]) -> Vec<Element> {
let mut elements = Vec::new();
for token in tokens
.iter_mut()
.filter(|t| is_free_token(t) && pattern().is_match(&t.value))
{
token.element_kind = Some(ElementKind::VideoResolution);
elements.push(Element {
kind: ElementKind::VideoResolution,
value: token.value.clone(),
position: token.position,
});
}
if elements.is_empty() {
if let Some(token) = tokens.iter_mut().find(|t| {
is_free_token(t) && is_numeric_token(t) && matches!(t.value.as_str(), "1080" | "720")
}) {
token.element_kind = Some(ElementKind::VideoResolution);
elements.push(Element {
kind: ElementKind::VideoResolution,
value: token.value.clone(),
position: token.position,
});
}
}
elements
}