From cdd1d4a0573b1a871985bcc0022e13eab6a690d0 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 13 Mar 2021 22:05:18 +0100 Subject: 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()`. --- src/main.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src') 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> { let mut email_input: Vec = 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> { -- cgit v1.2.3