Skip to content

Commit

Permalink
parsing flagfile
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhibas committed Feb 21, 2024
1 parent 951137a commit 5dea1f4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bool_expr_parser_nom"
version = "0.3.5"
version = "0.3.6"
edition = "2021"
authors = ["Nikolajus Krauklis <[email protected]>"]

Expand Down
9 changes: 6 additions & 3 deletions Flagfile.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* FF-feature-complex-ticket-234234 {
FF-feature-complex-ticket-234234 {
// this is yet another
a = b and c=d and (dd not in (1,2,3) or z == "demo car"): true

Expand All @@ -13,7 +13,7 @@
and demo == false: true

false
}*/
}

// once you dont have rules you can use short notation to return boolean
FF-feature-flat-on-off -> true
Expand All @@ -27,16 +27,19 @@ FF-feature-y {
countryCode == NL: true
false
}

// demo
FF-some-other -> true

// feature with inside commments and works like scheduler
FF-testing {
false
// testing
json({"success": true})
}

// simple flat on-off flags still can be written as a functions
FF-feature1 {
/* comment like this */
true
}

23 changes: 17 additions & 6 deletions src/parse_flagfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,26 @@ fn parse_rule_static(i: &str) -> IResult<&str, Rule> {
fn parse_rules(i: &str) -> IResult<&str, Rule> {
alt((parse_rule_expr, parse_rule_static))(i)
}

fn parse_rules_or_comments(i: &str) -> IResult<&str, Rule> {
terminated(
preceded(many0(alt((parse_comment, multiline_comment))), parse_rules),
many0(alt((parse_comment, multiline_comment))),
)(i)
}

fn parse_rules_list(i: &str) -> IResult<&str, Vec<Rule>> {
many1(parse_rules)(i)
many1(parse_rules_or_comments)(i)
}

fn parse_function(i: &str) -> IResult<&str, FlagValue> {
let func_content = delimited(ws(tag("{")), take_until("}"), ws(tag("}")));
let (i, (flag_name, func_body)) = pair(ws(parse_flag_name), func_content)(i)?;
let (_, rules) = parse_rules_list(func_body)?;
Ok((i, HashMap::from([(flag_name, rules)])))
let parser = pair(
ws(parse_flag_name),
delimited(ws(tag("{")), parse_rules_list, ws(tag("}"))),
);
map(parser, |(flag_name, rules)| {
HashMap::from([(flag_name, rules)])
})(i)
}

pub fn parse_flagfile(i: &str) -> IResult<&str, Vec<FlagValue>> {
Expand Down Expand Up @@ -125,8 +136,8 @@ mod tests {
let data = include_str!("../Flagfile.example");

let (i, v) = parse_flagfile(data).unwrap();
dbg!(i, &v);
assert_eq!(true, v.len() > 0);
// dbg!(i, v);
}
}

Expand Down

0 comments on commit 5dea1f4

Please sign in to comment.