interpret as bytes by default

This commit is contained in:
Breval Ferrari 2025-04-15 15:54:39 -04:00
parent efb9c7c39d
commit 24d4b5ac2c
Signed by: breval
GPG key ID: A2EEBF62257FF960

View file

@ -116,17 +116,11 @@ mod dynamic {
infer::get_from_path(&path)? infer::get_from_path(&path)?
.map(|t| (t.matcher_type(), t.extension())) .map(|t| (t.matcher_type(), t.extension()))
.map( .map(
|(matcher, extension)| -> Result<Option<DynamicBendable>, OpenError> { |(matcher, extension)| -> Result<DynamicBendable, OpenError> {
Ok(match matcher { Ok(match matcher {
Image => Some(DynamicBendable::Image(img::open(path)?)), Image => DynamicBendable::Image(img::open(path)?),
App | Archive if extension != "pdf" => Some(DynamicBendable::Binary({ Audio => DynamicBendable::Sound(crate::snd::Audio::open(path)?),
let mut buf = Vec::new(); Archive if extension == "pdf" => DynamicBendable::Archive(
File::open(path)?.read_to_end(&mut buf)?;
buf
})),
App => unreachable!(),
Audio => Some(DynamicBendable::Sound(crate::snd::Audio::open(path)?)),
Archive if extension == "pdf" => Some(DynamicBendable::Archive(
PdfDocument::try_from_data_bytes( PdfDocument::try_from_data_bytes(
File::open(path)? File::open(path)?
.bytes() .bytes()
@ -135,39 +129,39 @@ mod dynamic {
Default::default(), Default::default(),
) )
.map_err(OpenError::Pdf)?, .map_err(OpenError::Pdf)?,
)), ),
Archive => { Archive => {
let document_type = DocumentType::from_extension(extension) let document_type = DocumentType::from_extension(extension)
.ok_or(ShivaUnknownExtensionError) .ok_or(ShivaUnknownExtensionError)
.map_err(ShivaError::UnknownExtension)?; .map_err(ShivaError::UnknownExtension)?;
Some(DynamicBendable::Doc(ShivaDocument::new( DynamicBendable::Doc(ShivaDocument::new(
Document::parse( Document::parse(
&bytes::Bytes::from(std::fs::read(path)?), &bytes::Bytes::from(std::fs::read(path)?),
document_type, document_type,
) )
.map_err(ShivaError::Anyhow)?, .map_err(ShivaError::Anyhow)?,
document_type, document_type,
))) ))
} }
Book => todo!(), Book => todo!(),
Doc => todo!(), Doc => todo!(),
Font => todo!(), Font => todo!(),
Text => Some(DynamicBendable::Text( Text => DynamicBendable::Text(crate::txt::Text::try_from_data_bytes(
crate::txt::Text::try_from_data_bytes( File::open(path)?
File::open(path)? .bytes()
.bytes() .collect::<Result<Bytes, io::Error>>()?,
.collect::<Result<Bytes, io::Error>>()?, (),
(), Default::default(),
Default::default(), )?),
)?, _ => DynamicBendable::Binary({
)), let mut buf = Vec::new();
Video => todo!(), File::open(path)?.read_to_end(&mut buf)?;
Custom => None, buf
}),
}) })
}, },
) )
.transpose() .transpose()
.map(|opt| -> Option<DynamicBendable> { opt? })
} }
} }