| 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 | |
unwrap_err(rrecutils::Recfile::parse(input))
|
| 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 | |
unwrap_err(serde_json::to_string_pretty(&json))
|
| 62 |
serde_json::to_string_pretty(&json)?
|
| 67 | 63 |
} else {
|
| 68 | 64 |
json.to_string()
|
| 69 | 65 |
};
|
| 70 | 66 |
|
| 71 | |
unwrap_err(writeln!(output, "{}", serialized));
|
| 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 |
}
|