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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use super::RpType;
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct RpField {
pub required: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub safe_ident: Option<String>,
pub ident: String,
pub comment: Vec<String>,
#[serde(rename = "type")]
pub ty: RpType,
#[serde(skip_serializing_if = "Option::is_none")]
pub field_as: Option<String>,
}
impl RpField {
pub fn new<S: AsRef<str>>(ident: S, ty: RpType) -> Self {
RpField {
required: true,
safe_ident: None,
ident: ident.as_ref().to_string(),
comment: Vec::new(),
ty: ty,
field_as: None,
}
}
pub fn is_optional(&self) -> bool {
!self.required
}
pub fn is_required(&self) -> bool {
self.required
}
pub fn safe_ident(&self) -> &str {
self.safe_ident.as_ref().unwrap_or(&self.ident)
}
pub fn with_safe_ident<S: AsRef<str>>(self, safe_ident: S) -> RpField {
Self {
safe_ident: Some(safe_ident.as_ref().to_string()),
..self
}
}
pub fn ident(&self) -> &str {
&self.ident
}
pub fn name(&self) -> &str {
self.field_as.as_ref().unwrap_or(&self.ident)
}
pub fn display(&self) -> String {
self.name().to_owned()
}
}