summaryrefslogtreecommitdiff
path: root/pest_consume/examples/csv/main.rs
blob: 88621db5e4546171bbd9df6e9cbc53716496d5c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use pest_consume::{match_nodes, Error, Parser};

#[derive(Debug)]
enum CSVField<'a> {
    Number(f64),
    String(&'a str),
}
type CSVRecord<'a> = Vec<CSVField<'a>>;
type CSVFile<'a> = Vec<CSVRecord<'a>>;

type Result<T> = std::result::Result<T, Error<Rule>>;
type Node<'i> = pest_consume::Node<'i, Rule, ()>;

#[derive(Parser)]
#[grammar = "../examples/csv/csv.pest"]
struct CSVParser;

#[pest_consume::parser]
impl CSVParser {
    fn EOI(_input: Node) -> Result<()> {
        Ok(())
    }

    fn number(input: Node) -> Result<f64> {
        input
            .as_str()
            .parse::<f64>()
            // `input.error` links the error to the location in the input file where it occurred.
            .map_err(|e| input.error(e.to_string()))
    }

    fn string(input: Node) -> Result<&str> {
        Ok(input.as_str())
    }

    fn field(input: Node) -> Result<CSVField> {
        Ok(match_nodes!(input.children();
            [number(n)] => CSVField::Number(n),
            [string(s)] => CSVField::String(s),
        ))
    }

    fn record(input: Node) -> Result<CSVRecord> {
        Ok(match_nodes!(input.children();
            [field(fields)..] => fields.collect(),
        ))
    }

    fn file(input: Node) -> Result<CSVFile> {
        Ok(match_nodes!(input.children();
            [record(records).., EOI(_)] => records.collect(),
        ))
    }
}

fn parse_csv(input_str: &str) -> Result<CSVFile> {
    let inputs = CSVParser::parse(Rule::file, input_str)?;
    Ok(match_nodes!(<CSVParser>; inputs;
        [file(e)] => e,
    ))
}

fn main() {
    let successful_parse = parse_csv("-273.15, ' a string '\n\n42, 0");
    println!("success: {:?}", successful_parse.unwrap());

    let unsuccessful_parse = parse_csv("0, 273.15.12");
    println!("failure: {}", unsuccessful_parse.unwrap_err());
}