rust-embed-utils-8.2.0/.cargo_vcs_info.json0000644000000001430000000000100142460ustar { "git": { "sha1": "ed8faec32e3d17f115e768ec14e731ac2f237720" }, "path_in_vcs": "utils" }rust-embed-utils-8.2.0/Cargo.toml0000644000000022300000000000100122430ustar # 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.2.0" authors = ["pyros2097 "] 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" [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" [features] debug-embed = [] include-exclude = ["globset"] mime-guess = ["mime_guess"] rust-embed-utils-8.2.0/Cargo.toml.orig000064400000000000000000000012051046102023000157250ustar 00000000000000[package] name = "rust-embed-utils" version = "8.2.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.2.0/license000064400000000000000000000020651046102023000144100ustar 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.2.0/readme.md000064400000000000000000000001271046102023000146170ustar 00000000000000# Rust Embed Utilities The utilities used by rust-embed and rust-embed-impl lie here. rust-embed-utils-8.2.0/src/lib.rs000064400000000000000000000114541046102023000147500ustar 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(not(feature = "include-exclude"))] pub fn is_path_included(_path: &str, _includes: &[&str], _excludes: &[&str]) -> bool { true } #[cfg(feature = "include-exclude")] pub fn is_path_included(rel_path: &str, includes: &[&str], excludes: &[&str]) -> bool { use globset::Glob; // ignore path matched by exclusion pattern for exclude in excludes { let pattern = Glob::new(exclude) .unwrap_or_else(|_| panic!("invalid exclude pattern '{}'", exclude)) .compile_matcher(); if pattern.is_match(rel_path) { return false; } } // accept path if no includes provided if includes.is_empty() { return true; } // accept path if matched by inclusion pattern for include in includes { let pattern = Glob::new(include) .unwrap_or_else(|_| panic!("invalid include pattern '{}'", include)) .compile_matcher(); if pattern.is_match(rel_path) { return true; } } false } #[cfg_attr(all(debug_assertions, not(feature = "debug-embed")), allow(unused))] pub fn get_files<'patterns>(folder_path: String, includes: &'patterns [&str], excludes: &'patterns [&str]) -> impl Iterator + 'patterns { walkdir::WalkDir::new(&folder_path) .follow_links(true) .sort_by_file_name() .into_iter() .filter_map(|e| e.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 is_path_included(&rel_path, includes, excludes) { 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().map(|last_modified| { last_modified .duration_since(SystemTime::UNIX_EPOCH) .expect("Time before the UNIX epoch is unsupported") .as_secs() }); let created = metadata.created().ok().map(|created| { created .duration_since(SystemTime::UNIX_EPOCH) .expect("Time before the UNIX epoch is unsupported") .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() }