gdritter repos rrecutils / 236bcc0
Start using a common helper module for all binaries This also introduces a tiny abstraction over the common "stdin or file" pattern that shows up regularly in these tools Getty Ritter 6 years ago
4 changed file(s) with 96 addition(s) and 41 deletion(s). Collapse all Expand all
1 use std::{fs,io};
2
3 pub const VERSION: &'static str = "0.0";
4 pub const AUTHOR: &'static str =
5 "Getty Ritter <rrecutils@infinitenegativeutility.com>";
6
7 pub enum Input {
8 Stdin(io::BufReader<io::Stdin>),
9 File(io::BufReader<fs::File>),
10 }
11
12 impl io::Read for Input {
13 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
14 match self {
15 &mut Input::Stdin(ref mut stdin) =>
16 stdin.read(buf),
17 &mut Input::File(ref mut file) =>
18 file.read(buf),
19 }
20 }
21 }
22
23 impl io::BufRead for Input {
24 fn fill_buf(&mut self) -> io::Result<&[u8]> {
25 match self {
26 &mut Input::Stdin(ref mut stdin) =>
27 stdin.fill_buf(),
28 &mut Input::File(ref mut file) =>
29 file.fill_buf(),
30 }
31 }
32
33 fn consume(&mut self, amt: usize) {
34 match self {
35 &mut Input::Stdin(ref mut stdin) =>
36 stdin.consume(amt),
37 &mut Input::File(ref mut file) =>
38 file.consume(amt),
39 }
40 }
41 }
42
43 pub fn input_from_spec<'a>(
44 spec: Option<&'a str>
45 ) -> io::Result<Input> {
46 match spec.unwrap_or("-") {
47 "-" => Ok(Input::Stdin(io::BufReader::new(io::stdin()))),
48 path => {
49 let f = fs::File::open(path)?;
50 Ok(Input::File(io::BufReader::new(f)))
51 }
52 }
53 }
54
55 pub fn output_from_spec<'a>(
56 spec: Option<&'a str>
57 ) -> io::Result<Box<io::Write>>
58 {
59 match spec.unwrap_or("-") {
60 "-" => Ok(Box::new(io::stdout())),
61 path => Ok(Box::new(fs::File::open(path)?)),
62 }
63 }
55 use std::{fs,io};
66 use std::convert::From;
77 use std::string::FromUtf8Error;
8
9 mod common;
810
911 use rustache::Render;
1012
6365 }
6466
6567
66 fn run() -> Result<(), FormatErr> {
67 let matches = clap::App::new("rr-format")
68 .version("0.0")
69 .author("Getty Ritter <rrecutils@infinitenegativeutility.com>")
68 fn rr_format_args() -> clap::ArgMatches<'static> {
69 clap::App::new("rr-format")
70 .version(common::VERSION)
71 .author(common::AUTHOR)
7072 .about("Display the Rust AST for a Recutils file")
7173
7274 .arg(clap::Arg::with_name("input")
100102 .value_name("STRING")
101103 .help("The string used to separate each fragment"))
102104
103 .get_matches();
105 .get_matches()
106 }
104107
105 let stdin = io::stdin();
108 fn run() -> Result<(), FormatErr> {
109 let matches = rr_format_args();
106110
107 let input: Box<io::BufRead> =
108 match matches.value_of("input").unwrap_or("-") {
109 "-" => Box::new(stdin.lock()),
110 path =>
111 Box::new(io::BufReader::new(fs::File::open(path)?)),
112 };
111 let input = common::input_from_spec(
112 matches.value_of("input"))?;
113 let mut output = common::output_from_spec(
114 matches.value_of("output"))?;
113115
114116 let template: String = match matches.value_of("mustache") {
115117 Some(path) => {
127129 recfile.filter_by_type(typ);
128130 }
129131
130 let mut output: Box<io::Write> =
131 match matches.value_of("output").unwrap_or("-") {
132 "-" => Box::new(io::stdout()),
133 path => Box::new(fs::File::open(path)?),
134 };
135132
136133 let joiner = matches.value_of("joiner");
137134
11 extern crate clap;
22 extern crate rrecutils;
33
4 mod common;
5
46 fn main() {
57 let matches = clap::App::new("rr-sel")
6 .version("0.0")
7 .author("Getty Ritter <rrecutils@infinitenegativeutility.com>")
8 .version(common::VERSION)
9 .author(common::AUTHOR)
810 .about("Print records from a recfile")
911
1012 .arg(clap::Arg::with_name("type")
22 extern crate rrecutils;
33 extern crate serde_json;
44
5 use std::{fmt,fs,io};
5 mod common;
6
7 use std::fmt;
68
79 use serde_json::Value;
810 use serde_json::map::Map;
2931
3032 fn main() {
3133 let matches = clap::App::new("rr-to-json")
32 .version("0.0")
33 .author("Getty Ritter <rrecutils@infinitenegativeutility.com>")
34 .version(common::VERSION)
35 .author(common::AUTHOR)
3436 .about("Display the Rust AST for a Recutils file")
3537 .arg(clap::Arg::with_name("pretty")
3638 .short("p")
4850 .help("The desired output location (or - for stdout)"))
4951 .get_matches();
5052
51 let stdin = io::stdin();
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")));
5257
53 let input: Box<io::BufRead> =
54 match matches.value_of("input").unwrap_or("-") {
55 "-" => Box::new(stdin.lock()),
56 path =>
57 Box::new(io::BufReader::new(unwrap_err(fs::File::open(path)))),
58 };
59
60 let json = Value::Array(unwrap_err(rrecutils::Recfile::parse(input))
61 .records
62 .iter()
63 .map(|x| record_to_json(x))
64 .collect());
65
66 let mut output: Box<io::Write> =
67 match matches.value_of("output").unwrap_or("-") {
68 "-" => Box::new(io::stdout()),
69 path => Box::new(unwrap_err(fs::File::open(path))),
70 };
58 let json = Value::Array(
59 unwrap_err(rrecutils::Recfile::parse(input))
60 .records
61 .iter()
62 .map(|x| record_to_json(x))
63 .collect());
7164
7265 let serialized = if matches.is_present("pretty") {
7366 unwrap_err(serde_json::to_string_pretty(&json))