use gix_error::{Result, validation};
use std::path::Path;
use gix_error::{OptionExt, bail};
use crate::Bundle;
impl Bundle {
pub fn at(path: impl AsRef<Path>, object_hash: gix_hash::Kind) -> Result<Self> {
Self::at_inner(path.as_ref(), object_hash)
}
fn at_inner(path: &Path, object_hash: gix_hash::Kind) -> Result<Self> {
let ext = path.extension().and_then(std::ffi::OsStr::to_str).ok_or_raise(|| {
validation(format!(
"An 'idx' extension is expected of an index file: \"{}\"",
path.display()
))
})?;
Ok(match ext {
"idx" => Self {
index: crate::index::File::at(path, object_hash)?,
pack: crate::data::File::at(path.with_extension("pack"), object_hash)?,
},
"pack" => Self {
pack: crate::data::File::at(path, object_hash)?,
index: crate::index::File::at(path.with_extension("idx"), object_hash)?,
},
_ => {
bail!(validation(format!(
"An 'idx' extension is expected of an index file: \"{}\"",
path.display()
)));
}
})
}
}