Skip to main content

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()).ok()?);
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/// A file embedded into the binary compressed
48#[cfg(feature = "compression")]
49pub struct EmbeddedCompressedFile {
50  pub data: &'static include_flate::IFlate,
51  pub metadata: Metadata,
52}
53
54#[cfg(feature = "compression")]
55impl EmbeddedCompressedFile {
56  /// Returns the HTTP `Content-Encoding` header value for this file's compression algorithm.
57  pub fn content_encoding(&self) -> &'static str {
58    match self.data.algo() {
59      include_flate::CompressionMethod::Deflate => "deflate",
60      include_flate::CompressionMethod::Zstd => "zstd",
61    }
62  }
63}
64
65/// Metadata about an embedded file
66#[derive(Clone)]
67pub struct Metadata {
68  hash: [u8; 32],
69  last_modified: Option<u64>,
70  created: Option<u64>,
71  #[cfg(feature = "mime-guess")]
72  mimetype: Cow<'static, str>,
73}
74
75/// Constructs a [`Metadata`] from tokens emitted by the derive macro.
76///
77/// The derive macro (`rust-embed-impl`) is a proc-macro and is therefore always
78/// compiled for the *host*, where its `mime-guess` feature is resolved
79/// independently of the *target* that the generated code is compiled into. It
80/// must not branch on its own `#[cfg(feature = "mime-guess")]`, since that
81/// reflects the host rather than the target. Instead it unconditionally emits a
82/// `__mimetype_of!(...)` call as the final argument and defers to this macro,
83/// which *is* compiled for the target. This variant (target has `mime-guess`)
84/// forwards the argument, expanding the proc macro to the real mimetype.
85#[cfg(feature = "mime-guess")]
86#[doc(hidden)]
87#[macro_export]
88macro_rules! __rust_embed_metadata {
89  ($hash:expr, $last_modified:expr, $created:expr, $mimetype:expr $(,)?) => {
90    $crate::Metadata::__rust_embed_new($hash, $last_modified, $created, $mimetype)
91  };
92}
93
94/// See the `mime-guess` variant above. This variant (target lacks `mime-guess`)
95/// drops the final argument, so the `__mimetype_of!` call it contains is never
96/// expanded.
97#[cfg(not(feature = "mime-guess"))]
98#[doc(hidden)]
99#[macro_export]
100macro_rules! __rust_embed_metadata {
101  ($hash:expr, $last_modified:expr, $created:expr, $mimetype:expr $(,)?) => {
102    $crate::Metadata::__rust_embed_new($hash, $last_modified, $created)
103  };
104}
105
106impl Metadata {
107  #[doc(hidden)]
108  pub const fn __rust_embed_new(
109    hash: [u8; 32], last_modified: Option<u64>, created: Option<u64>, #[cfg(feature = "mime-guess")] mimetype: &'static str,
110  ) -> Self {
111    Self {
112      hash,
113      last_modified,
114      created,
115      #[cfg(feature = "mime-guess")]
116      mimetype: Cow::Borrowed(mimetype),
117    }
118  }
119
120  /// The SHA256 hash of the file
121  pub fn sha256_hash(&self) -> [u8; 32] {
122    self.hash
123  }
124
125  /// The last modified date in seconds since the UNIX epoch. If the underlying
126  /// platform/file-system does not support this, None is returned.
127  pub fn last_modified(&self) -> Option<u64> {
128    self.last_modified
129  }
130
131  /// The created data in seconds since the UNIX epoch. If the underlying
132  /// platform/file-system does not support this, None is returned.
133  pub fn created(&self) -> Option<u64> {
134    self.created
135  }
136
137  /// The mime type of the file
138  #[cfg(feature = "mime-guess")]
139  pub fn mimetype(&self) -> &str {
140    &self.mimetype
141  }
142}
143
144pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
145  let data = fs::read(file_path)?;
146  let data = Cow::from(data);
147
148  let mut hasher = sha2::Sha256::new();
149  hasher.update(&data);
150  let hash: [u8; 32] = hasher.finalize().into();
151
152  let source_date_epoch = match std::env::var("SOURCE_DATE_EPOCH") {
153    Ok(value) => value.parse::<u64>().ok(),
154    Err(_) => None,
155  };
156
157  let metadata = fs::metadata(file_path)?;
158  let last_modified = metadata
159    .modified()
160    .ok()
161    .and_then(|modified| modified.duration_since(SystemTime::UNIX_EPOCH).ok())
162    .map(|secs| secs.as_secs());
163
164  let created = metadata
165    .created()
166    .ok()
167    .and_then(|created| created.duration_since(SystemTime::UNIX_EPOCH).ok())
168    .map(|secs| secs.as_secs());
169
170  #[cfg(feature = "mime-guess")]
171  let mimetype = mime_guess::from_path(file_path).first_or_octet_stream().to_string();
172
173  Ok(EmbeddedFile {
174    data,
175    metadata: Metadata {
176      hash,
177      last_modified: source_date_epoch.or(last_modified),
178      created: source_date_epoch.or(created),
179      #[cfg(feature = "mime-guess")]
180      mimetype: mimetype.into(),
181    },
182  })
183}
184
185fn path_to_str<P: AsRef<std::path::Path>>(p: P) -> String {
186  p.as_ref().to_str().expect("Path does not have a string representation").to_owned()
187}
188
189#[derive(Clone)]
190pub struct PathMatcher {
191  #[cfg(feature = "include-exclude")]
192  include_matcher: globset::GlobSet,
193  #[cfg(feature = "include-exclude")]
194  exclude_matcher: globset::GlobSet,
195}
196
197#[cfg(feature = "include-exclude")]
198impl PathMatcher {
199  pub fn new(includes: &[&str], excludes: &[&str]) -> Self {
200    let mut include_matcher = globset::GlobSetBuilder::new();
201    for include in includes {
202      include_matcher.add(globset::Glob::new(include).unwrap_or_else(|_| panic!("invalid include pattern '{}'", include)));
203    }
204    let include_matcher = include_matcher
205      .build()
206      .unwrap_or_else(|_| panic!("Could not compile included patterns matcher"));
207
208    let mut exclude_matcher = globset::GlobSetBuilder::new();
209    for exclude in excludes {
210      exclude_matcher.add(globset::Glob::new(exclude).unwrap_or_else(|_| panic!("invalid exclude pattern '{}'", exclude)));
211    }
212    let exclude_matcher = exclude_matcher
213      .build()
214      .unwrap_or_else(|_| panic!("Could not compile excluded patterns matcher"));
215
216    Self {
217      include_matcher,
218      exclude_matcher,
219    }
220  }
221  pub fn is_path_included(&self, path: &str) -> bool {
222    !self.exclude_matcher.is_match(path) && (self.include_matcher.is_empty() || self.include_matcher.is_match(path))
223  }
224}
225
226#[cfg(not(feature = "include-exclude"))]
227impl PathMatcher {
228  pub fn new(_includes: &[&str], _excludes: &[&str]) -> Self {
229    Self {}
230  }
231  pub fn is_path_included(&self, _path: &str) -> bool {
232    true
233  }
234}