|
| 1 | +extern crate easy_error; |
1 | 2 | extern crate clap; |
2 | 3 | extern crate pad; |
3 | 4 |
|
4 | 5 | mod config; |
| 6 | +use config::Config; |
5 | 7 | use pad::PadStr; |
6 | | -use std::io::{self, Result, BufRead, BufReader}; |
7 | | -use std::fs::{self, File}; |
| 8 | +use easy_error::{ ResultExt, termination, ensure }; |
| 9 | +use std::io::{ self, Result, BufRead, BufReader }; |
| 10 | +use std::fs::{ self, File }; |
8 | 11 | use std::cmp::max; |
9 | | -use config::Config; |
10 | 12 |
|
11 | 13 | fn read_lines(file: &Option<String>) -> Result<Vec<String>> { |
12 | 14 | match file { |
@@ -85,22 +87,23 @@ fn format_pretty(data: &Vec<Vec<String>>) -> String { |
85 | 87 | ].join("\n") |
86 | 88 | } |
87 | 89 |
|
88 | | -fn main() { |
| 90 | +fn main() -> termination::Result { |
89 | 91 | let config = Config::from_args(); |
90 | | - let lines = read_lines(&config.file).expect("Error when reading input"); |
| 92 | + let lines = read_lines(&config.file).context("Error when reading input")?; |
91 | 93 | let data = parse_table_data(&lines, &config.separator); |
92 | 94 |
|
93 | | - if data.len() < 2 || data[0].len() == 0 { |
94 | | - eprintln!("Bad Input: Requires at least 2 rows (including header) and 1 column."); |
95 | | - std::process::exit(1); |
96 | | - } |
| 95 | + ensure!( |
| 96 | + data.len() > 1 && !data[0].is_empty(), |
| 97 | + "Input requires at least 2 rows and 1 column." |
| 98 | + ); |
97 | 99 |
|
98 | 100 | let table = match config.minimize { |
99 | 101 | true => format_minimized(&data), |
100 | 102 | false => format_pretty(&data), |
101 | 103 | } + "\n"; |
102 | 104 | match config.out { |
103 | | - Some(f) => fs::write(f, &table).expect("Error when writing output to file"), |
104 | | - None => print!("{}", &table), |
| 105 | + Some(f) => fs::write(f, table).context("Error when writing output")?, |
| 106 | + None => print!("{}", table), |
105 | 107 | }; |
| 108 | + Ok(()) |
106 | 109 | } |
0 commit comments