#[cfg(test)]
mod tests;
#[derive(Debug, Clone, PartialEq)]
pub struct Preserver<'a> {
lookup: &'a str,
inner: Option<Box<Preserver<'a>>>,
}
impl<'a> Preserver<'a> {
pub fn new(lookup: &'a str) -> Self {
Self { lookup, inner: None }
}
pub fn add_inners(&mut self, lookups: &[&'a str]) {
let mut current = self;
for lookup in lookups {
current.inner = Some(Box::new(Self::new(lookup)));
current = current.inner.as_mut().expect("Inner is Some due to the previous line; qed");
}
}
pub fn lookup(&self) -> &str {
self.lookup
}
pub fn get_inner(&self) -> Option<&Preserver> {
self.inner.as_deref()
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct DelimitersCount {
counts: [u8; 6],
}
impl DelimitersCount {
pub(crate) fn new() -> Self {
Self { counts: [0; 6] }
}
pub(crate) fn is_complete(&self) -> bool {
self.counts[0] == self.counts[1] && self.counts[2] == self.counts[3] && self.counts[4] == self.counts[5] }
pub(crate) fn count(&mut self, line: &str) {
self.counts[0] += line.matches('{').count() as u8;
self.counts[1] += line.matches('}').count() as u8;
self.counts[2] += line.matches('(').count() as u8;
self.counts[3] += line.matches(')').count() as u8;
self.counts[4] += line.matches('[').count() as u8;
self.counts[5] += line.matches(']').count() as u8;
}
}