codegen/
field.rs

1use crate::r#type::Type;
2
3/// Defines a struct field.
4#[derive(Debug, Clone)]
5pub struct Field {
6    /// Field name
7    pub name: String,
8
9    /// Field type
10    pub ty: Type,
11
12    /// Field documentation
13    pub documentation: String,
14
15    /// Field annotation
16    pub annotation: Vec<String>,
17
18    /// Field value
19    pub value: String,
20
21    /// The visibility of the field
22    pub visibility: Option<String>,
23}
24
25impl Field {
26    /// Return a field definition with the provided name and type
27    pub fn new<T>(name: &str, ty: T) -> Self
28    where
29        T: Into<Type>,
30    {
31        Field {
32            name: name.into(),
33            ty: ty.into(),
34            documentation: String::new(),
35            annotation: Vec::new(),
36            value: String::new(),
37            visibility: None,
38        }
39    }
40
41    /// Set field's documentation.
42    pub fn doc(&mut self, documentation: impl Into<String>) -> &mut Self {
43        self.documentation = documentation.into();
44        self
45    }
46
47    /// Set field's annotation.
48    pub fn annotation(&mut self, annotation: impl Into<String>) -> &mut Self {
49        self.annotation.push(annotation.into());
50        self
51    }
52
53    /// Set the visibility of the field
54    pub fn vis(&mut self, visibility: impl Into<String>) -> &mut Self {
55        self.visibility = Some(visibility.into());
56        self
57    }
58}