use super::tokens::FormatToken;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SectionKind {
Positive,
Negative,
Zero,
Text,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Condition {
#[allow(dead_code)]
Conditional(String),
}
#[derive(Debug, Clone)]
pub(crate) struct FormatSection {
pub kind: SectionKind,
#[allow(dead_code)]
pub condition: Option<Condition>,
pub tokens: Vec<FormatToken>,
}
impl FormatSection {
pub fn new(kind: SectionKind) -> Self {
Self {
kind,
condition: None,
tokens: Vec::new(),
}
}
pub fn is_datetime(&self) -> bool {
self.tokens.iter().any(|t| t.is_datetime())
}
pub fn is_numeric(&self) -> bool {
self.tokens.iter().any(|t| t.is_numeric())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_section_new() {
let section = FormatSection::new(SectionKind::Positive);
assert_eq!(section.kind, SectionKind::Positive);
assert!(section.condition.is_none());
assert!(section.tokens.is_empty());
}
#[test]
fn test_is_datetime() {
let mut section = FormatSection::new(SectionKind::Positive);
section.tokens.push(FormatToken::Year(4));
section.tokens.push(FormatToken::Month(2));
section.tokens.push(FormatToken::Day(2));
assert!(section.is_datetime());
let mut section2 = FormatSection::new(SectionKind::Positive);
section2.tokens.push(FormatToken::IntegerZero(1));
assert!(!section2.is_datetime());
}
#[test]
fn test_is_numeric() {
let mut section = FormatSection::new(SectionKind::Positive);
section.tokens.push(FormatToken::IntegerZero(1));
section.tokens.push(FormatToken::DecimalPoint);
section.tokens.push(FormatToken::DecimalZero(2));
assert!(section.is_numeric());
let mut section2 = FormatSection::new(SectionKind::Positive);
section2.tokens.push(FormatToken::Year(4));
assert!(!section2.is_numeric());
}
}