php_codegen/
attribute.rs

1use crate::Generator;
2use crate::Indentation;
3
4#[derive(Debug)]
5pub struct AttributeGroup {
6    pub members: Vec<(String, Option<String>)>,
7}
8
9impl AttributeGroup {
10    pub fn new() -> Self {
11        Self { members: vec![] }
12    }
13
14    pub fn add<T: ToString>(mut self, name: T, arguments: Option<T>) -> Self {
15        self.members.push((
16            name.to_string(),
17            arguments.map(|arguments| arguments.to_string()),
18        ));
19
20        self
21    }
22}
23
24impl Generator for AttributeGroup {
25    fn generate(&self, indentation: Indentation, level: usize) -> String {
26        let mut result = String::new();
27
28        result.push_str(&indentation.indent("#[", level));
29        result.push_str(
30            &self
31                .members
32                .iter()
33                .map(|(name, arguments)| {
34                    if let Some(arguments) = arguments {
35                        format!("{}({})", name, arguments)
36                    } else {
37                        name.to_string()
38                    }
39                })
40                .collect::<Vec<String>>()
41                .join(", "),
42        );
43        result.push_str("]\n");
44
45        result
46    }
47}
48
49impl Default for AttributeGroup {
50    fn default() -> Self {
51        Self::new()
52    }
53}