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 super::Style;
use std::fmt;

const SPACE_SEPRATOR: &str = " ";
const COMMA_SEPRATOR: &str = ",";

//
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct StyleList<'a> {
    pub styles: Option<Vec<Style>>,
    pub separator: Option<&'a str>,
}

impl<'a> fmt::Display for StyleList<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut v = Vec::new();
        if let Some(styles) = &self.styles {
            for style in styles {
                v.push(style.to_string());
            }
        }
        let s = v.join(self.separator.unwrap_or(COMMA_SEPRATOR));
        write!(f, "{}", s)
    }
}

impl<'a> StyleList<'a> {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn add(&mut self, style: Style) -> &mut Self {
        self.styles.get_or_insert(Vec::new()).push(style);
        self
    }

    pub fn space_separator(&mut self) -> &mut Self {
        self.separator = Some(SPACE_SEPRATOR);
        self
    }

    pub fn comma_separator(&mut self) -> &mut Self {
        self.separator = Some(COMMA_SEPRATOR);
        self
    }
}