Debian Codegraph @

src/config/child_properties.rs

raw
1 use log::error;
2 use toml::Value;
3
4 use super::{error::TomlHelper, parsable::Parse};
5
6 #[derive(Clone, Debug)]
7 pub struct ChildProperty {
8 pub name: String,
9 pub rename_getter: Option<String>,
10 pub type_name: String,
11 pub doc_hidden: bool,
12 pub generate_doc: bool,
13 }
14
15 impl Parse for ChildProperty {
16 fn parse(toml: &Value, object_name: &str) -> Option<Self> {
17 let name = toml
18 .lookup("name")
19 .and_then(Value::as_str)
20 .map(ToOwned::to_owned);
21 let name = if let Some(name) = name {
22 name
23 } else {
24 error!("No child property name for `{object_name}`");
25 return None;
26 };
27
28 toml.check_unwanted(
29 &["name", "type", "doc_hidden", "rename_getter"],
30 &format!("child property {object_name}"),
31 );
32
33 let type_name = toml
34 .lookup("type")
35 .and_then(Value::as_str)
36 .map(ToOwned::to_owned);
37 let type_name = if let Some(type_name) = type_name {
38 type_name
39 } else {
40 error!("No type for child property `{name}` for `{object_name}`");
41 return None;
42 };
43 let doc_hidden = toml
44 .lookup("doc_hidden")
45 .and_then(Value::as_bool)
46 .unwrap_or(false);
47 let rename_getter = toml
48 .lookup("rename_getter")
49 .and_then(Value::as_str)
50 .map(ToOwned::to_owned);
51 let generate_doc = toml
52 .lookup("generate_doc")
53 .and_then(Value::as_bool)
54 .unwrap_or(true);
55
56 Some(Self {
57 name,
58 rename_getter,
59 type_name,
60 doc_hidden,
61 generate_doc,
62 })
63 }
64 }
65
66 #[derive(Clone, Debug)]
67 pub struct ChildProperties {
68 pub child_name: Option<String>,
69 pub child_type: Option<String>,
70 pub properties: Vec<ChildProperty>,
71 }
72
73 impl Parse for ChildProperties {
74 fn parse(toml_object: &Value, object_name: &str) -> Option<Self> {
75 let child_name = toml_object
76 .lookup("child_name")
77 .and_then(Value::as_str)
78 .map(ToOwned::to_owned);
79 let child_type = toml_object
80 .lookup("child_type")
81 .and_then(Value::as_str)
82 .map(ToOwned::to_owned);
83 let mut properties: Vec<ChildProperty> = Vec::new();
84 if let Some(configs) = toml_object.lookup("child_prop").and_then(Value::as_array) {
85 for config in configs {
86 if let Some(item) = ChildProperty::parse(config, object_name) {
87 properties.push(item);
88 }
89 }
90 }
91
92 if !properties.is_empty() {
93 Some(Self {
94 child_name,
95 child_type,
96 properties,
97 })
98 } else {
99 if child_name.is_some() {
100 error!("`{object_name}` has child_name but no child_prop's");
101 }
102 if child_type.is_some() {
103 error!("`{object_name}` has child_type but no child_prop's");
104 }
105 None
106 }
107 }
108 }
109
110 #[cfg(test)]
111 mod tests {
112 use super::{super::parsable::Parse, *};
113
114 fn toml(input: &str) -> ::toml::Value {
115 let value = ::toml::from_str(input);
116 assert!(value.is_ok());
117 value.unwrap()
118 }
119
120 #[test]
121 fn child_property_parse() {
122 let toml = toml(
123 r#"
124 name = "prop"
125 type = "prop_type"
126 "#,
127 );
128 let child = ChildProperty::parse(&toml, "a").unwrap();
129 assert_eq!("prop", child.name);
130 assert_eq!("prop_type", child.type_name);
131 }
132
133 #[test]
134 fn child_property_parse_generate_doc() {
135 let r = toml(
136 r#"
137 name = "prop"
138 type = "prop_type"
139 generate_doc = false
140 "#,
141 );
142 let child = ChildProperty::parse(&r, "a").unwrap();
143 assert!(!child.generate_doc);
144
145 // Ensure that the default value is "true".
146 let r = toml(
147 r#"
148 name = "prop"
149 type = "prop_type"
150 "#,
151 );
152 let child = ChildProperty::parse(&r, "a").unwrap();
153 assert!(child.generate_doc);
154 }
155
156 #[test]
157 fn child_property_parse_not_all() {
158 let tml = toml(
159 r#"
160 name = "prop"
161 "#,
162 );
163 assert!(ChildProperty::parse(&tml, "a").is_none());
164
165 let tml = toml(
166 r#"
167 type_name = "prop_type"
168 "#,
169 );
170 assert!(ChildProperty::parse(&tml, "a").is_none());
171 }
172
173 #[test]
174 fn child_properties_parse() {
175 let toml = toml(
176 r#"
177 child_name = "child_name"
178 child_type = "child_type"
179 [[child_prop]]
180 name = "prop"
181 type = "prop_type"
182 [[child_prop]]
183 name = "prop2"
184 type = "prop_type2"
185 "#,
186 );
187 let props = ChildProperties::parse(&toml, "a").unwrap();
188 assert_eq!(Some("child_name".into()), props.child_name);
189 assert_eq!(Some("child_type".into()), props.child_type);
190 assert_eq!(2, props.properties.len());
191 assert_eq!("prop", props.properties[0].name);
192 assert_eq!("prop_type", props.properties[0].type_name);
193 assert_eq!("prop2", props.properties[1].name);
194 assert_eq!("prop_type2", props.properties[1].type_name);
195 }
196
197 #[test]
198 fn child_property_no_parse_without_children() {
199 let toml = toml(
200 r#"
201 child_name = "child_name"
202 child_type = "child_type"
203 "#,
204 );
205 let props = ChildProperties::parse(&toml, "a");
206 assert!(props.is_none());
207 }
208
209 #[test]
210 fn child_properties_parse_without_child_type_name() {
211 let toml = toml(
212 r#"
213 [[child_prop]]
214 name = "prop"
215 type = "prop_type"
216 "#,
217 );
218 let props = ChildProperties::parse(&toml, "a").unwrap();
219 assert_eq!(None, props.child_name);
220 assert_eq!(None, props.child_type);
221 assert_eq!(1, props.properties.len());
222 }
223
224 #[test]
225 fn child_properties_parse_without_child_type() {
226 let toml = toml(
227 r#"
228 child_name = "child_name"
229 [[child_prop]]
230 name = "prop"
231 type = "prop_type"
232 "#,
233 );
234 let props = ChildProperties::parse(&toml, "a").unwrap();
235 assert_eq!(Some("child_name".into()), props.child_name);
236 assert_eq!(None, props.child_type);
237 assert_eq!(1, props.properties.len());
238 }
239
240 #[test]
241 fn child_properties_parse_without_child_name() {
242 let toml = toml(
243 r#"
244 child_type = "child_type"
245 [[child_prop]]
246 name = "prop"
247 type = "prop_type"
248 "#,
249 );
250 let props = ChildProperties::parse(&toml, "a").unwrap();
251 assert_eq!(None, props.child_name);
252 assert_eq!(Some("child_type".into()), props.child_type);
253 assert_eq!(1, props.properties.len());
254 }
255 }