Skip to main content

rust_embed_for_web_utils/
lib.rs

1//! This crate contains utility code for `rust-embed-for-web`.
2//!
3//! You generally don't want to use this crate directly, `rust-embed-for-web`
4//! re-exports any necessary parts from this crate.
5#![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 = match std::fs::canonicalize(e.path()) {
32                Ok(path) => path_to_str(path),
33                // With allow_missing set, tolerate entries that disappear (e.g.
34                // broken symlinks) instead of aborting the build.
35                Err(err)
36                    if config.allow_missing() && err.kind() == std::io::ErrorKind::NotFound =>
37                {
38                    return None
39                }
40                Err(err) => panic!("Could not get canonical path: {}", err),
41            };
42
43            let rel_path = if std::path::MAIN_SEPARATOR == '\\' {
44                rel_path.replace('\\', "/")
45            } else {
46                rel_path
47            };
48
49            if !config.should_include(&rel_path) {
50                return None;
51            }
52
53            Some(FileEntry {
54                rel_path,
55                full_canonical_path,
56            })
57        })
58}
59
60fn path_to_str<P: AsRef<std::path::Path>>(p: P) -> String {
61    p.as_ref()
62        .to_str()
63        .expect("Path does not have a string representation")
64        .to_owned()
65}
66
67#[cfg(all(test, unix))]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn allow_missing_skips_broken_symlinks() {
73        let dir = std::env::temp_dir().join(format!("rust-embed-for-web-{}", std::process::id()));
74        std::fs::create_dir_all(&dir).unwrap();
75        let link = dir.join("broken.txt");
76        let _ = std::fs::remove_file(&link);
77        std::os::unix::fs::symlink(dir.join("does-not-exist"), &link).unwrap();
78
79        let mut config = Config::default();
80        config.set_allow_missing(true);
81        let files: Vec<_> = get_files(dir.to_str().unwrap(), &config, "").collect();
82
83        std::fs::remove_dir_all(&dir).unwrap();
84        assert!(files.is_empty());
85    }
86}