From bcce055d4c7d256ab534d3a13bc1f82005da5b80 Mon Sep 17 00:00:00 2001 From: Breval Ferrari Date: Thu, 6 Mar 2025 12:55:59 -0500 Subject: [PATCH] cleanup generic open function --- bingus/src/lib.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/bingus/src/lib.rs b/bingus/src/lib.rs index 56b043d..d8d0a47 100644 --- a/bingus/src/lib.rs +++ b/bingus/src/lib.rs @@ -40,25 +40,27 @@ pub mod dynamic { pub fn open>(path: P) -> Result, OpenError> { use MatcherType::*; - if let Some(matcher) = infer::get_from_path(&path)?.map(|t| t.matcher_type()) { - Ok(Some(match matcher { - Image => DynamicBendable::Image(img::open(path)?), - App | Archive => DynamicBendable::Binary({ - let mut buf = Vec::new(); - File::open(path)?.read_to_end(&mut buf)?; - buf - }), - Audio => todo!(), - Book => todo!(), - Doc => todo!(), - Font => todo!(), - Text => todo!(), - Video => todo!(), - Custom => unimplemented!("I don't even know what this is!"), - })) - } else { - Ok(None) - } + infer::get_from_path(&path)? + .map(|t| t.matcher_type()) + .map(|matcher| -> Result, OpenError> { + Ok(match matcher { + Image => Some(DynamicBendable::Image(img::open(path)?)), + App | Archive => Some(DynamicBendable::Binary({ + let mut buf = Vec::new(); + File::open(path)?.read_to_end(&mut buf)?; + buf + })), + Audio => todo!(), + Book => todo!(), + Doc => todo!(), + Font => todo!(), + Text => todo!(), + Video => todo!(), + Custom => None, + }) + }) + .transpose() + .map(|opt| -> Option { opt? }) } }