use crate::pack; use std::{ convert::TryFrom, path::{Path, PathBuf}, }; mod locate; /// pub mod write; /// Returned by [`Bundle::at()`] #[derive(thiserror::Error, Debug)] #[allow(missing_docs)] pub enum Error { #[error("An 'idx' extension is expected of an index file: '{0}'")] InvalidPath(PathBuf), #[error(transparent)] Pack(#[from] pack::data::parse::Error), #[error(transparent)] Index(#[from] pack::index::init::Error), } /// A bundle of pack data and the corresponding pack index pub struct Bundle { /// The pack file corresponding to `index` pub pack: pack::data::File, /// The index file corresponding to `pack` pub index: pack::index::File, } /// Initialization impl Bundle { /// Create a `Bundle` from `path`, which is either a pack file _(*.pack)_ or an index file _(*.idx)_. /// /// The corresponding complementary file is expected to be present. /// Also available via [`Bundle::try_from()`]. pub fn at(path: impl AsRef) -> Result { Self::try_from(path.as_ref()) } } impl TryFrom<&Path> for Bundle { type Error = Error; fn try_from(path: &Path) -> Result { let ext = path .extension() .and_then(|e| e.to_str()) .ok_or_else(|| Error::InvalidPath(path.to_owned()))?; Ok(match ext { "idx" => Self { index: pack::index::File::at(path)?, pack: pack::data::File::at(path.with_extension("pack"))?, }, "pack" => Self { pack: pack::data::File::at(path)?, index: pack::index::File::at(path.with_extension("idx"))?, }, _ => return Err(Error::InvalidPath(path.to_owned())), }) } }