libgir/config/
derives.rs

1use log::error;
2use toml::Value;
3
4use super::{error::TomlHelper, parsable::Parse};
5
6#[derive(Clone, Debug)]
7pub struct Derive {
8    pub names: Vec<String>,
9    pub cfg_condition: Option<String>,
10}
11
12impl Parse for Derive {
13    fn parse(toml: &Value, object_name: &str) -> Option<Self> {
14        let names = match toml.lookup("name").and_then(Value::as_str) {
15            Some(names) => names,
16            None => {
17                error!("No 'name' given for derive for object {}", object_name);
18                return None;
19            }
20        };
21        toml.check_unwanted(&["name", "cfg_condition"], &format!("derive {object_name}"));
22
23        let cfg_condition = toml
24            .lookup("cfg_condition")
25            .and_then(Value::as_str)
26            .map(ToOwned::to_owned);
27
28        let mut names_vec = Vec::new();
29        for name in names.split(',') {
30            names_vec.push(name.trim().into());
31        }
32
33        Some(Self {
34            names: names_vec,
35            cfg_condition,
36        })
37    }
38}
39
40pub type Derives = Vec<Derive>;