-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinclude_dir.rs
136 lines (118 loc) · 2.86 KB
/
include_dir.rs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::{
borrow::Cow,
collections::BTreeMap,
path::{Path, PathBuf},
};
pub enum IncludeDir {
Fs(FsDirectory),
Included(IncludedDirectory),
}
pub enum FsOrIncludedFile {
Fs(FsFile),
Included(IncludedFile),
}
impl IncludeDir {
pub fn read(&self, path: &Path) -> Option<FsOrIncludedFile> {
match self {
IncludeDir::Fs(s) => s.read(path),
IncludeDir::Included(s) => s.read(path),
}
}
}
impl IntoIterator for IncludeDir {
type Item = (PathBuf, FsOrIncludedFile);
type IntoIter = FsOrIncludedIntoIter;
fn into_iter(self) -> Self::IntoIter {
match self {
IncludeDir::Fs(fs) => FsOrIncludedIntoIter::Fs(
walkdir::WalkDir::new(fs.0).sort_by_file_name().into_iter(),
),
IncludeDir::Included(embedded) => {
FsOrIncludedIntoIter::Included(embedded.0.into_iter())
}
}
}
}
pub enum FsOrIncludedIntoIter {
Fs(walkdir::IntoIter),
Included(std::collections::btree_map::IntoIter<&'static Path, IncludedFile>),
}
impl Iterator for FsOrIncludedIntoIter {
type Item = (PathBuf, FsOrIncludedFile);
fn next(&mut self) -> Option<Self::Item> {
match self {
FsOrIncludedIntoIter::Fs(walkdir) => loop {
let entry = match walkdir.next() {
None => return None,
Some(Err(e)) => panic!("{}", e),
Some(Ok(entry)) => entry,
};
if entry.file_type().is_file() {
let path = entry.path().to_owned();
return Some((path.clone(), FsOrIncludedFile::Fs(FsFile(path))));
} else {
continue;
}
},
FsOrIncludedIntoIter::Included(map) => map
.next()
.map(|(path, file)| (path.to_owned(), FsOrIncludedFile::Included(file))),
}
}
}
impl FsOrIncludedFile {
pub fn data(&self) -> Cow<'static, [u8]> {
match self {
FsOrIncludedFile::Fs(s) => s.data(),
FsOrIncludedFile::Included(s) => s.data(),
}
}
pub fn hash(&self) -> Option<&'static str> {
match self {
FsOrIncludedFile::Fs(s) => s.hash(),
FsOrIncludedFile::Included(s) => s.hash(),
}
}
}
pub struct FsDirectory(pub PathBuf);
impl FsDirectory {
pub fn read(&self, path: &Path) -> Option<FsOrIncludedFile> {
let path = self.0.join(path);
if path.exists() {
Some(FsOrIncludedFile::Fs(FsFile(path)))
} else {
None
}
}
}
pub struct FsFile(pub PathBuf);
impl FsFile {
pub fn data(&self) -> Cow<'static, [u8]> {
Cow::Owned(std::fs::read(&self.0).unwrap())
}
pub fn hash(&self) -> Option<&'static str> {
None
}
}
#[derive(Debug)]
pub struct IncludedDirectory(pub BTreeMap<&'static Path, IncludedFile>);
#[derive(Clone, Debug)]
pub struct IncludedFile {
pub data: &'static [u8],
pub hash: &'static str,
}
impl IncludedDirectory {
pub fn read(&self, path: &Path) -> Option<FsOrIncludedFile> {
self.0
.get(path)
.map(|file| FsOrIncludedFile::Included(file.clone()))
}
}
impl IncludedFile {
pub fn data(&self) -> Cow<'static, [u8]> {
Cow::Borrowed(self.data)
}
pub fn hash(&self) -> Option<&'static str> {
Some(self.hash)
}
}