lunar-lib 0.9.0

Common utilities for lunar applications
Documentation
use std::fmt::{self, Write};

use crate::formatter::{
    FormatTable, Render,
    template::{TemplateItem, write_escaped},
};

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(super) struct Tag<'a> {
    pub args: Vec<TemplateItem<'a>>,
}

impl Render for Tag<'_> {
    fn render(&self, format_table: &FormatTable) -> String {
        let key = self.args.render(format_table);
        format_table.table().get(&key).cloned().unwrap_or_default()
    }
}

impl fmt::Display for Tag<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_char('$')?;
        for arg in &self.args {
            match arg {
                TemplateItem::Text(s) => write_escaped(f, s, true)?,
                _ => arg.fmt(f)?,
            }
        }
        Ok(())
    }
}