use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use crate::calendar::Level;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Part {
Lit(String),
Yyyy,
Yy,
YyNext,
Q,
Pp,
Ww,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Template {
pub source: String,
pub parts: Vec<Part>,
}
impl Template {
pub fn compile(source: &str, level: Level) -> Result<Template, String> {
let mut parts = Vec::new();
let mut lit = String::new();
let mut rest = source;
while let Some(c) = rest.chars().next() {
match c {
'{' => {
let close = rest
.find('}')
.ok_or_else(|| String::from("unbalanced brace: '{' is never closed"))?;
let name = &rest[1..close];
if name.contains('{') {
return Err(String::from("unbalanced brace: '{' inside a placeholder"));
}
let part = match name {
"yyyy" => Part::Yyyy,
"yy" => Part::Yy,
"yy_next" => Part::YyNext,
"q" => Part::Q,
"pp" => Part::Pp,
"ww" => Part::Ww,
_ => return Err(format!("unknown placeholder {{{name}}}")),
};
let allowed = match part {
Part::Q => level == Level::Quarter,
Part::Pp => level == Level::Period,
Part::Ww => level == Level::Week,
_ => true,
};
if !allowed {
return Err(format!(
"placeholder {{{name}}} is not allowed in a {} template",
level.name()
));
}
if !lit.is_empty() {
parts.push(Part::Lit(core::mem::take(&mut lit)));
}
parts.push(part);
rest = &rest[close + 1..];
}
'}' => return Err(String::from("unbalanced brace: '}' without '{'")),
_ => {
lit.push(c);
rest = &rest[c.len_utf8()..];
}
}
}
if !lit.is_empty() {
parts.push(Part::Lit(lit));
}
if !parts.iter().any(|p| matches!(p, Part::Yyyy | Part::Yy)) {
return Err(String::from(
"the template has no year placeholder: it needs {yyyy} or {yy}, and {yy_next} alone is not enough",
));
}
let index = match level {
Level::Year => None,
Level::Quarter => Some((Part::Q, "{q}")),
Level::Period => Some((Part::Pp, "{pp}")),
Level::Week => Some((Part::Ww, "{ww}")),
};
if let Some((part, name)) = index {
if !parts.contains(&part) {
return Err(format!(
"a {} template must contain {name} so that its labels identify the {}",
level.name(),
level.name()
));
}
}
Ok(Template {
source: String::from(source),
parts,
})
}
pub fn needs_pivot(&self) -> bool {
!self.parts.contains(&Part::Yyyy) && self.parts.contains(&Part::Yy)
}
pub fn render(&self, year_number: i64, index: Option<u32>) -> Option<String> {
let mut out = String::new();
for part in &self.parts {
match part {
Part::Lit(s) => out.push_str(s),
Part::Yyyy => {
if !(1..=9999).contains(&year_number) {
return None;
}
out.push_str(&format!("{year_number:04}"));
}
Part::Yy => out.push_str(&format!("{:02}", year_number.rem_euclid(100))),
Part::YyNext => out.push_str(&format!("{:02}", (year_number + 1).rem_euclid(100))),
Part::Q => out.push_str(&format!("{}", index.unwrap_or(0))),
Part::Pp | Part::Ww => out.push_str(&format!("{:02}", index.unwrap_or(0))),
}
}
Some(out)
}
pub fn captures(&self, label: &str) -> Vec<Captures> {
let mut found = Vec::new();
match_parts(
&self.parts,
label.as_bytes(),
Captures::default(),
&mut found,
);
found
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Captures {
pub yyyy: Option<u32>,
pub yy: Option<u32>,
pub yy_next: Option<u32>,
pub index: Option<u32>,
}
fn set(slot: &mut Option<u32>, value: u32) -> bool {
match *slot {
Some(v) => v == value,
None => {
*slot = Some(value);
true
}
}
}
fn digits(s: &[u8], n: usize) -> Option<u32> {
if s.len() < n {
return None;
}
s[..n].iter().try_fold(0u32, |acc, &c| {
c.is_ascii_digit().then(|| acc * 10 + u32::from(c - b'0'))
})
}
fn match_parts(parts: &[Part], s: &[u8], caps: Captures, found: &mut Vec<Captures>) {
let Some((first, rest)) = parts.split_first() else {
if s.is_empty() {
found.push(caps);
}
return;
};
let widths: &[usize] = match first {
Part::Lit(lit) => {
if s.starts_with(lit.as_bytes()) {
match_parts(rest, &s[lit.len()..], caps, found);
}
return;
}
Part::Yyyy => &[4],
Part::Q => &[1, 2],
_ => &[2],
};
for &n in widths {
let Some(value) = digits(s, n) else { continue };
if matches!(first, Part::Q) && s[0] == b'0' {
continue;
}
let mut next = caps;
let ok = match first {
Part::Yyyy => set(&mut next.yyyy, value),
Part::Yy => set(&mut next.yy, value),
Part::YyNext => set(&mut next.yy_next, value),
_ => set(&mut next.index, value),
};
if ok {
match_parts(rest, &s[n..], next, found);
}
}
}