rust-embed-utils-8.7.0/.cargo_vcs_info.json0000644000000001430000000000100142530ustar { "git": { "sha1": "f292486b6c5a2af3c7ecba5905ab3143ebcde10b" }, "path_in_vcs": "utils" }rust-embed-utils-8.7.0/Cargo.lock0000644000000214400000000000100122310ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "aho-corasick" version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" dependencies = [ "memchr", ] [[package]] name = "block-buffer" version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ "generic-array", ] [[package]] name = "bstr" version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "234113d19d0d7d613b40e86fb654acf958910802bcceab913a4f9e7cda03b1a4" dependencies = [ "memchr", "serde", ] [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cpufeatures" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" dependencies = [ "libc", ] [[package]] name = "crypto-common" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", "typenum", ] [[package]] name = "digest" version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", ] [[package]] name = "generic-array" version = "0.14.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ "typenum", "version_check", ] [[package]] name = "globset" version = "0.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54a1028dfc5f5df5da8a56a73e6c153c9a9708ec57232470703592a3f18e49f5" dependencies = [ "aho-corasick", "bstr", "log", "regex-automata", "regex-syntax", ] [[package]] name = "libc" version = "0.2.171" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" [[package]] name = "log" version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "mime" version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" [[package]] name = "mime_guess" version = "2.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" dependencies = [ "mime", "unicase", ] [[package]] name = "proc-macro2" version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" dependencies = [ "proc-macro2", ] [[package]] name = "regex-automata" version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "rust-embed-utils" version = "8.7.0" dependencies = [ "globset", "mime_guess", "sha2", "walkdir", ] [[package]] name = "same-file" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" dependencies = [ "winapi-util", ] [[package]] name = "serde" version = "1.0.219" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.219" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "sha2" version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ "cfg-if", "cpufeatures", "digest", ] [[package]] name = "syn" version = "2.0.100" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "typenum" version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" [[package]] name = "unicase" version = "2.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539" [[package]] name = "unicode-ident" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" [[package]] name = "version_check" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] name = "walkdir" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ "same-file", "winapi-util", ] [[package]] name = "winapi-util" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ "windows-sys", ] [[package]] name = "windows-sys" version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" rust-embed-utils-8.7.0/Cargo.toml0000644000000024670000000000100122640ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "rust-embed-utils" version = "8.7.0" authors = ["pyros2097 "] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "Utilities for rust-embed" documentation = "https://docs.rs/rust-embed" readme = "readme.md" keywords = [ "http", "rocket", "static", "web", "server", ] categories = ["web-programming::http-server"] license = "MIT" repository = "https://github.com/pyros2097/rust-embed" [features] debug-embed = [] include-exclude = ["globset"] mime-guess = ["mime_guess"] [lib] name = "rust_embed_utils" path = "src/lib.rs" [dependencies.globset] version = "0.4.8" optional = true [dependencies.mime_guess] version = "2.0.4" optional = true [dependencies.sha2] version = "0.10.5" [dependencies.walkdir] version = "2.3.1" rust-embed-utils-8.7.0/Cargo.toml.orig000064400000000000000000000012051046102023000157320ustar 00000000000000[package] name = "rust-embed-utils" version = "8.7.0" description = "Utilities for rust-embed" readme = "readme.md" documentation = "https://docs.rs/rust-embed" repository = "https://github.com/pyros2097/rust-embed" license = "MIT" keywords = ["http", "rocket", "static", "web", "server"] categories = ["web-programming::http-server"] authors = ["pyros2097 "] edition = "2018" [dependencies] walkdir = "2.3.1" sha2 = "0.10.5" mime_guess = { version = "2.0.4", optional = true } [dependencies.globset] version = "0.4.8" optional = true [features] debug-embed = [] mime-guess = ["mime_guess"] include-exclude = ["globset"] rust-embed-utils-8.7.0/license000064400000000000000000000020651046102023000144150ustar 00000000000000The MIT License (MIT) Copyright (c) 2018 pyros2097 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rust-embed-utils-8.7.0/readme.md000064400000000000000000000001271046102023000146240ustar 00000000000000# Rust Embed Utilities The utilities used by rust-embed and rust-embed-impl lie here. rust-embed-utils-8.7.0/src/lib.rs000064400000000000000000000123011046102023000147450ustar 00000000000000#![forbid(unsafe_code)] use sha2::Digest; use std::borrow::Cow; use std::path::Path; use std::time::SystemTime; use std::{fs, io}; #[cfg_attr(all(debug_assertions, not(feature = "debug-embed")), allow(unused))] pub struct FileEntry { pub rel_path: String, pub full_canonical_path: String, } #[cfg_attr(all(debug_assertions, not(feature = "debug-embed")), allow(unused))] pub fn get_files(folder_path: String, matcher: PathMatcher) -> impl Iterator { walkdir::WalkDir::new(&folder_path) .follow_links(true) .sort_by_file_name() .into_iter() .filter_map(std::result::Result::ok) .filter(|e| e.file_type().is_file()) .filter_map(move |e| { let rel_path = path_to_str(e.path().strip_prefix(&folder_path).unwrap()); let full_canonical_path = path_to_str(std::fs::canonicalize(e.path()).expect("Could not get canonical path")); let rel_path = if std::path::MAIN_SEPARATOR == '\\' { rel_path.replace('\\', "/") } else { rel_path }; if matcher.is_path_included(&rel_path) { Some(FileEntry { rel_path, full_canonical_path }) } else { None } }) } /// A file embedded into the binary #[derive(Clone)] pub struct EmbeddedFile { pub data: Cow<'static, [u8]>, pub metadata: Metadata, } /// Metadata about an embedded file #[derive(Clone)] pub struct Metadata { hash: [u8; 32], last_modified: Option, created: Option, #[cfg(feature = "mime-guess")] mimetype: Cow<'static, str>, } impl Metadata { #[doc(hidden)] pub const fn __rust_embed_new( hash: [u8; 32], last_modified: Option, created: Option, #[cfg(feature = "mime-guess")] mimetype: &'static str, ) -> Self { Self { hash, last_modified, created, #[cfg(feature = "mime-guess")] mimetype: Cow::Borrowed(mimetype), } } /// The SHA256 hash of the file pub fn sha256_hash(&self) -> [u8; 32] { self.hash } /// The last modified date in seconds since the UNIX epoch. If the underlying /// platform/file-system does not support this, None is returned. pub fn last_modified(&self) -> Option { self.last_modified } /// The created data in seconds since the UNIX epoch. If the underlying /// platform/file-system does not support this, None is returned. pub fn created(&self) -> Option { self.created } /// The mime type of the file #[cfg(feature = "mime-guess")] pub fn mimetype(&self) -> &str { &self.mimetype } } pub fn read_file_from_fs(file_path: &Path) -> io::Result { let data = fs::read(file_path)?; let data = Cow::from(data); let mut hasher = sha2::Sha256::new(); hasher.update(&data); let hash: [u8; 32] = hasher.finalize().into(); let source_date_epoch = match std::env::var("SOURCE_DATE_EPOCH") { Ok(value) => value.parse::().ok(), Err(_) => None, }; let metadata = fs::metadata(file_path)?; let last_modified = metadata .modified() .ok() .and_then(|modified| modified.duration_since(SystemTime::UNIX_EPOCH).ok()) .map(|secs| secs.as_secs()); let created = metadata .created() .ok() .and_then(|created| created.duration_since(SystemTime::UNIX_EPOCH).ok()) .map(|secs| secs.as_secs()); #[cfg(feature = "mime-guess")] let mimetype = mime_guess::from_path(file_path).first_or_octet_stream().to_string(); Ok(EmbeddedFile { data, metadata: Metadata { hash, last_modified: source_date_epoch.or(last_modified), created: source_date_epoch.or(created), #[cfg(feature = "mime-guess")] mimetype: mimetype.into(), }, }) } fn path_to_str>(p: P) -> String { p.as_ref().to_str().expect("Path does not have a string representation").to_owned() } #[derive(Clone)] pub struct PathMatcher { #[cfg(feature = "include-exclude")] include_matcher: globset::GlobSet, #[cfg(feature = "include-exclude")] exclude_matcher: globset::GlobSet, } #[cfg(feature = "include-exclude")] impl PathMatcher { pub fn new(includes: &[&str], excludes: &[&str]) -> Self { let mut include_matcher = globset::GlobSetBuilder::new(); for include in includes { include_matcher.add(globset::Glob::new(include).unwrap_or_else(|_| panic!("invalid include pattern '{}'", include))); } let include_matcher = include_matcher .build() .unwrap_or_else(|_| panic!("Could not compile included patterns matcher")); let mut exclude_matcher = globset::GlobSetBuilder::new(); for exclude in excludes { exclude_matcher.add(globset::Glob::new(exclude).unwrap_or_else(|_| panic!("invalid exclude pattern '{}'", exclude))); } let exclude_matcher = exclude_matcher .build() .unwrap_or_else(|_| panic!("Could not compile excluded patterns matcher")); Self { include_matcher, exclude_matcher, } } pub fn is_path_included(&self, path: &str) -> bool { !self.exclude_matcher.is_match(path) && (self.include_matcher.is_empty() || self.include_matcher.is_match(path)) } } #[cfg(not(feature = "include-exclude"))] impl PathMatcher { pub fn new(_includes: &[&str], _excludes: &[&str]) -> Self { Self {} } pub fn is_path_included(&self, _path: &str) -> bool { true } }