From 85a914f52e435d4e7b5c1b879407e92b0d485d3d Mon Sep 17 00:00:00 2001 From: rfuzzo Date: Sat, 9 Dec 2023 15:06:21 +0100 Subject: [PATCH] start note parsing --- src/lib.rs | 47 +++++++++++++++++++++-------------- tests/cmop/rules_note.txt | 23 +++++++++++++++++ tests/cmop/rules_warnings.txt | 3 --- tests/integration_tests.rs | 13 ++++++++-- 4 files changed, 62 insertions(+), 24 deletions(-) create mode 100644 tests/cmop/rules_note.txt delete mode 100644 tests/cmop/rules_warnings.txt diff --git a/src/lib.rs b/src/lib.rs index 50e7b28..8944b5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,39 +114,41 @@ where if parsing && line.is_empty() { parsing = false; if let Some(rule) = current_rule.take() { + // Order rule is handled separately if let RuleKind::Order(_o) = rule { orders.push(current_order.to_owned()); + current_order.clear(); } else { rules.push(rule); } } else { - // todo error + // error and abort + panic!("Parsing error: unknown empty new line"); } - current_order.clear(); - continue; } // HANDLE RULE START // start order parsing + let mut r_line = line; if !parsing { - if line == "[Order]" { - parsing = true; + if r_line.starts_with("[Order") { current_rule = Some(RuleKind::Order(Order::default())); - continue; - } else if line == "[Note]" { - parsing = true; + r_line = r_line["[Order".len()..].to_owned(); + } else if r_line.starts_with("[Note") { current_rule = Some(RuleKind::Note(Note::default())); - continue; - } else if line == "[Conflict]" { - parsing = true; + r_line = r_line["[Note".len()..].to_owned(); + } else if r_line.starts_with("[Conflict") { current_rule = Some(RuleKind::Conflict(Conflict::default())); - continue; - } else if line == "[Requires]" { - parsing = true; + r_line = r_line["[Conflict".len()..].to_owned(); + } else if r_line.starts_with("[Requires") { current_rule = Some(RuleKind::Requires(Requires::default())); - continue; + r_line = r_line["[Requires".len()..].to_owned(); + } else { + // unknown rule + panic!("Parsing error: unknown rule"); } + parsing = true; } // HANDLE RULE PARSE @@ -156,15 +158,22 @@ where match current_rule { RuleKind::Order(_o) => { // order is just a list of names - current_order.push(line) + // TODO in-line names? + current_order.push(r_line) } RuleKind::Note(_n) => { // parse rule - // first line is the comment + // Syntax: [Note optional-message] expr-1 expr-2 ... expr-N + // TODO alternative: + // [Note] + // message + // A.esp - // subsequent line is archive name + // subsequent lines are archive names - // handle more lines + // parse expressions + + todo!() } RuleKind::Conflict(_c) => { todo!() diff --git a/tests/cmop/rules_note.txt b/tests/cmop/rules_note.txt new file mode 100644 index 0000000..72d0721 --- /dev/null +++ b/tests/cmop/rules_note.txt @@ -0,0 +1,23 @@ +[Note] + message +a + +[Note message] +a +mod with spaces.archive +c + +[Note message] a b c + +[Note message] a "mod with spaces.archive" "c" + +[Note message] +[ALL A.archive [NOT X.archive]] +[ANY B.archive 7.archive] +[ALL C.archive elephant.archive potato.archive] + +[Note] + message +[ALL A.archive [NOT X.archive]] +[ANY B.archive 7.archive] +[ALL C.archive elephant.archive potato.archive] \ No newline at end of file diff --git a/tests/cmop/rules_warnings.txt b/tests/cmop/rules_warnings.txt deleted file mode 100644 index 8751ab4..0000000 --- a/tests/cmop/rules_warnings.txt +++ /dev/null @@ -1,3 +0,0 @@ -[Note] - message -a \ No newline at end of file diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index b826441..14c8804 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -10,8 +10,17 @@ mod unit_tests { #[test] fn test_parse_rules() { - let rules_path = "./tests/cmop/rules_order.txt"; - assert!(parse_rules(rules_path).is_ok(), "rules parsing failed") + assert!( + parse_rules("./tests/cmop/rules_order.txt").is_ok(), + "rules parsing failed" + ); + + assert!( + parse_rules("./tests/cmop/rules_note.txt").is_ok(), + "rules parsing failed" + ); + + // TODO add other rules } #[test]