rust_embed_for_web_utils/
lib.rs1#![forbid(unsafe_code)]
6
7mod file;
8pub use file::*;
9
10mod config;
11pub use config::Config;
12
13pub struct FileEntry {
14 pub rel_path: String,
15 pub full_canonical_path: String,
16}
17
18pub fn get_files<'t>(
19 folder_path: &'t str,
20 config: &'t Config,
21 prefix: &'t str,
22) -> impl Iterator<Item = FileEntry> + 't {
23 walkdir::WalkDir::new(folder_path)
24 .follow_links(true)
25 .into_iter()
26 .filter_map(|e| e.ok())
27 .filter(|e| e.file_type().is_file())
28 .filter_map(move |e| {
29 let rel_path = path_to_str(e.path().strip_prefix(folder_path).unwrap());
30 let rel_path = format!("{}{}", prefix, rel_path);
31 let full_canonical_path =
32 path_to_str(std::fs::canonicalize(e.path()).expect("Could not get canonical path"));
33
34 let rel_path = if std::path::MAIN_SEPARATOR == '\\' {
35 rel_path.replace('\\', "/")
36 } else {
37 rel_path
38 };
39
40 if !config.should_include(&rel_path) {
41 return None;
42 }
43
44 Some(FileEntry {
45 rel_path,
46 full_canonical_path,
47 })
48 })
49}
50
51fn path_to_str<P: AsRef<std::path::Path>>(p: P) -> String {
52 p.as_ref()
53 .to_str()
54 .expect("Path does not have a string representation")
55 .to_owned()
56}