diff options
-rw-r--r-- | src/lib.rs | 44 |
1 files changed, 35 insertions, 9 deletions
@@ -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<String>, pub vars: Vec<ModelVar>, @@ -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<ModelVarType>, + pub name_en: Option<String>, + pub vtype: ModelVarType, pub latex: Option<String>, pub units_si: Option<String>, } @@ -42,12 +47,34 @@ pub struct ModelEntry { } // TODO: this -pub fn parse_metadata(s: String) -> Result<ModelMetadata,String> { +pub fn parse_metadata(raw: String) -> Result<ModelMetadata,String> { + 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<String> { 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![], |