code_gen/rust/common/
with_attributes.rs

1use crate::CodeBuffer;
2
3/// An element with attributes.
4pub trait WithAttributes: Sized {
5    /// Gets the attributes.
6    fn attributes(&self) -> &[String];
7
8    /// Adds the attribute.
9    fn add_attribute<S>(&mut self, attribute: S)
10    where
11        S: Into<String>;
12
13    /// Adds the attribute.
14    fn with_attribute<S>(mut self, attribute: S) -> Self
15    where
16        S: Into<String>,
17    {
18        self.add_attribute(attribute);
19        self
20    }
21
22    /// Writes the attributes.
23    fn write_attributes(&self, b: &mut CodeBuffer, level: usize) {
24        for attribute in self.attributes() {
25            b.indent(level);
26            b.write("#[");
27            b.write(attribute);
28            b.write("]");
29            b.end_line();
30        }
31    }
32}