use crate::pack; use quick_error::quick_error; use std::{ convert::TryFrom, path::{Path, PathBuf}, }; pub mod locate; pub mod write; quick_error! { #[derive(Debug)] pub enum Error { InvalidPath(path: PathBuf) { display("An 'idx' extension is expected of an index file: '{}'", path.display()) } Pack(err: pack::data::parse::Error) { display("Could not instantiate pack") from() source(err) } Index(err: pack::index::init::Error) { display("Could not instantiate pack index") from() source(err) } } } /// A packfile with an index pub struct Bundle { pub pack: pack::data::File, pub index: pack::index::File, } impl Bundle { /// `path` is either a pack file or an index file 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())), }) } }