Skip to main content

lunar_lib/formatter/
format_table.rs

1use std::collections::HashMap;
2
3use crate::formatter::Taggable;
4
5/// The format table for `key=value` pairs used when rendering `Arguments`, like in `format()`
6#[derive(Debug, Clone, PartialEq, Eq, Default)]
7pub struct FormatTable {
8    table: HashMap<String, String>,
9}
10
11impl FormatTable {
12    /// Creates a new, empty format table
13    pub fn new() -> Self {
14        Self {
15            table: HashMap::new(),
16        }
17    }
18
19    /// Adds a single `key=value` pair to the table
20    pub fn add_entry(&mut self, key: impl Into<String>, value: impl Into<String>) {
21        self.table
22            .insert(key.into().to_ascii_lowercase(), value.into());
23    }
24
25    /// Extends the table with an iterator of `key=value` pairs
26    pub fn add_table(&mut self, table: impl IntoIterator<Item = (String, String)>) {
27        self.table.extend(table);
28    }
29
30    /// Extends the table with any [`Taggable`]
31    pub fn extend<T: Taggable>(&mut self, from: &T) {
32        from.fill_table(self);
33    }
34
35    /// Gets the the held table of `self`
36    pub fn table(&self) -> &HashMap<String, String> {
37        &self.table
38    }
39}
40
41impl Taggable for FormatTable {
42    fn fill_table(&self, table: &mut FormatTable) {
43        table.table.extend(self.table.clone());
44    }
45}