diff --git a/bingus/src/doc/pdf.rs b/bingus/src/doc/pdf.rs index b311016..4d64099 100644 --- a/bingus/src/doc/pdf.rs +++ b/bingus/src/doc/pdf.rs @@ -63,7 +63,7 @@ impl Bendable for PdfDocument { } fn format() -> crate::Format { - crate::Format::Doc + crate::Format::Archive } } diff --git a/bingus/src/lib.rs b/bingus/src/lib.rs index 38bc068..d3552a7 100644 --- a/bingus/src/lib.rs +++ b/bingus/src/lib.rs @@ -13,7 +13,10 @@ mod dynamic { path::Path, }; - use crate::snd::{self, Audio}; + use crate::{ + snd::{self, Audio}, + TryFromDataBytes, + }; use super::{ img::{self, DynamicImage}, @@ -32,7 +35,7 @@ mod dynamic { Binary(Bytes), Sound(Audio), Text, - Doc(PdfDocument), + Archive(PdfDocument), } #[derive(Debug, Error)] @@ -43,29 +46,44 @@ mod dynamic { Image(#[from] img::ImageError), #[error("audio: {0}")] Audio(#[from] snd::AudioOpenError), + #[error("pdf: {0}")] + Pdf(String), } pub fn open>(path: P) -> Result, OpenError> { use MatcherType::*; 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 => Some(DynamicBendable::Sound(crate::snd::Audio::open(path)?)), - Book => todo!(), - Doc => todo!(), - Font => todo!(), - Text => todo!(), - Video => todo!(), - Custom => None, - }) - }) + .map(|t| (t.matcher_type(), t.extension())) + .map( + |(matcher, extension)| -> Result, OpenError> { + Ok(match matcher { + Image => Some(DynamicBendable::Image(img::open(path)?)), + App | Archive if extension != "pdf" => Some(DynamicBendable::Binary({ + let mut buf = Vec::new(); + File::open(path)?.read_to_end(&mut buf)?; + buf + })), + App => unreachable!(), + Audio => Some(DynamicBendable::Sound(crate::snd::Audio::open(path)?)), + Archive => Some(DynamicBendable::Archive( + PdfDocument::try_from_data_bytes( + File::open(path)? + .bytes() + .collect::>()?, + (), + Default::default(), + ) + .map_err(OpenError::Pdf)?, + )), + Book => todo!(), + Doc => todo!(), + Font => todo!(), + Text => todo!(), + Video => todo!(), + Custom => None, + }) + }, + ) .transpose() .map(|opt| -> Option { opt? }) }