From: Jacob Casper Date: Sat, 25 Apr 2020 01:40:10 +0000 (-0500) Subject: Reduce error handling boilerplate X-Git-Url: https://git.jacobcasper.com/?a=commitdiff_plain;h=5f1c95cca28c538b1f8e75af157c6d096cc17945;p=sockgit.git Reduce error handling boilerplate --- diff --git a/src/main.rs b/src/main.rs index 2b11523..982c234 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,19 +4,16 @@ use std::path; use std::error; use git2; -fn check_ip() -> Result<(), io::Error> { - match env::var("REMOTE_ADDR") { - Ok(remote_ip) => match env::var("WHITELIST_IP") { - Ok(whitelist_ip) => { - if remote_ip == whitelist_ip { - Ok(()) - } else { - Err(io::Error::new(io::ErrorKind::ConnectionRefused, format!("Blocked connection from {}", remote_ip))) - } - } - Err(e) => Err(io::Error::new(io::ErrorKind::ConnectionRefused, e)), - } - Err(e) => Err(io::Error::new(io::ErrorKind::ConnectionRefused, e)), +fn check_ip() -> Result<(), Box> { + let remote_ip = env::var("REMOTE_ADDR")?; + let whitelist_ip = env::var("WHITELIST_IP")?; + if remote_ip == whitelist_ip { + Ok(()) + } else { + Err(Box::new(io::Error::new( + io::ErrorKind::ConnectionRefused, + format!("Blocked connection from {}", remote_ip), + ))) } } @@ -50,7 +47,7 @@ fn main() -> Result<(), Box> { ) )?; Ok(()) - } - Err(e) => Err(Box::new(e)), + } + Err(e) => Err(e), } }