Skip to content

Commit

Permalink
Merge pull request #17 from THCLab/feat/meta
Browse files Browse the repository at this point in the history
Feat: add meta attributes
  • Loading branch information
olichwiruk authored Nov 10, 2023
2 parents ee0c152 + 0e6fabf commit 0d78473
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
4 changes: 3 additions & 1 deletion oca-ast/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use indexmap::IndexMap;
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use strum_macros::Display;
use std::str::FromStr;
use std::{str::FromStr, collections::HashMap};
use wasm_bindgen::prelude::*;


Expand All @@ -10,6 +10,7 @@ pub struct OCAAst {
pub version: String,
pub commands: Vec<Command>,
pub commands_meta: IndexMap<usize, CommandMeta>,
pub meta: HashMap<String, String>
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -281,6 +282,7 @@ impl OCAAst {
version: String::from("1.0.0"),
commands: Vec::new(),
commands_meta: IndexMap::new(),
meta: HashMap::new(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions oca-bundle/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ mod tests {
version: "1.0".to_string(),
commands,
commands_meta: IndexMap::new(),
meta: HashMap::new(),
};

let build_result = from_ast(None, oca_ast);
Expand Down
1 change: 1 addition & 0 deletions oca-dag/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ mod tests {
version: "0.1.0".to_string(),
commands,
commands_meta: IndexMap::new(),
meta: HashMap::new(),
};
let oca_build = oca_bundle::build::from_ast(None, ast).unwrap();

Expand Down
8 changes: 7 additions & 1 deletion oca-file/src/ocafile.pest
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
// insignificant whitespace, not repeated
ws = _{ " " | "\t" }

meta_attr_key = ${ "name" | "version" }
meta_attr_value = ${ string | char+}
meta_key_pair = @{ meta_attr_key ~ "=" ~ meta_attr_value }
meta_comment = @{ "--" ~ ws* ~ (!NEWLINE ~ meta_key_pair)* }
meta_comment_line = _{ ws* ~ meta_comment ~ NEWLINE? }

comment = @{ "#" ~ (!NEWLINE ~ ANY)* }
comment_line = _{ ws* ~ comment ~ NEWLINE? }
empty_line = @{ ws* ~ NEWLINE }
Expand Down Expand Up @@ -191,6 +197,6 @@ lang = ${ ASCII_ALPHA{2} ~ ("-" ~ ASCII_ALPHA{2})? }

file = {
SOI ~
(empty_line | comment_line | commands)*
(empty_line | meta_comment_line | comment_line | commands)*
~ EOI
}
44 changes: 44 additions & 0 deletions oca-file/src/ocafile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ pub fn parse_from_string(unparsed_file: String) -> Result<OCAAst, ParseError> {
if let Rule::comment = line.as_rule() {
continue;
}
if let Rule::meta_comment = line.as_rule() {
let mut key = "".to_string();
let mut value = "".to_string();
for attr in line.into_inner() {
match attr.as_rule() {
Rule::meta_attr_key => {
key = attr.as_str().to_string();
}
Rule::meta_attr_value => {
value = attr.as_str().to_string();
}
_ => {
return Err(ParseError::Custom(format!("Error parsing meta: {}", attr.as_str())));
}
}
}
if key == "" {
return Err(ParseError::Custom(format!("Error parsing meta: key is empty")));
}
if value == "" {
return Err(ParseError::Custom(format!("Error parsing meta: value is empty")));
}
oca_ast.meta.insert(key, value);
continue;
}
if let Rule::empty_line = line.as_rule() {
continue;
}
Expand All @@ -103,3 +128,22 @@ pub fn parse_from_string(unparsed_file: String) -> Result<OCAAst, ParseError> {
}
Ok(oca_ast)
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn parse_meta_from_string_valid() {
let unparsed_file = r#"
-- version=0.0.1
-- name=Objekt
ADD attribute name=Text age=Numeric
"#;

let oca_ast = parse_from_string(unparsed_file.to_string()).unwrap();
assert_eq!(oca_ast.meta.get("version").unwrap(), "0.0.1");
assert_eq!(oca_ast.meta.get("name").unwrap(), "Objekt");
}
}

0 comments on commit 0d78473

Please sign in to comment.