From 5f1c95cca28c538b1f8e75af157c6d096cc17945 Mon Sep 17 00:00:00 2001 From: Jacob Casper Date: Fri, 24 Apr 2020 20:40:10 -0500 Subject: [PATCH] Reduce error handling boilerplate --- src/main.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) 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), } } -- 2.20.1