This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / knot2 / third_party / gix-pack / src / bundle / init.rs
1.7 kB 46 lines
1use std::path::{Path, PathBuf}; 2 3use crate::Bundle; 4 5/// Returned by [`Bundle::at()`] 6#[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 17/// Initialization 18impl Bundle { 19 /// Create a `Bundle` from `path`, which is either a pack file _(*.pack)_ or an index file _(*.idx)_. 20 /// 21 /// The corresponding complementary file is expected to be present. 22 /// 23 /// The `object_hash` is a way to read (and write) the same file format with different hashes, as the hash kind 24 /// isn't stored within the file format itself. 25 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}