gdritter repos rrecutils / 618659a
Improve argument-handling and error-handling for rr-format Getty Ritter 6 years ago
2 changed file(s) with 37 addition(s) and 8 deletion(s). Collapse all Expand all
1 the book "{{Title}}" by {{Author}} is a {{%rec}}
1 the book "{{Title}}"{{#Author}} (by {{Author}}){{/Author}} is a {{%rec}}
6868 .version("0.0")
6969 .author("Getty Ritter <rrecutils@infinitenegativeutility.com>")
7070 .about("Display the Rust AST for a Recutils file")
71
7172 .arg(clap::Arg::with_name("input")
7273 .short("i")
7374 .long("input")
7475 .value_name("FILE")
7576 .help("The input recfile (or - for stdin)"))
77
7678 .arg(clap::Arg::with_name("output")
7779 .short("o")
7880 .long("output")
7981 .value_name("FILE")
8082 .help("The desired output location (or - for stdout)"))
81 .arg(clap::Arg::with_name("template")
83
84 .arg(clap::Arg::with_name("mustache")
85 .short("m")
86 .long("mustache")
87 .value_name("FILE")
88 .help("The mustache template to use"))
89
90 .arg(clap::Arg::with_name("type")
8291 .short("t")
83 .long("template")
84 .value_name("FILE")
85 .help("The template to use"))
92 .long("type")
93 .value_name("TYPE")
94 .takes_value(true)
95 .help("The type of records to pass to the mustache file"))
96
8697 .arg(clap::Arg::with_name("joiner")
8798 .short("j")
8899 .long("joiner")
89100 .value_name("STRING")
90101 .help("The string used to separate each fragment"))
102
91103 .get_matches();
92104
93105 let stdin = io::stdin();
99111 Box::new(io::BufReader::new(fs::File::open(path)?)),
100112 };
101113
102 let template: String = match matches.value_of("template") {
114 let template: String = match matches.value_of("mustache") {
103115 Some(path) => {
104116 use io::Read;
105117 let mut buf = Vec::new();
109121 None => Err(format!("No template specified!"))?,
110122 };
111123
112 let recfile = rrecutils::Recfile::parse(input)?;
124 let mut recfile = rrecutils::Recfile::parse(input)?;
125
126 if let Some(typ) = matches.value_of("type") {
127 recfile.filter_by_type(typ);
128 }
113129
114130 let mut output: Box<io::Write> =
115131 match matches.value_of("output").unwrap_or("-") {
117133 path => Box::new(fs::File::open(path)?),
118134 };
119135
136 let joiner = matches.value_of("joiner");
137
138 let mut first = true;
120139 for r in recfile.records.into_iter() {
140 if first {
141 first = false;
142 } else if let Some(j) = joiner {
143 output.write(j.as_bytes())?;
144 output.write(&['\n' as u8])?;
145 }
121146 R { rec: r }.render(&template, &mut output.as_mut())?;
122147 }
123148
125150 }
126151
127152 fn main() {
153 use FormatErr::*;
128154 match run() {
129155 Ok(()) => (),
130 Err(err) => panic!(err),
156 Err(IOError(_)) => panic!("IO Error"),
157 Err(Utf8Error(_)) => panic!("Cannot decode as UTF-8"),
158 Err(Rustache(r)) => panic!("Rustache error: {:?}", r),
159 Err(Generic(s)) => panic!("{}", s),
131160 }
132161 }