libgir/config/
parsable.rs

1use toml::Value;
2
3pub trait Parse: Sized {
4    fn parse(toml: &Value, name: &str) -> Option<Self>;
5}
6
7pub trait Parsable {
8    type Item;
9
10    fn parse(toml: Option<&Value>, object_name: &str) -> Vec<Self::Item>;
11}
12
13impl<T: Parse> Parsable for Vec<T> {
14    type Item = T;
15
16    fn parse(toml: Option<&Value>, object_name: &str) -> Vec<Self::Item> {
17        let mut v = Vec::new();
18        if let Some(configs) = toml.and_then(Value::as_array) {
19            for config in configs {
20                if let Some(item) = T::parse(config, object_name) {
21                    v.push(item);
22                }
23            }
24        }
25
26        v
27    }
28}