rust_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: Vec<String>,
14
15 /// Field annotation
16 pub annotation: Vec<String>,
17}
18
19impl Field {
20 /// Return a field definition with the provided name and type.
21 ///
22 /// # Arguments
23 ///
24 /// * `name` - The name of the field.
25 /// * `ty` - The type of the field.
26 ///
27 /// # Examples
28 ///
29 /// ```
30 /// use rust_codegen::Field;
31 ///
32 /// let count_field = Field::new("count", "i32");
33 /// ```
34 pub fn new<T>(name: &str, ty: T) -> Self
35 where
36 T: Into<Type>,
37 {
38 Field {
39 name: name.into(),
40 ty: ty.into(),
41 documentation: Vec::new(),
42 annotation: Vec::new(),
43 }
44 }
45
46 /// Set the field's documentation.
47 ///
48 /// # Arguments
49 ///
50 /// * `documentation` - The documentation to set for the field.
51 ///
52 /// # Examples
53 ///
54 /// ```
55 /// use rust_codegen::Field;
56 ///
57 /// let count_field = Field::new("count", "i32").doc(Vec::from(["The number of Foos"]));
58 pub fn doc(&mut self, documentation: Vec<&str>) -> &mut Self {
59 self.documentation = documentation.iter().map(|doc| doc.to_string()).collect();
60 self
61 }
62
63 /// Set the field's annotation.
64 ///
65 /// # Arguments
66 ///
67 /// * `annotation` - The annotation to set for the field.
68 ///
69 /// # Examples
70 ///
71 /// ```
72 /// use rust_codegen::Field;
73 ///
74 /// let count_field = Field::new("count", "i32").annotation(Vec::from(["serde(rename = \"name\")"]));
75 pub fn annotation(&mut self, annotation: Vec<&str>) -> &mut Self {
76 self.annotation = annotation.iter().map(|ann| ann.to_string()).collect();
77 self
78 }
79}