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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::str::from_utf8;
#[derive(Debug)]
pub struct Archive {
files: BTreeMap<String, String>,
}
impl Archive {
pub fn load(file: &Path) -> Result<Archive, String> {
let mut f = File::open(file).map_err(|e| format!("{}", e))?;
let mut data = vec![];
f.read_to_end(&mut data).map_err(|e| format!("{}", e))?;
Archive::parse(from_utf8(&data).map_err(|e| format!("{}", e))?)
}
pub fn parse(data: &str) -> Result<Archive, String> {
let mut files = BTreeMap::new();
let boundary = format!(
"\n{}",
find_boundary(data)
.ok_or_else(|| "No archive boundary found".to_string())?
);
for item in data[boundary.len() - 1..].split(&boundary) {
if item.is_empty() || item.starts_with('\n') {
} else if let Some(item) = item.strip_prefix(' ') {
if let Some(nl) = item.find('\n') {
let name = &item[..nl];
let body = &item[1 + nl..];
files.insert(name.into(), body.into());
} else {
files.insert(item.into(), String::new());
}
} else {
return Err(format!("Invalid item: {:?}", item));
}
}
Ok(Archive { files })
}
pub fn names(&self) -> Vec<&str> {
self.files.keys().map(|s| s.as_ref()).collect()
}
pub fn get(&self, name: &str) -> Option<&str> {
self.files.get(name).map(|s| s.as_ref())
}
pub fn entries(&self) -> impl Iterator<Item = (&str, &str)> {
self.files.iter().map(|(k, v)| (k.as_ref(), v.as_ref()))
}
}
fn find_boundary(data: &str) -> Option<&str> {
for (i, b) in data.bytes().enumerate() {
match (i, b) {
(0, b'<') => (),
(_i, b'=') => (),
(i, b'>') => return Some(&data[0..i + 1]),
_ => return None,
}
}
None
}