From 0fa5b304ed57517ba04b7a42ff30258e456972f8 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 14 Mar 2021 03:42:16 +0100 Subject: Exit with sysexits.h error codes on error --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 16 +++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 047960a..f5844c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,6 +64,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "exitcode" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193" + [[package]] name = "hashbrown" version = "0.7.2" @@ -90,6 +96,7 @@ name = "ottolangy" version = "0.0.1" dependencies = [ "anyhow", + "exitcode", "mailparse", "thiserror", "whatlang", diff --git a/Cargo.toml b/Cargo.toml index 97e4592..439c05f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2018" [dependencies] anyhow = "1.0.38" +exitcode = "1.1.2" mailparse = "0.13.2" thiserror = "1.0.24" whatlang = "0.11.1" diff --git a/src/main.rs b/src/main.rs index 2775155..a50b42d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ // along with this program. If not, see . use anyhow::{anyhow, Context, Error}; +use exitcode; use mailparse; use thiserror; use whatlang::{self, Lang}; @@ -21,6 +22,7 @@ use xdg; use std::fs::File; use std::io::{self, Read, Write}; +use std::process; const PROGRAM_NAME: &'static str = "ottolangy"; @@ -59,7 +61,19 @@ enum OttolangyError { fn main() { match run() { Ok(_) => (), - Err(e) => eprintln!("{}: error: {}", PROGRAM_NAME, e), + Err(e) => { + eprintln!("{}: error: {}", PROGRAM_NAME, e); + + match e.downcast_ref::() { + Some(OttolangyError::ParseMail(_)) => + process::exit(exitcode::DATAERR), + Some(OttolangyError::ParseMailUnknown) => + process::exit(exitcode::DATAERR), + Some(OttolangyError::Xdg(_)) => process::exit(exitcode::IOERR), + Some(OttolangyError::Io(_)) => process::exit(exitcode::IOERR), + None => process::exit(exitcode::UNAVAILABLE), + } + }, } } -- cgit v1.2.3