mithril_persistence/sqlite/
source_alias.rs

1use std::collections::{hash_map::Iter, HashMap};
2
3/// Handful tool to store SQL source aliases.
4/// ```
5/// use mithril_persistence::sqlite::SourceAlias;
6///
7/// let aliases = SourceAlias::new(&[("first", "one"), ("second", "two")]);
8/// ```
9#[derive(Debug, Default, Clone)]
10pub struct SourceAlias {
11    /// Internal HashMap of source_name => source_alias
12    aliases: HashMap<String, String>,
13}
14
15impl SourceAlias {
16    /// Create a new alias from a `&[(name, alias)]` list
17    pub fn new(aliases: &[(&str, &str)]) -> Self {
18        Self {
19            aliases: aliases
20                .iter()
21                .map(|(name, alias)| (name.to_string(), alias.to_string()))
22                .collect(),
23        }
24    }
25
26    /// get an iterator from the current alias map
27    pub fn get_iterator(&self) -> Iter<'_, String, String> {
28        self.aliases.iter()
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn simple_source_alias() {
38        let source_alias = SourceAlias::new(&[("first", "one"), ("second", "two")]);
39        let mut fields = "first.one, second.two".to_string();
40
41        for (alias, source) in source_alias.get_iterator() {
42            fields = fields.replace(alias, source);
43        }
44        assert_eq!("one.one, two.two".to_string(), fields);
45    }
46}