Newer
Older
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
use std::{
fmt::Debug,
fs,
path::{Path, PathBuf},
};
use anyhow::Context;
pub trait Loadable: Sized {
fn load(path: &Path, slug_prefix: &str) -> anyhow::Result<Self>;
}
pub fn load_from_path<L: Loadable>(path: &str, slug_prefix: &str) -> anyhow::Result<Vec<L>> {
recursive_scan(path)?
.iter()
.map(|file_path| {
let mut diff: PathBuf = file_path.strip_prefix(path)?.into();
diff.pop();
let diff = diff
.to_str()
.context("Could not convert path to a string")?;
let prefix = if diff.is_empty() {
slug_prefix.to_string()
} else if slug_prefix.is_empty() {
diff.to_string()
} else {
format!("{slug_prefix}/{diff}")
};
L::load(file_path, &prefix)
})
.collect()
}
pub fn recursive_scan<P>(path: P) -> anyhow::Result<Vec<PathBuf>>
where
P: AsRef<Path> + Debug,
{
let mut out = vec![];
for entry in fs::read_dir(path)? {
let entry = entry?;
let t = entry.file_type()?;
let path = entry.path();
if t.is_dir() {
out.extend(recursive_scan(&path)?);
} else {
out.push(path);
}
}
Ok(out)
}