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
54
55
56
57
58
use crate::r#type::Type;

/// Defines a struct field.
#[derive(Debug, Clone)]
pub struct Field {
    /// Field name
    pub name: String,

    /// Field type
    pub ty: Type,

    /// Field documentation
    pub documentation: String,

    /// Field annotation
    pub annotation: Vec<String>,

    /// Field value
    pub value: String,

    /// The visibility of the field
    pub visibility: Option<String>,
}

impl Field {
    /// Return a field definition with the provided name and type
    pub fn new<T>(name: &str, ty: T) -> Self
    where
        T: Into<Type>,
    {
        Field {
            name: name.into(),
            ty: ty.into(),
            documentation: String::new(),
            annotation: Vec::new(),
            value: String::new(),
            visibility: None,
        }
    }

    /// Set field's documentation.
    pub fn doc(&mut self, documentation: impl Into<String>) -> &mut Self {
        self.documentation = documentation.into();
        self
    }

    /// Set field's annotation.
    pub fn annotation(&mut self, annotation: impl Into<String>) -> &mut Self {
        self.annotation.push(annotation.into());
        self
    }

    /// Set the visibility of the field
    pub fn vis(&mut self, visibility: impl Into<String>) -> &mut Self {
        self.visibility = Some(visibility.into());
        self
    }
}