Switch over to Failure instead of custom errors
Getty Ritter
6 years ago
13 | 13 | serde_json = "*" |
14 | 14 | clap = "2.27.1" |
15 | 15 | rustache = "*" |
16 | failure = "*" | |
16 | 17 | |
17 | 18 | [[bin]] |
18 | 19 | name = "rr-pretty" |
1 | #[macro_use] extern crate failure; | |
2 | ||
1 | 3 | pub mod contlines; |
2 | 4 | |
3 | 5 | use contlines::ContinuationLines; |
55 | 57 | } |
56 | 58 | } |
57 | 59 | |
60 | #[derive(Debug, Fail)] | |
61 | pub enum RecError { | |
62 | #[fail(display = "Error parsing records: {}", message)] | |
63 | GenericError { | |
64 | message: String, | |
65 | }, | |
66 | ||
67 | #[fail(display = "Found cont line in nonsensical place: {}", ln)] | |
68 | BadContLine { | |
69 | ln: String, | |
70 | }, | |
71 | ||
72 | #[fail(display = "Invalid line: {}", ln)] | |
73 | InvalidLine { | |
74 | ln: String, | |
75 | }, | |
76 | } | |
77 | ||
58 | 78 | |
59 | 79 | impl Recfile { |
60 |
pub fn parse<I>(i: I) -> Result<Recfile, |
|
80 | pub fn parse<I>(i: I) -> Result<Recfile, RecError> | |
61 | 81 | where I: std::io::BufRead |
62 | 82 | { |
63 | 83 | let mut iter = ContinuationLines::new(i.lines()); |
93 | 113 | &ln[1..] |
94 | 114 | }); |
95 | 115 | } else { |
96 | return Err(format!( | |
97 | "Found continuation line in nonsensical place: {}", | |
98 |
|
|
116 | return Err(RecError::BadContLine{ ln: ln.to_owned() }); | |
99 | 117 | } |
100 | 118 | } else if let Some(pos) = ln.find(':') { |
101 | 119 | let (key, val) = ln.split_at(pos); |
106 | 124 | ctx.current_record_type = Some(val[1..].trim_left().to_owned()); |
107 | 125 | } |
108 | 126 | } else { |
109 |
return Err( |
|
127 | return Err(RecError::InvalidLine { ln: ln.to_owned() }); | |
110 | 128 | } |
111 | 129 | } |
112 | 130 |
1 | #![allow(dead_code)] | |
2 | ||
1 | 3 | use std::{fs,io}; |
2 | 4 | |
5 | /// This can be changed to modify all the tool metadata all at once | |
3 | 6 | pub const VERSION: &'static str = "0.0"; |
4 | 7 | pub const AUTHOR: &'static str = |
5 | 8 | "Getty Ritter <rrecutils@infinitenegativeutility.com>"; |
1 | 1 | extern crate clap; |
2 | 2 | extern crate rrecutils; |
3 | 3 | extern crate rustache; |
4 | #[macro_use] extern crate failure; | |
4 | 5 | |
5 | 6 | use std::{fs,io}; |
6 | use std::convert::From; | |
7 | use std::string::FromUtf8Error; | |
8 | 7 | |
9 | 8 | mod common; |
10 | 9 | |
30 | 29 | hb = hb.insert(&field.0, field.1.clone()); |
31 | 30 | } |
32 | 31 | hb.render(template, writer) |
33 | } | |
34 | } | |
35 | ||
36 | enum FormatErr { | |
37 | IOError(io::Error), | |
38 | Utf8Error(FromUtf8Error), | |
39 | Rustache(rustache::RustacheError), | |
40 | Generic(String), | |
41 | } | |
42 | ||
43 | impl From<io::Error> for FormatErr { | |
44 | fn from(err: io::Error) -> FormatErr { | |
45 | FormatErr::IOError(err) | |
46 | } | |
47 | } | |
48 | ||
49 | impl From<FromUtf8Error> for FormatErr { | |
50 | fn from(err: FromUtf8Error) -> FormatErr { | |
51 | FormatErr::Utf8Error(err) | |
52 | } | |
53 | } | |
54 | ||
55 | impl From<rustache::RustacheError> for FormatErr { | |
56 | fn from(err: rustache::RustacheError) -> FormatErr { | |
57 | FormatErr::Rustache(err) | |
58 | } | |
59 | } | |
60 | ||
61 | impl From<String> for FormatErr { | |
62 | fn from(err: String) -> FormatErr { | |
63 | FormatErr::Generic(err) | |
64 | 32 | } |
65 | 33 | } |
66 | 34 | |
105 | 73 | .get_matches() |
106 | 74 | } |
107 | 75 | |
108 |
fn run() -> Result<(), |
|
76 | fn run() -> Result<(), failure::Error> { | |
109 | 77 | let matches = rr_format_args(); |
110 | 78 | |
111 | 79 | let input = common::input_from_spec( |
120 | 88 | fs::File::open(path)?.read_to_end(&mut buf)?; |
121 | 89 | String::from_utf8(buf)? |
122 | 90 | }, |
123 |
None => |
|
91 | None => bail!("No template specified!"), | |
124 | 92 | }; |
125 | 93 | |
126 | 94 | let mut recfile = rrecutils::Recfile::parse(input)?; |
140 | 108 | output.write(j.as_bytes())?; |
141 | 109 | output.write(&['\n' as u8])?; |
142 | 110 | } |
143 |
R { rec: r }.render(&template, &mut output.as_mut()) |
|
111 | R { rec: r }.render(&template, &mut output.as_mut()) | |
112 | .map_err(|e| format_err!("Rustache error: {:?}", e))?; | |
144 | 113 | } |
145 | 114 | |
146 | 115 | Ok(()) |
147 | 116 | } |
148 | 117 | |
149 | 118 | fn main() { |
150 | use FormatErr::*; | |
151 | 119 | match run() { |
152 | 120 | Ok(()) => (), |
153 | Err(IOError(_)) => panic!("IO Error"), | |
154 | Err(Utf8Error(_)) => panic!("Cannot decode as UTF-8"), | |
155 | Err(Rustache(r)) => panic!("Rustache error: {:?}", r), | |
156 | Err(Generic(s)) => panic!("{}", s), | |
121 | Err(e) => println!("{}", e), | |
157 | 122 | } |
158 | 123 | } |
1 | 1 | extern crate clap; |
2 | 2 | extern crate rrecutils; |
3 | extern crate failure; | |
3 | 4 | |
4 | 5 | mod common; |
5 | 6 | |
6 | fn main() { | |
7 | let matches = clap::App::new("rr-sel") | |
7 | use failure::Error; | |
8 | ||
9 | fn rr_select_args() -> clap::ArgMatches<'static> { | |
10 | clap::App::new("rr-sel") | |
8 | 11 | .version(common::VERSION) |
9 | 12 | .author(common::AUTHOR) |
10 | 13 | .about("Print records from a recfile") |
14 | ||
15 | .arg(clap::Arg::with_name("input") | |
16 | .short("i") | |
17 | .long("input") | |
18 | .value_name("FILE") | |
19 | .help("The input recfile (or - for stdin)")) | |
20 | ||
21 | .arg(clap::Arg::with_name("output") | |
22 | .short("o") | |
23 | .long("output") | |
24 | .value_name("FILE") | |
25 | .help("The desired output location (or - for stdout)")) | |
11 | 26 | |
12 | 27 | .arg(clap::Arg::with_name("type") |
13 | 28 | .long("type") |
39 | 54 | .required(false) |
40 | 55 | .takes_value(true)) |
41 | 56 | |
42 |
.get_matches() |
|
57 | .get_matches() | |
58 | } | |
43 | 59 | |
44 | let source = std::io::stdin(); | |
45 | let mut records = rrecutils::Recfile::parse(source.lock()).unwrap(); | |
60 | fn run() -> Result<(), Error> { | |
61 | let matches = rr_select_args(); | |
62 | ||
63 | let input = common::input_from_spec( | |
64 | matches.value_of("input"))?; | |
65 | let mut output = common::output_from_spec( | |
66 | matches.value_of("output"))?; | |
67 | ||
68 | let mut records = rrecutils::Recfile::parse(input)?; | |
46 | 69 | |
47 | 70 | if let Some(typ) = matches.value_of("type") { |
48 | 71 | records.filter_by_type(typ); |
49 | 72 | } |
50 | 73 | |
51 |
records.write(&mut |
|
74 | records.write(&mut output)?; | |
75 | ||
76 | Ok(()) | |
52 | 77 | } |
78 | ||
79 | fn main() { | |
80 | match run() { | |
81 | Ok(()) => (), | |
82 | Err(e) => println!("{}", e), | |
83 | } | |
84 | } |