1 mod json;
2 mod parser;
3 mod parser_dispatch;
4 #[allow(dead_code)]
5 mod parser_partial;
6
7 use winnow::error::ErrorKind;
8 use winnow::prelude::*;
9
main() -> Result<(), lexopt::Error>10 fn main() -> Result<(), lexopt::Error> {
11 let args = Args::parse()?;
12
13 let data = args.input.as_deref().unwrap_or(if args.invalid {
14 " { \"a\"\t: 42,
15 \"b\": [ \"x\", \"y\", 12 ] ,
16 \"c\": { 1\"hello\" : \"world\"
17 }
18 } "
19 } else {
20 " { \"a\"\t: 42,
21 \"b\": [ \"x\", \"y\", 12 ] ,
22 \"c\": { \"hello\" : \"world\"
23 }
24 } "
25 });
26
27 let result = match args.implementation {
28 Impl::Naive => parser::json::<ErrorKind>.parse(data),
29 Impl::Dispatch => parser_dispatch::json::<ErrorKind>.parse(data),
30 };
31 match result {
32 Ok(json) => {
33 println!("{:#?}", json);
34 }
35 Err(err) => {
36 if args.pretty {
37 println!("{}", err);
38 } else {
39 println!("{:#?}", err);
40 }
41 }
42 }
43
44 Ok(())
45 }
46
47 #[derive(Default)]
48 struct Args {
49 input: Option<String>,
50 invalid: bool,
51 pretty: bool,
52 implementation: Impl,
53 }
54
55 enum Impl {
56 Naive,
57 Dispatch,
58 }
59
60 impl Default for Impl {
default() -> Self61 fn default() -> Self {
62 Self::Naive
63 }
64 }
65
66 impl Args {
parse() -> Result<Self, lexopt::Error>67 fn parse() -> Result<Self, lexopt::Error> {
68 use lexopt::prelude::*;
69
70 let mut res = Args::default();
71
72 let mut args = lexopt::Parser::from_env();
73 while let Some(arg) = args.next()? {
74 match arg {
75 Long("invalid") => {
76 res.invalid = true;
77 }
78 Long("pretty") => {
79 // Only case where pretty matters
80 res.pretty = true;
81 res.invalid = true;
82 }
83 Long("impl") => {
84 res.implementation = args.value()?.parse_with(|s| match s {
85 "naive" => Ok(Impl::Naive),
86 "dispatch" => Ok(Impl::Dispatch),
87 _ => Err("expected `naive`, `dispatch`"),
88 })?;
89 }
90 Value(input) => {
91 res.input = Some(input.string()?);
92 }
93 _ => return Err(arg.unexpected()),
94 }
95 }
96 Ok(res)
97 }
98 }
99