gdritter repos rrecutils / c0b472b
Updated argument-handling + renamed pretty to debug Getty Ritter 6 years ago
4 changed file(s) with 86 addition(s) and 37 deletion(s). Collapse all Expand all
1717
1818
1919 [[bin]]
20 name = "rr-pretty"
21 path = "src/tools/pretty.rs"
20 name = "rr-debug"
21 path = "src/tools/debug.rs"
2222
2323 [[bin]]
2424 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 }
+0
-13
src/tools/pretty.rs less more
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 }
11 extern crate clap;
2 extern crate failure;
23 extern crate rrecutils;
34 extern crate serde_json;
45
56 mod common;
6
7 use std::fmt;
87
98 use serde_json::Value;
109 use serde_json::map::Map;
1918 Value::Object(m)
2019 }
2120
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")
3423 .version(common::VERSION)
3524 .author(common::AUTHOR)
3625 .about("Display the Rust AST for a Recutils file")
3827 .short("p")
3928 .long("pretty")
4029 .help("Pretty-print the resulting JSON"))
30
4131 .arg(clap::Arg::with_name("input")
4232 .short("i")
4333 .long("input")
4434 .value_name("FILE")
4535 .help("The input recfile (or - for stdin)"))
36
4637 .arg(clap::Arg::with_name("output")
4738 .short("o")
4839 .long("output")
4940 .value_name("FILE")
5041 .help("The desired output location (or - for stdout)"))
51 .get_matches();
5242
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"))?;
5753
5854 let json = Value::Array(
59 unwrap_err(rrecutils::Recfile::parse(input))
55 rrecutils::Recfile::parse(input)?
6056 .records
6157 .iter()
6258 .map(|x| record_to_json(x))
6359 .collect());
6460
6561 let serialized = if matches.is_present("pretty") {
66 unwrap_err(serde_json::to_string_pretty(&json))
62 serde_json::to_string_pretty(&json)?
6763 } else {
6864 json.to_string()
6965 };
7066
71 unwrap_err(writeln!(output, "{}", serialized));
67 writeln!(output, "{}", serialized)?;
7268
69 Ok(())
7370 }
71
72 fn main() {
73 match run() {
74 Ok(()) => (),
75 Err(e) => println!("{}", e),
76 }
77 }