1use std::path::{Path, PathBuf};
2
3use crate::Bundle;
4
5#[derive(thiserror::Error, Debug)]
7#[allow(missing_docs)]
8pub enum Error {
9 #[error("An 'idx' extension is expected of an index file: '{0}'")]
10 InvalidPath(PathBuf),
11 #[error(transparent)]
12 Pack(#[from] crate::data::header::decode::Error),
13 #[error(transparent)]
14 Index(#[from] crate::index::init::Error),
15}
16
17impl Bundle {
19 pub fn at(path: impl AsRef<Path>, object_hash: gix_hash::Kind) -> Result<Self, Error> {
26 Self::at_inner(path.as_ref(), object_hash)
27 }
28
29 fn at_inner(path: &Path, object_hash: gix_hash::Kind) -> Result<Self, Error> {
30 let ext = path
31 .extension()
32 .and_then(std::ffi::OsStr::to_str)
33 .ok_or_else(|| Error::InvalidPath(path.to_owned()))?;
34 Ok(match ext {
35 "idx" => Self {
36 index: crate::index::File::at(path, object_hash)?,
37 pack: crate::data::File::at(path.with_extension("pack"), object_hash)?,
38 },
39 "pack" => Self {
40 pack: crate::data::File::at(path, object_hash)?,
41 index: crate::index::File::at(path.with_extension("idx"), object_hash)?,
42 },
43 _ => return Err(Error::InvalidPath(path.to_owned())),
44 })
45 }
46}