summaryrefslogtreecommitdiffstats
path: root/crates/rebel-parse/examples/parse-string.rs
blob: ba6a3785646558d32a684e320f1a25dcfde87f4f (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
use std::{fmt::Debug, time::Instant};

use clap::{Parser, ValueEnum};

use rebel_parse::recipe;

#[derive(Clone, Debug, ValueEnum)]
enum Rule {
	Recipe,
	RecipeStmt,
	Body,
	BodyStmt,
	Expr,
}

#[derive(Clone, Debug, Parser)]
struct Opts {
	rule: Rule,
	input: String,
}

fn main() {
	let opts: Opts = Opts::parse();
	let input = opts.input.trim();

	fn as_debug<'a>(v: impl Debug + 'a) -> Box<dyn Debug + 'a> {
		Box::new(v)
	}

	let start = Instant::now();
	let result = match opts.rule {
		Rule::Recipe => recipe::recipe(input).map(as_debug),
		Rule::RecipeStmt => recipe::recipe_stmt(input).map(as_debug),
		Rule::Body => recipe::body(input).map(as_debug),
		Rule::BodyStmt => recipe::body_stmt(input).map(as_debug),
		Rule::Expr => recipe::expr(input).map(as_debug),
	};
	let dur = Instant::now().duration_since(start);

	match result {
		Ok(value) => {
			println!("{value:#?}");
		}
		Err(err) => {
			println!("{err}");
		}
	};
	println!("Took {} us", dur.as_micros());
}