cleanup generic open function

This commit is contained in:
Breval Ferrari 2025-03-06 12:55:59 -05:00
parent 3527c0a2af
commit bcce055d4c
Signed by: breval
GPG key ID: 6AEDB3B098E5B3DD

View file

@ -40,25 +40,27 @@ pub mod dynamic {
pub fn open<P: AsRef<Path>>(path: P) -> Result<Option<DynamicBendable>, OpenError> { pub fn open<P: AsRef<Path>>(path: P) -> Result<Option<DynamicBendable>, OpenError> {
use MatcherType::*; use MatcherType::*;
if let Some(matcher) = infer::get_from_path(&path)?.map(|t| t.matcher_type()) { infer::get_from_path(&path)?
Ok(Some(match matcher { .map(|t| t.matcher_type())
Image => DynamicBendable::Image(img::open(path)?), .map(|matcher| -> Result<Option<DynamicBendable>, OpenError> {
App | Archive => DynamicBendable::Binary({ Ok(match matcher {
Image => Some(DynamicBendable::Image(img::open(path)?)),
App | Archive => Some(DynamicBendable::Binary({
let mut buf = Vec::new(); let mut buf = Vec::new();
File::open(path)?.read_to_end(&mut buf)?; File::open(path)?.read_to_end(&mut buf)?;
buf buf
}), })),
Audio => todo!(), Audio => todo!(),
Book => todo!(), Book => todo!(),
Doc => todo!(), Doc => todo!(),
Font => todo!(), Font => todo!(),
Text => todo!(), Text => todo!(),
Video => todo!(), Video => todo!(),
Custom => unimplemented!("I don't even know what this is!"), Custom => None,
})) })
} else { })
Ok(None) .transpose()
} .map(|opt| -> Option<DynamicBendable> { opt? })
} }
} }