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> {
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<Option<DynamicBendable>, 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<DynamicBendable> { opt? })
}
}