1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::collections::{hash_map::Iter, HashMap};

/// Handful tool to store SQL source aliases.
/// ```
/// use mithril_persistence::sqlite::SourceAlias;
///
/// let aliases = SourceAlias::new(&[("first", "one"), ("second", "two")]);
/// ```
#[derive(Debug, Default, Clone)]
pub struct SourceAlias {
    /// Internal HashMap of source_name => source_alias
    aliases: HashMap<String, String>,
}

impl SourceAlias {
    /// Create a new alias from a `&[(name, alias)]` list
    pub fn new(aliases: &[(&str, &str)]) -> Self {
        Self {
            aliases: aliases
                .iter()
                .map(|(name, alias)| (name.to_string(), alias.to_string()))
                .collect(),
        }
    }

    /// get an iterator from the current alias map
    pub fn get_iterator(&self) -> Iter<'_, String, String> {
        self.aliases.iter()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn simple_source_alias() {
        let source_alias = SourceAlias::new(&[("first", "one"), ("second", "two")]);
        let mut fields = "first.one, second.two".to_string();

        for (alias, source) in source_alias.get_iterator() {
            fields = fields.replace(alias, source);
        }
        assert_eq!("one.one, two.two".to_string(), fields);
    }
}