rust_embed_utils/
lib.rs

1#![forbid(unsafe_code)]
2
3use sha2::Digest;
4use std::borrow::Cow;
5use std::path::Path;
6use std::time::SystemTime;
7use std::{fs, io};
8
9#[cfg_attr(all(debug_assertions, not(feature = "debug-embed")), allow(unused))]
10pub struct FileEntry {
11  pub rel_path: String,
12  pub full_canonical_path: String,
13}
14
15#[cfg_attr(all(debug_assertions, not(feature = "debug-embed")), allow(unused))]
16pub fn get_files(folder_path: String, matcher: PathMatcher) -> impl Iterator<Item = FileEntry> {
17  walkdir::WalkDir::new(&folder_path)
18    .follow_links(true)
19    .sort_by_file_name()
20    .into_iter()
21    .filter_map(std::result::Result::ok)
22    .filter(|e| e.file_type().is_file())
23    .filter_map(move |e| {
24      let rel_path = path_to_str(e.path().strip_prefix(&folder_path).unwrap());
25      let full_canonical_path = path_to_str(std::fs::canonicalize(e.path()).expect("Could not get canonical path"));
26
27      let rel_path = if std::path::MAIN_SEPARATOR == '\\' {
28        rel_path.replace('\\', "/")
29      } else {
30        rel_path
31      };
32      if matcher.is_path_included(&rel_path) {
33        Some(FileEntry { rel_path, full_canonical_path })
34      } else {
35        None
36      }
37    })
38}
39
40/// A file embedded into the binary
41#[derive(Clone)]
42pub struct EmbeddedFile {
43  pub data: Cow<'static, [u8]>,
44  pub metadata: Metadata,
45}
46
47/// Metadata about an embedded file
48#[derive(Clone)]
49pub struct Metadata {
50  hash: [u8; 32],
51  last_modified: Option<u64>,
52  created: Option<u64>,
53  #[cfg(feature = "mime-guess")]
54  mimetype: Cow<'static, str>,
55}
56
57impl Metadata {
58  #[doc(hidden)]
59  pub const fn __rust_embed_new(
60    hash: [u8; 32], last_modified: Option<u64>, created: Option<u64>, #[cfg(feature = "mime-guess")] mimetype: &'static str,
61  ) -> Self {
62    Self {
63      hash,
64      last_modified,
65      created,
66      #[cfg(feature = "mime-guess")]
67      mimetype: Cow::Borrowed(mimetype),
68    }
69  }
70
71  /// The SHA256 hash of the file
72  pub fn sha256_hash(&self) -> [u8; 32] {
73    self.hash
74  }
75
76  /// The last modified date in seconds since the UNIX epoch. If the underlying
77  /// platform/file-system does not support this, None is returned.
78  pub fn last_modified(&self) -> Option<u64> {
79    self.last_modified
80  }
81
82  /// The created data in seconds since the UNIX epoch. If the underlying
83  /// platform/file-system does not support this, None is returned.
84  pub fn created(&self) -> Option<u64> {
85    self.created
86  }
87
88  /// The mime type of the file
89  #[cfg(feature = "mime-guess")]
90  pub fn mimetype(&self) -> &str {
91    &self.mimetype
92  }
93}
94
95pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
96  let data = fs::read(file_path)?;
97  let data = Cow::from(data);
98
99  let mut hasher = sha2::Sha256::new();
100  hasher.update(&data);
101  let hash: [u8; 32] = hasher.finalize().into();
102
103  let source_date_epoch = match std::env::var("SOURCE_DATE_EPOCH") {
104    Ok(value) => value.parse::<u64>().ok(),
105    Err(_) => None,
106  };
107
108  let metadata = fs::metadata(file_path)?;
109  let last_modified = metadata
110    .modified()
111    .ok()
112    .and_then(|modified| modified.duration_since(SystemTime::UNIX_EPOCH).ok())
113    .map(|secs| secs.as_secs());
114
115  let created = metadata
116    .created()
117    .ok()
118    .and_then(|created| created.duration_since(SystemTime::UNIX_EPOCH).ok())
119    .map(|secs| secs.as_secs());
120
121  #[cfg(feature = "mime-guess")]
122  let mimetype = mime_guess::from_path(file_path).first_or_octet_stream().to_string();
123
124  Ok(EmbeddedFile {
125    data,
126    metadata: Metadata {
127      hash,
128      last_modified: source_date_epoch.or(last_modified),
129      created: source_date_epoch.or(created),
130      #[cfg(feature = "mime-guess")]
131      mimetype: mimetype.into(),
132    },
133  })
134}
135
136fn path_to_str<P: AsRef<std::path::Path>>(p: P) -> String {
137  p.as_ref().to_str().expect("Path does not have a string representation").to_owned()
138}
139
140#[derive(Clone)]
141pub struct PathMatcher {
142  #[cfg(feature = "include-exclude")]
143  include_matcher: globset::GlobSet,
144  #[cfg(feature = "include-exclude")]
145  exclude_matcher: globset::GlobSet,
146}
147
148#[cfg(feature = "include-exclude")]
149impl PathMatcher {
150  pub fn new(includes: &[&str], excludes: &[&str]) -> Self {
151    let mut include_matcher = globset::GlobSetBuilder::new();
152    for include in includes {
153      include_matcher.add(globset::Glob::new(include).unwrap_or_else(|_| panic!("invalid include pattern '{}'", include)));
154    }
155    let include_matcher = include_matcher
156      .build()
157      .unwrap_or_else(|_| panic!("Could not compile included patterns matcher"));
158
159    let mut exclude_matcher = globset::GlobSetBuilder::new();
160    for exclude in excludes {
161      exclude_matcher.add(globset::Glob::new(exclude).unwrap_or_else(|_| panic!("invalid exclude pattern '{}'", exclude)));
162    }
163    let exclude_matcher = exclude_matcher
164      .build()
165      .unwrap_or_else(|_| panic!("Could not compile excluded patterns matcher"));
166
167    Self {
168      include_matcher,
169      exclude_matcher,
170    }
171  }
172  pub fn is_path_included(&self, path: &str) -> bool {
173    !self.exclude_matcher.is_match(path) && (self.include_matcher.is_empty() || self.include_matcher.is_match(path))
174  }
175}
176
177#[cfg(not(feature = "include-exclude"))]
178impl PathMatcher {
179  pub fn new(_includes: &[&str], _excludes: &[&str]) -> Self {
180    Self {}
181  }
182  pub fn is_path_included(&self, _path: &str) -> bool {
183    true
184  }
185}