Updated argument-handling + renamed pretty to debug
Getty Ritter
6 years ago
17 | 17 | |
18 | 18 | |
19 | 19 | [[bin]] |
20 | name = "rr-pretty" | |
21 | path = "src/tools/pretty.rs" | |
20 | name = "rr-debug" | |
21 | path = "src/tools/debug.rs" | |
22 | 22 | |
23 | 23 | [[bin]] |
24 | 24 | name = "rr-to-json" |
1 | extern crate clap; | |
2 | extern crate failure; | |
3 | extern crate rrecutils; | |
4 | ||
5 | mod common; | |
6 | ||
7 | fn rr_debug_args() -> clap::ArgMatches<'static> { | |
8 | clap::App::new("rr-debug") | |
9 | .version("0.0") | |
10 | .author("Getty Ritter <rrecutils@infinitenegativeutility.com>") | |
11 | ||
12 | .arg(clap::Arg::with_name("input") | |
13 | .short("i") | |
14 | .long("input") | |
15 | .value_name("FILE") | |
16 | .help("The input recfile (or - for stdin)")) | |
17 | ||
18 | .arg(clap::Arg::with_name("output") | |
19 | .short("o") | |
20 | .long("output") | |
21 | .value_name("FILE") | |
22 | .help("The desired output location (or - for stdout)")) | |
23 | ||
24 | .arg(clap::Arg::with_name("pretty") | |
25 | .short("p") | |
26 | .long("pretty") | |
27 | .takes_value(false) | |
28 | .help("Whether to pretty-print the Rust AST")) | |
29 | ||
30 | .about("Display the Rust AST for a Recutils file") | |
31 | .get_matches() | |
32 | } | |
33 | ||
34 | fn main() { | |
35 | fn run() -> Result<(), failure::Error> { | |
36 | let matches = rr_debug_args(); | |
37 | ||
38 | let input = common::input_from_spec( | |
39 | matches.value_of("input"))?; | |
40 | let mut output = common::output_from_spec( | |
41 | matches.value_of("output"))?; | |
42 | ||
43 | let records = rrecutils::Recfile::parse(input)?; | |
44 | ||
45 | if matches.is_present("pretty") { | |
46 | writeln!(output, "{:#?}", records)?; | |
47 | } else { | |
48 | writeln!(output, "{:?}", records)?; | |
49 | } | |
50 | ||
51 | Ok(()) | |
52 | } | |
53 | ||
54 | match run() { | |
55 | Ok(()) => (), | |
56 | Err(e) => println!("{}", e), | |
57 | } | |
58 | } |
1 | extern crate clap; | |
2 | extern crate rrecutils; | |
3 | ||
4 | fn main() { | |
5 | let _matches = clap::App::new("rr-pretty") | |
6 | .version("0.0") | |
7 | .author("Getty Ritter <rrecutils@infinitenegativeutility.com>") | |
8 | .about("Display the Rust AST for a Recutils file") | |
9 | .get_matches(); | |
10 | let source = std::io::stdin(); | |
11 | let records = rrecutils::Recfile::parse(source.lock()); | |
12 | println!("{:#?}", records); | |
13 | } |
1 | 1 | extern crate clap; |
2 | extern crate failure; | |
2 | 3 | extern crate rrecutils; |
3 | 4 | extern crate serde_json; |
4 | 5 | |
5 | 6 | mod common; |
6 | ||
7 | use std::fmt; | |
8 | 7 | |
9 | 8 | use serde_json::Value; |
10 | 9 | use serde_json::map::Map; |
19 | 18 | Value::Object(m) |
20 | 19 | } |
21 | 20 | |
22 | fn unwrap_err<L, R: fmt::Debug>(value: Result<L, R>) -> L { | |
23 | match value { | |
24 | Ok(v) => v, | |
25 | Err(err) => { | |
26 | println!("{:?}", err); | |
27 | std::process::exit(99) | |
28 | } | |
29 | } | |
30 | } | |
31 | ||
32 | fn main() { | |
33 | let matches = clap::App::new("rr-to-json") | |
21 | fn rr_tojson_args() -> clap::ArgMatches<'static> { | |
22 | clap::App::new("rr-to-json") | |
34 | 23 | .version(common::VERSION) |
35 | 24 | .author(common::AUTHOR) |
36 | 25 | .about("Display the Rust AST for a Recutils file") |
38 | 27 | .short("p") |
39 | 28 | .long("pretty") |
40 | 29 | .help("Pretty-print the resulting JSON")) |
30 | ||
41 | 31 | .arg(clap::Arg::with_name("input") |
42 | 32 | .short("i") |
43 | 33 | .long("input") |
44 | 34 | .value_name("FILE") |
45 | 35 | .help("The input recfile (or - for stdin)")) |
36 | ||
46 | 37 | .arg(clap::Arg::with_name("output") |
47 | 38 | .short("o") |
48 | 39 | .long("output") |
49 | 40 | .value_name("FILE") |
50 | 41 | .help("The desired output location (or - for stdout)")) |
51 | .get_matches(); | |
52 | 42 | |
53 | let input = unwrap_err(common::input_from_spec( | |
54 | matches.value_of("input"))); | |
55 | let mut output = unwrap_err(common::output_from_spec( | |
56 | matches.value_of("output"))); | |
43 | .get_matches() | |
44 | } | |
45 | ||
46 | fn run() -> Result<(), failure::Error> { | |
47 | let matches = rr_tojson_args(); | |
48 | ||
49 | let input = common::input_from_spec( | |
50 | matches.value_of("input"))?; | |
51 | let mut output = common::output_from_spec( | |
52 | matches.value_of("output"))?; | |
57 | 53 | |
58 | 54 | let json = Value::Array( |
59 |
|
|
55 | rrecutils::Recfile::parse(input)? | |
60 | 56 | .records |
61 | 57 | .iter() |
62 | 58 | .map(|x| record_to_json(x)) |
63 | 59 | .collect()); |
64 | 60 | |
65 | 61 | let serialized = if matches.is_present("pretty") { |
66 |
|
|
62 | serde_json::to_string_pretty(&json)? | |
67 | 63 | } else { |
68 | 64 | json.to_string() |
69 | 65 | }; |
70 | 66 | |
71 |
|
|
67 | writeln!(output, "{}", serialized)?; | |
72 | 68 | |
69 | Ok(()) | |
73 | 70 | } |
71 | ||
72 | fn main() { | |
73 | match run() { | |
74 | Ok(()) => (), | |
75 | Err(e) => println!("{}", e), | |
76 | } | |
77 | } |