summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorRyan Geary <rtgnj42@gmail.com>2021-07-21 16:30:37 -0400
committerRyan Geary <rtgnj42@gmail.com>2021-07-23 16:27:02 -0400
commit1970d0cc74bf25afdb71f433b0392208098f4607 (patch)
tree6b167f58430a3feacab6eb86ce10402b76350367 /src/main.rs
parent03e5ca4c1d235e79f497fe91af2b33c0be013781 (diff)
Improve error handling38-unwrap-on-empty-choice
Create choose::Error and choose::Result Improve error propogation Improve error printing Add tests for negative indices Add get_negative_start_end tests Fixup negative choice indexing Remove most uses of unwrap Fixes #38
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 1d00960..9b511a7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,16 +8,20 @@ extern crate lazy_static;
mod choice;
mod config;
-mod errors;
+mod error;
mod escape;
mod opt;
mod parse;
mod parse_error;
mod reader;
+mod result;
mod writeable;
mod writer;
+
use config::Config;
+use error::Error;
use opt::Opt;
+use result::Result;
use writer::WriteReceiver;
fn main() {
@@ -32,18 +36,23 @@ fn main() {
match exit_result {
Ok(_) => (),
- Err(e) => {
- if e.kind() == io::ErrorKind::BrokenPipe {
- // BrokenPipe means whoever is reading the output hung up, we should
- // gracefully exit
- } else {
- eprintln!("Failed to write to output: {}", e)
+ Err(err) => {
+ match err {
+ Error::Io(e) => {
+ if e.kind() == io::ErrorKind::BrokenPipe {
+ // BrokenPipe means whoever is reading the output hung up, we should
+ // gracefully exit
+ } else {
+ eprintln!("Failed to write to output: {}", e)
+ }
+ }
+ e @ _ => eprintln!("Error: {}", e),
}
}
}
}
-fn main_generic<W: WriteReceiver>(opt: Opt, handle: &mut W) -> io::Result<()> {
+fn main_generic<W: WriteReceiver>(opt: Opt, handle: &mut W) -> Result<()> {
let config = Config::new(opt);
let read = match &config.opt.input {
@@ -65,7 +74,7 @@ fn main_generic<W: WriteReceiver>(opt: Opt, handle: &mut W) -> io::Result<()> {
match line {
Ok(l) => {
let l = if config.opt.character_wise || config.opt.field_separator.is_some() {
- &l[0..l.len() - 1]
+ &l[0..l.len().saturating_sub(1)]
} else {
&l
};