Skip to content

Commit

Permalink
feat: Parse tagline (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf authored Jan 18, 2024
1 parent 620ece6 commit f010495
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 0 deletions.
212 changes: 212 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pest = "2.7.6"
pest_derive = "2.7.6"
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// parsers contains the relevant grammar parsers
pub mod parsers;
83 changes: 83 additions & 0 deletions src/parsers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use pest_derive::Parser;

#[derive(Parser)]
#[grammar = "tagline.pest"]
/// TaglineParser is responsible for parsing the taglines at the start of each searched file.
pub struct TaglineParser;

#[cfg(test)]
mod tests {
use pest::Parser;

use super::*;

#[test]
fn test_tagline_parser() {
struct TestCase<'a> {
name: &'a str,
input: &'a str,
expected_tags: Vec<&'a str>,
expected_error: bool,
}

let test_cases = [
TestCase {
name: "success_with_space",
input: "tags: [#1 #2 #3]",
expected_tags: vec!["#1", "#2", "#3"],
expected_error: false,
},
TestCase {
name: "success_without_space",
input: "tags:[#1#asdf#something-idk]",
expected_tags: vec!["#1", "#asdf", "#something-idk"],
expected_error: false,
},
TestCase {
name: "success_with_newline",
input: "tags:\n[\n\t#something_else\n]",
expected_tags: vec!["#something_else"],
expected_error: false,
},
TestCase {
name: "fail_no_brackets",
input: "tags:#1#2#3",
expected_tags: vec![],
expected_error: true,
},
TestCase {
name: "fail_no_tags",
input: "[#1#2#3]",
expected_tags: vec![],
expected_error: true,
},
TestCase {
name: "fail_wrong_tag",
input: "tags:[##]",
expected_tags: vec![],
expected_error: true,
},
];

test_cases.iter().for_each(|test_case| {
println!("Testing {}", test_case.name);

let res = TaglineParser::parse(Rule::tagline, test_case.input);
if res.is_err() {
assert!(test_case.expected_error);
return;
}

assert!(!test_case.expected_error);

for (i, tag) in res.unwrap().next().unwrap().into_inner().enumerate() {
match tag.as_rule() {
Rule::tag => {
assert!(tag.as_str() == test_case.expected_tags[i])
}
_ => unreachable!(),
}
}
})
}
}
6 changes: 6 additions & 0 deletions tagline.pest
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tag = {"#" ~ (LETTER|NUMBER|CONNECTOR_PUNCTUATION|DASH_PUNCTUATION)+}
taglist = _{"[" ~ tag* ~ "]"}
tagline = _{"tags:" ~ taglist}

WHITESPACE = _{" " | "\t" | NEWLINE}

0 comments on commit f010495

Please sign in to comment.