1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::pack;
use std::{
convert::TryFrom,
path::{Path, PathBuf},
};
mod locate;
pub mod write;
#[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),
}
pub struct Bundle {
pub pack: pack::data::File,
pub index: pack::index::File,
}
impl Bundle {
pub fn at(path: impl AsRef<Path>) -> Result<Self, Error> {
Self::try_from(path.as_ref())
}
}
impl TryFrom<&Path> for Bundle {
type Error = Error;
fn try_from(path: &Path) -> Result<Self, Self::Error> {
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())),
})
}
}