ligen_parser/parser/config/
mod.rs

1mod group;
2pub use group::*;
3
4use crate::prelude::*;
5
6use ligen_ir::{Literal, Path};
7
8#[derive(Debug, Serialize, Deserialize, Clone)]
9pub struct ParserConfig {
10    #[serde(flatten)]
11    map: Group
12}
13
14impl Default for ParserConfig {
15    fn default() -> Self {
16        let map = Default::default();
17        let mut config = Self { map };
18        config.set_only_parse_symbols(false);
19        config
20    }
21}
22
23impl ParserConfig {
24    pub fn iter(&self) -> impl Iterator<Item = (Path, Literal)> {
25        self.map.iter()
26    }
27
28    /// Sets whether to parse all symbols or only the ones that are explicitly marked as such.
29    pub fn set_only_parse_symbols(&mut self, value: bool) {
30        self.set("ligen::only-parse-symbols", value);
31    }    
32
33    /// Whether to parse all symbols or only the ones that are explicitly marked as such.
34    pub fn get_only_parse_symbols(&self) -> bool {
35        self.get("ligen::only-parse-symbols")
36            .and_then(|literal| literal.as_boolean())
37            .cloned()
38            .unwrap_or(false)
39    }
40}
41
42pub trait ParserConfigGet {
43    /// Gets the value at the given path.
44    fn get<P: Into<Path>>(&self, path: P) -> Option<&Literal>;
45}
46
47pub trait ParserConfigSet {    
48    /// Sets the value at the given path.
49    fn set<P: Into<Path>, L: Into<Literal>>(&mut self, path: P, value: L);    
50}
51
52impl ParserConfig {
53    /// Creates a new parser config.
54    pub fn new() -> Self {
55        Default::default()
56    }
57}
58
59impl ParserConfigGet for ParserConfig {
60    /// Gets the value at the given path.
61    fn get<P: Into<Path>>(&self, path: P) -> Option<&Literal> {
62        self.map.get(path)
63    }
64}
65
66impl ParserConfigGet for &ParserConfig {
67    /// Gets the value at the given path.
68    fn get<P: Into<Path>>(&self, path: P) -> Option<&Literal> {
69        self.map.get(path)
70    }
71}
72
73impl ParserConfigGet for &mut ParserConfig {
74    /// Gets the value at the given path.
75    fn get<P: Into<Path>>(&self, path: P) -> Option<&Literal> {
76        self.map.get(path)
77    }
78}
79
80impl ParserConfigSet for ParserConfig {
81    /// Sets the value at the given path.
82    fn set<P: Into<Path>, L: Into<Literal>>(&mut self, path: P, value: L) {
83        self.map.set(path, value);
84    }
85}
86
87impl ParserConfigSet for &mut ParserConfig {
88    /// Sets the value at the given path.
89    fn set<P: Into<Path>, L: Into<Literal>>(&mut self, path: P, value: L) {
90        self.map.set(path, value);
91    }
92}
93
94impl TryFrom<&str> for ParserConfig {
95    type Error = toml::de::Error;
96    fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
97        toml::from_str(value)
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    fn config() -> ParserConfig {
106        let mut config = ParserConfig::try_from(r#"
107            [ligen]
108            parse-all = false"#
109        ).unwrap();
110        config.set("ligen::default-name", "library");
111        config
112    }
113
114    #[test]
115    fn parser_config() {
116        let config = config();
117        assert_eq!(config.get("ligen::parse_all"), None);
118        assert_eq!(config.get("ligen::parse-all"), Some(&false.into()));
119        assert_eq!(config.get("ligen::default-name"), Some(&"library".into()));
120
121        assert_eq!(config.get(["ligen", "parse_all"].as_slice()), None);
122        assert_eq!(config.get(["ligen", "parse-all"].as_slice()), Some(&false.into()));
123        assert_eq!(config.get(["ligen", "default-name"].as_slice()), Some(&"library".into()));
124    }
125}