diff options
author | Teddy Wing | 2021-03-13 22:05:18 +0100 |
---|---|---|
committer | Teddy Wing | 2021-03-13 22:05:18 +0100 |
commit | cdd1d4a0573b1a871985bcc0022e13eab6a690d0 (patch) | |
tree | 415acb8655c3c9b26526a8afd5a45ee5880c9f21 | |
parent | e99d57d6cdb8760d4351c9f0ed9f5b0285b26e37 (diff) | |
download | mutt-ottolangy-cdd1d4a0573b1a871985bcc0022e13eab6a690d0.tar.bz2 |
main(): Replace `unwrap`s with try
Move the `main()` function contents to a new `run()` function that
returns a `Result`. This way we can easily print the error in `main()`.
-rw-r--r-- | src/main.rs | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 45d4820..6f2a342 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,15 +19,23 @@ const ATTRIBUTION_EN: &'static str = fn main() { + match run() { + Ok(_) => (), + Err(e) => eprintln!("{}: error: {}", PROGRAM_NAME, e), + } +} + +fn run() -> Result<(), Box<dyn Error>> { let mut email_input: Vec<u8> = Vec::new(); let mut stdin = io::stdin(); - stdin.read_to_end(&mut email_input).unwrap(); + stdin.read_to_end(&mut email_input)?; - let body = get_email_body(&email_input).unwrap(); + let body = get_email_body(&email_input)?; print!("{}", body); - let lang_info = whatlang::detect(&body).unwrap(); + let lang_info = whatlang::detect(&body) + .ok_or("unable to detect language")?; println!("{:?}", lang_info); let attribution_config = if lang_info.lang() == Lang::Fra { @@ -36,7 +44,9 @@ fn main() { ATTRIBUTION_EN }; - write_attribution(&attribution_config).unwrap(); + write_attribution(&attribution_config)?; + + Ok(()) } fn get_email_body(email: &[u8]) -> Result<String, Box<dyn Error>> { |