diff options
author | Nadrieril | 2019-09-18 22:37:30 +0200 |
---|---|---|
committer | Nadrieril | 2019-09-18 22:37:30 +0200 |
commit | bf417fadb206d6d2351a13cd7c6988977a46dd33 (patch) | |
tree | 06effc0f10795a208e929bdf9fdbbbfa6d94cc31 /pest_consume/examples/csv/main.rs | |
parent | f8341503c778db92f46fa9f6f368a2013e4c0c1a (diff) |
Extract pest_consume into its own crate
Diffstat (limited to 'pest_consume/examples/csv/main.rs')
-rw-r--r-- | pest_consume/examples/csv/main.rs | 69 |
1 files changed, 0 insertions, 69 deletions
diff --git a/pest_consume/examples/csv/main.rs b/pest_consume/examples/csv/main.rs deleted file mode 100644 index 88621db..0000000 --- a/pest_consume/examples/csv/main.rs +++ /dev/null @@ -1,69 +0,0 @@ -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()); -} |