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
use crate::Generator;
use crate::Indentation;

#[derive(Debug)]
pub struct AttributeGroup {
    pub members: Vec<(String, Option<String>)>,
}

impl AttributeGroup {
    pub fn new() -> Self {
        Self { members: vec![] }
    }

    pub fn add<T: ToString>(mut self, name: T, arguments: Option<T>) -> Self {
        self.members.push((
            name.to_string(),
            arguments.map(|arguments| arguments.to_string()),
        ));

        self
    }
}

impl Generator for AttributeGroup {
    fn generate(&self, indentation: Indentation, level: usize) -> String {
        let mut result = String::new();

        result.push_str(&indentation.indent("#[", level));
        result.push_str(
            &self
                .members
                .iter()
                .map(|(name, arguments)| {
                    if let Some(arguments) = arguments {
                        format!("{}({})", name, arguments)
                    } else {
                        name.to_string()
                    }
                })
                .collect::<Vec<String>>()
                .join(", "),
        );
        result.push_str("]\n");

        result
    }
}

impl Default for AttributeGroup {
    fn default() -> Self {
        Self::new()
    }
}