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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use super::sort_by_key;
use crate::parser::Node;
use std::collections::HashMap;
use std::string::ToString;

#[derive(Clone, Debug, Default)]
pub struct Attributes(HashMap<String, String>);

pub trait Merge<Other> {
    fn concat(self, other: &Other) -> Self;
    fn merge(&mut self, other: &Other);
}

impl Attributes {
    pub fn new() -> Self {
        Self(HashMap::new())
    }

    pub fn inner(&self) -> &HashMap<String, String> {
        &self.0
    }

    pub fn concat_iter<K, V, I>(self, items: I) -> Self
    where
        K: ToString,
        V: ToString,
        I: Iterator<Item = (K, V)>,
    {
        items.fold(self, |res, (key, value)| res.add(key, value))
    }

    pub fn merge_iter<K, V, I>(&mut self, items: I)
    where
        K: ToString,
        V: ToString,
        I: Iterator<Item = (K, V)>,
    {
        for (key, value) in items {
            self.set(key, value);
        }
    }

    pub fn has<K: ToString>(&self, key: K) -> bool {
        self.0.contains_key(&key.to_string())
    }

    pub fn get<K: ToString>(&self, key: K) -> Option<&String> {
        self.0.get(&key.to_string())
    }

    pub fn set<K: ToString, V: ToString>(&mut self, key: K, value: V) {
        self.0.insert(key.to_string(), value.to_string());
    }

    pub fn add<K: ToString, V: ToString>(mut self, key: K, value: V) -> Self {
        self.set(key, value);
        self
    }

    pub fn maybe_set<K: ToString, V: ToString>(&mut self, key: K, value: Option<V>) {
        if let Some(content) = value {
            self.set(key, content);
        }
    }

    pub fn maybe_add<K: ToString, V: ToString>(mut self, key: K, value: Option<V>) -> Self {
        self.maybe_set(key, value);
        self
    }

    pub fn entries(&self) -> Vec<(&String, &String)> {
        self.0.iter().collect()
    }
}

impl Merge<Attributes> for Attributes {
    fn concat(self, other: &Attributes) -> Self {
        self.concat_iter(other.0.iter())
    }

    fn merge(&mut self, other: &Attributes) {
        self.merge_iter(other.0.iter())
    }
}

impl<'a> Merge<Node<'a>> for Attributes {
    fn concat(self, other: &Node<'a>) -> Self {
        self.concat_iter(
            other
                .attributes
                .iter()
                .map(|(key, value)| (key.as_str(), value.as_str())),
        )
    }

    fn merge(&mut self, other: &Node<'a>) {
        self.merge_iter(
            other
                .attributes
                .iter()
                .map(|(key, value)| (key.as_str(), value.as_str())),
        )
    }
}

impl From<&Attributes> for Attributes {
    fn from(value: &Attributes) -> Self {
        Self(value.0.clone())
    }
}

impl<'a> From<&Node<'a>> for Attributes {
    fn from(node: &Node) -> Self {
        Self::new().concat(node)
    }
}

impl ToString for Attributes {
    fn to_string(&self) -> String {
        let mut entries = self.entries();
        entries.sort_by(sort_by_key);
        entries
            .iter()
            .map(|v| format!("{}=\"{}\"", v.0, v.1))
            .collect::<Vec<String>>()
            .join(" ")
    }
}

pub fn suffix_unit(input: Option<&String>, suffix: &str) -> Option<String> {
    input.map(|v| format!("{}{}", v, suffix))
}

pub fn suffix_css_classes(input: Option<&String>, suffix: &str) -> Option<String> {
    if let Some(value) = input {
        let value: Vec<String> = value
            .split(' ')
            .filter(|v| !v.is_empty())
            .map(|v| format!("{}-{}", v, suffix))
            .collect();
        if value.is_empty() {
            None
        } else {
            Some(value.join(" "))
        }
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn suffix_css_classes_none() {
        assert_eq!(suffix_css_classes(None, "whatever"), None);
    }

    #[test]
    fn suffix_css_classes_some_empty() {
        assert_eq!(suffix_css_classes(Some(&"".into()), "whatever"), None);
    }

    #[test]
    fn suffix_css_classes_with_values() {
        assert_eq!(
            suffix_css_classes(Some(&"toto tutu".into()), "whatever"),
            Some("toto-whatever tutu-whatever".into())
        );
    }
}