Skip to main content

lunar_lib/formatter/
format_table.rs

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