libgir/config/
constants.rs

1use log::error;
2use toml::Value;
3
4use super::{error::TomlHelper, gobjects::GStatus, ident::Ident, parsable::Parse};
5use crate::version::Version;
6
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct Constant {
9    pub ident: Ident,
10    pub status: GStatus,
11    pub version: Option<Version>,
12    pub cfg_condition: Option<String>,
13    pub generate_doc: bool,
14}
15
16impl Parse for Constant {
17    fn parse(toml: &Value, object_name: &str) -> Option<Self> {
18        let Some(ident) = Ident::parse(toml, object_name, "constant") else {
19            error!(
20                "No 'name' or 'pattern' given for constant for object {}",
21                object_name
22            );
23            return None;
24        };
25        toml.check_unwanted(
26            &[
27                "ignore",
28                "manual",
29                "name",
30                "version",
31                "cfg_condition",
32                "pattern",
33                "generate_doc",
34            ],
35            &format!("function {object_name}"),
36        );
37
38        let version = toml
39            .lookup("version")
40            .and_then(Value::as_str)
41            .and_then(|s| s.parse().ok());
42        let cfg_condition = toml
43            .lookup("cfg_condition")
44            .and_then(Value::as_str)
45            .map(ToOwned::to_owned);
46
47        let status = {
48            if toml
49                .lookup("ignore")
50                .and_then(Value::as_bool)
51                .unwrap_or(false)
52            {
53                GStatus::Ignore
54            } else if toml
55                .lookup("manual")
56                .and_then(Value::as_bool)
57                .unwrap_or(false)
58            {
59                GStatus::Manual
60            } else {
61                GStatus::Generate
62            }
63        };
64        let generate_doc = toml
65            .lookup("generate_doc")
66            .and_then(Value::as_bool)
67            .unwrap_or(true);
68
69        Some(Self {
70            ident,
71            status,
72            version,
73            cfg_condition,
74            generate_doc,
75        })
76    }
77}
78
79impl AsRef<Ident> for Constant {
80    fn as_ref(&self) -> &Ident {
81        &self.ident
82    }
83}
84
85pub type Constants = Vec<Constant>;
86
87#[cfg(test)]
88mod tests {
89    use super::{super::parsable::Parse, *};
90
91    fn toml(input: &str) -> ::toml::Value {
92        let value = ::toml::from_str(input);
93        assert!(value.is_ok());
94        value.unwrap()
95    }
96
97    #[test]
98    fn child_property_parse_generate_doc() {
99        let r = toml(
100            r#"
101name = "prop"
102generate_doc = false
103"#,
104        );
105        let constant = Constant::parse(&r, "a").unwrap();
106        assert!(!constant.generate_doc);
107
108        // Ensure that the default value is "true".
109        let r = toml(
110            r#"
111name = "prop"
112"#,
113        );
114        let constant = Constant::parse(&r, "a").unwrap();
115        assert!(constant.generate_doc);
116    }
117}