From ecd51dcb017fe995bfa057fc7095b5f478d1cf68 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sun, 30 Oct 2016 22:29:55 -0700 Subject: add metadata parsing (toml) --- src/lib.rs | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index cc1bd36..1f7ed27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,8 @@ #[macro_use] extern crate log; +extern crate toml; + pub mod modelica_parser; pub mod modelica_ast; @@ -12,7 +14,6 @@ use std::fs::File; #[derive(Debug, PartialEq)] pub struct ModelMetadata { - pub slug: String, pub name_en: String, pub description_en: Option, pub vars: Vec, @@ -23,13 +24,17 @@ pub enum ModelVarType { Independent, Dependent, Constant, + // XXX: + Time, + State, + Parameter, } #[derive(Debug, PartialEq)] pub struct ModelVar { pub slug: String, - pub name_en: String, - pub vtype: Option, + pub name_en: Option, + pub vtype: ModelVarType, pub latex: Option, pub units_si: Option, } @@ -42,12 +47,34 @@ pub struct ModelEntry { } // TODO: this -pub fn parse_metadata(s: String) -> Result { +pub fn parse_metadata(raw: String) -> Result { + let root = toml::Parser::new(&raw).parse().unwrap(); + let model = root.get("model").unwrap().as_table().unwrap(); + let variables = root.get("variables").unwrap().as_table().unwrap(); + let mut vars = vec![]; + for (slug, info) in variables { + let info = info.as_table().unwrap(); + let vtype = match info.get("type").unwrap().as_str().unwrap() { + "independent" => ModelVarType::Independent, + "dependent" => ModelVarType::Dependent, + "constant" => ModelVarType::Constant, + "time" => ModelVarType::Time, + "state" => ModelVarType::State, + "parameter" => ModelVarType::Parameter, + other => return Err(format!("Unknown variable type: {}", other)), + }; + vars.push(ModelVar { + slug: slug.to_string(), + name_en: info.get("name-en").map(|x| x.as_str().unwrap().to_string()), + vtype: vtype, + latex: info.get("latex").map(|x| x.as_str().unwrap().to_string()), + units_si: info.get("units-si").map(|x| x.as_str().unwrap().to_string()), + }); + } Ok(ModelMetadata { - slug: "dummy".to_string(), - name_en: "Bogus Dummy Model".to_string(), - description_en: None, - vars: vec![], + name_en: model.get("name-en").unwrap().as_str().unwrap().to_string(), + description_en: model.get("description-en").map(|x| x.as_str().unwrap().to_string()), + vars: vars, }) } @@ -98,7 +125,6 @@ pub fn search_models(p: &Path) -> Vec { fn test_parse_metadata() { assert_eq!(parse_metadata("asdf".to_string()).unwrap(), ModelMetadata { - slug: "dummy".to_string(), name_en: "Bogus Dummy Model".to_string(), description_en: None, vars: vec![], -- cgit v1.2.3