firo_parser/
lib.rs

1use anyhow::Result;
2use pest::Parser;
3use pest_derive::Parser;
4use std::collections::HashSet;
5
6#[derive(Parser)]
7#[grammar = "firo.pest"]
8pub struct FiroParser;
9
10pub fn parse_origin(content: String) -> Result<HashSet<String>> {
11    let file = FiroParser::parse(Rule::origin_file, content.as_str())?
12        .next()
13        .expect("This unwrap never fails!");
14    let mut hs = HashSet::new();
15    for path in file.into_inner() {
16        match path.as_rule() {
17            Rule::path_part => _ = hs.insert(path.as_str().to_string()),
18            Rule::EOI => (),
19            _ => unreachable!(),
20        }
21    }
22    Ok(hs)
23}
24
25pub fn parse_destination(content: String) -> Result<Vec<Vec<String>>> {
26    let file = FiroParser::parse(Rule::destination_file, content.as_str())?
27        .next()
28        .expect("Never Fails");
29    let mut destination = Vec::new();
30    for line in file.into_inner() {
31        let mut path = Vec::new();
32        match line.as_rule() {
33            Rule::path => {
34                for token in line.into_inner() {
35                    match token.as_rule() {
36                        Rule::path_part | Rule::pin => path.push(token.as_str().to_string()),
37                        _ => unreachable!("No rules besides path_part & pin at this level."),
38                    }
39                }
40                destination.push(path);
41            }
42            Rule::EOI => (),
43            _ => unreachable!("No rules to match besides EOI & path"),
44        }
45    }
46    Ok(destination)
47}