-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(De)serializing ParserConfig from toml
- Loading branch information
Showing
5 changed files
with
71 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,82 @@ | ||
|
||
use crate::prelude::*; | ||
|
||
use std::collections::HashMap; | ||
|
||
use ligen_ir::Literal; | ||
use ligen_ir::{Literal, Path}; | ||
|
||
#[derive(Default)] | ||
#[derive(Default, Serialize, Deserialize)] | ||
pub struct ParserConfig { | ||
map: HashMap<String, Literal> | ||
#[serde(flatten)] | ||
map: Group | ||
} | ||
|
||
impl ParserConfig { | ||
pub fn get(&self, key: &str) -> Option<&Literal> { | ||
self.map.get(key) | ||
} | ||
#[derive(Serialize, Deserialize)] | ||
#[serde(untagged)] | ||
enum Value { | ||
Literal(Literal), | ||
Group(Group) | ||
} | ||
|
||
impl<S: Into<String>, L: Into<Literal>, I: IntoIterator<Item = (S, L)>> From<I> for ParserConfig | ||
{ | ||
fn from(input: I) -> Self { | ||
let mut map = HashMap::new(); | ||
for (key, value) in input.into_iter() { | ||
map.insert(key.into(), value.into()); | ||
#[derive(Default, Serialize, Deserialize)] | ||
struct Group { | ||
#[serde(flatten)] | ||
map: HashMap<String, Value> | ||
} | ||
|
||
impl Group { | ||
fn get<P: Into<Path>>(&self, path: P) -> Option<&Literal> { | ||
let mut path = path.into(); | ||
if let Some(word) = path.pop_front() { | ||
match self | ||
.map | ||
.get(&word.identifier.name) { | ||
Some(Value::Literal(literal)) => { | ||
if path.is_empty() { | ||
Some(literal) | ||
} else { | ||
None | ||
} | ||
}, | ||
Some(Value::Group(group)) => group.get(path), | ||
None => None | ||
} | ||
} else { | ||
None | ||
} | ||
Self { map } | ||
} | ||
} | ||
|
||
impl TryFrom<&str> for ParserConfig { | ||
type Error = toml::de::Error; | ||
fn try_from(value: &str) -> std::result::Result<Self, Self::Error> { | ||
toml::from_str(value) | ||
} | ||
} | ||
|
||
impl ParserConfig { | ||
pub fn get<P: Into<Path>>(&self, path: P) -> Option<&Literal> { | ||
self.map.get(path) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use ligen_ir::Literal; | ||
|
||
use super::ParserConfig; | ||
|
||
fn config() -> ParserConfig { | ||
ParserConfig::try_from(r#" | ||
[ligen] | ||
parse-all = false | ||
default-name = "library""# | ||
).unwrap() | ||
} | ||
|
||
#[test] | ||
fn parser_config() { | ||
let config = ParserConfig::from([ | ||
("parser::config::string", Literal::from("test")), | ||
("parser::config::bool", Literal::from(true)) | ||
]); | ||
assert_eq!(config.get("parser::config::none"), None); | ||
assert_eq!(config.get("parser::config::string"), Some(&"test".into())); | ||
assert_eq!(config.get("parser::config::bool"), Some(&true.into())); | ||
let config = config(); | ||
assert_eq!(config.get("ligen::parse_all"), None); | ||
assert_eq!(config.get("ligen::parse-all"), Some(&false.into())); | ||
assert_eq!(config.get("ligen::default-name"), Some(&"library".into())); | ||
} | ||
} |