From 4527045ddf4d58e4baf8bf1f873c5fce5f96961e Mon Sep 17 00:00:00 2001 From: pyrossh Date: Sat, 11 May 2024 12:25:28 +0530 Subject: [PATCH] lib: re-export RustEmbed as Embed --- examples/actix.rs | 4 ++-- examples/axum-spa/main.rs | 4 ++-- examples/axum.rs | 4 ++-- examples/basic.rs | 4 ++-- examples/poem.rs | 2 +- examples/rocket.rs | 4 ++-- examples/salvo.rs | 2 +- examples/warp.rs | 4 ++-- readme.md | 12 +++++++----- src/lib.rs | 6 ++++-- tests/include_exclude.rs | 12 ++++++------ tests/interpolated_path.rs | 6 +++--- tests/lib.rs | 18 +++++++++++++++--- tests/metadata.rs | 4 ++-- tests/metadata_only.rs | 4 ++-- tests/mime_guess.rs | 4 ++-- tests/path_traversal_attack.rs | 6 +++--- tests/prefix.rs | 4 ++-- 18 files changed, 60 insertions(+), 44 deletions(-) diff --git a/examples/actix.rs b/examples/actix.rs index fc24214..93f724f 100644 --- a/examples/actix.rs +++ b/examples/actix.rs @@ -1,8 +1,8 @@ use actix_web::{web, App, HttpResponse, HttpServer, Responder}; use mime_guess::from_path; -use rust_embed::RustEmbed; +use rust_embed::Embed; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] struct Asset; diff --git a/examples/axum-spa/main.rs b/examples/axum-spa/main.rs index 028dc1d..0e297fc 100644 --- a/examples/axum-spa/main.rs +++ b/examples/axum-spa/main.rs @@ -3,12 +3,12 @@ use axum::{ response::{Html, IntoResponse, Response}, routing::Router, }; -use rust_embed::RustEmbed; +use rust_embed::Embed; use std::net::SocketAddr; static INDEX_HTML: &str = "index.html"; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/axum-spa/assets/"] struct Assets; diff --git a/examples/axum.rs b/examples/axum.rs index b102eb0..9cd4f70 100644 --- a/examples/axum.rs +++ b/examples/axum.rs @@ -3,7 +3,7 @@ use axum::{ response::{Html, IntoResponse, Response}, routing::{get, Router}, }; -use rust_embed::RustEmbed; +use rust_embed::Embed; use std::net::SocketAddr; #[tokio::main] @@ -46,7 +46,7 @@ async fn not_found() -> Html<&'static str> { Html("

404

Not Found

") } -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] struct Asset; diff --git a/examples/basic.rs b/examples/basic.rs index b143e61..a9d41cd 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,6 +1,6 @@ -use rust_embed::RustEmbed; +use rust_embed::Embed; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] struct Asset; diff --git a/examples/poem.rs b/examples/poem.rs index fa13d58..870efd4 100644 --- a/examples/poem.rs +++ b/examples/poem.rs @@ -14,7 +14,7 @@ async fn main() -> Result<(), std::io::Error> { Ok(()) } -#[derive(rust_embed::RustEmbed)] +#[derive(rust_embed::Embed)] #[folder = "examples/public/"] struct Asset; pub(crate) struct StaticEmbed; diff --git a/examples/rocket.rs b/examples/rocket.rs index 455556c..a4ff612 100644 --- a/examples/rocket.rs +++ b/examples/rocket.rs @@ -3,13 +3,13 @@ extern crate rocket; use rocket::http::ContentType; use rocket::response::content::RawHtml; -use rust_embed::RustEmbed; +use rust_embed::Embed; use std::borrow::Cow; use std::ffi::OsStr; use std::path::PathBuf; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] struct Asset; diff --git a/examples/salvo.rs b/examples/salvo.rs index 6e17ccc..48244f3 100644 --- a/examples/salvo.rs +++ b/examples/salvo.rs @@ -12,7 +12,7 @@ async fn main() -> Result<(), std::io::Error> { Ok(()) } -#[derive(rust_embed::RustEmbed)] +#[derive(rust_embed::Embed)] #[folder = "examples/public/"] struct Asset; diff --git a/examples/warp.rs b/examples/warp.rs index 1164790..c5f34e4 100644 --- a/examples/warp.rs +++ b/examples/warp.rs @@ -1,7 +1,7 @@ -use rust_embed::RustEmbed; +use rust_embed::Embed; use warp::{http::header::HeaderValue, path::Tail, reply::Response, Filter, Rejection, Reply}; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] struct Asset; diff --git a/readme.md b/readme.md index 94e243c..3a6ad51 100644 --- a/readme.md +++ b/readme.md @@ -21,7 +21,7 @@ The path resolution works as follows: - In `release` or when `debug-embed` feature is enabled, the folder path is resolved relative to where `Cargo.toml` is. ```rust -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] struct Asset; ``` @@ -98,7 +98,7 @@ Always embed the files in the binary, even in debug mode. Allow environment variables to be used in the `folder` path. Example: ```rust -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "$CARGO_MANIFEST_DIR/foo"] struct Asset; ``` @@ -116,7 +116,9 @@ Matching is done on relative file paths, via [`globset`]. Example: ```rust -#[derive(RustEmbed)] +use rust_embed::Embed; + +#[derive(Embed)] #[folder = "examples/public/"] #[include = "*.html"] #[include = "images/*"] @@ -127,9 +129,9 @@ struct Asset; ## Usage ```rust -use rust_embed::RustEmbed; +use rust_embed::Embed; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] #[prefix = "prefix/"] struct Asset; diff --git a/src/lib.rs b/src/lib.rs index 60b0f6b..d1fa725 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,9 +21,9 @@ pub extern crate rust_embed_utils as utils; /// /// This trait is meant to be derived like so: /// ``` -/// use rust_embed::RustEmbed; +/// use rust_embed::Embed; /// -/// #[derive(RustEmbed)] +/// #[derive(Embed)] /// #[folder = "examples/public/"] /// struct Asset; /// @@ -50,6 +50,8 @@ pub trait RustEmbed { fn iter() -> Filenames; } +pub use RustEmbed as Embed; + /// An iterator over filenames. /// /// This enum exists for optimization purposes, to avoid boxing the iterator in diff --git a/tests/include_exclude.rs b/tests/include_exclude.rs index a0dd99d..7d08a14 100644 --- a/tests/include_exclude.rs +++ b/tests/include_exclude.rs @@ -1,6 +1,6 @@ -use rust_embed::RustEmbed; +use rust_embed::Embed; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] struct AllAssets; @@ -13,7 +13,7 @@ fn get_works() { assert_eq!(AllAssets::iter().count(), 7); } -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] #[include = "*.html"] #[include = "images/*"] @@ -27,7 +27,7 @@ fn including_some_assets_works() { assert_eq!(IncludeSomeAssets::iter().count(), 4); } -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] #[exclude = "*.html"] #[exclude = "images/*"] @@ -42,7 +42,7 @@ fn excluding_some_assets_works() { assert_eq!(ExcludeSomeAssets::iter().count(), 3); } -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] #[include = "images/*"] #[exclude = "*.txt"] @@ -55,7 +55,7 @@ fn exclude_has_higher_priority() { assert_eq!(ExcludePriorityAssets::iter().count(), 2); } -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/symlinks"] #[include = "main.js"] struct IncludeSymlink; diff --git a/tests/interpolated_path.rs b/tests/interpolated_path.rs index e446eef..3348907 100644 --- a/tests/interpolated_path.rs +++ b/tests/interpolated_path.rs @@ -1,7 +1,7 @@ -use rust_embed::RustEmbed; +use rust_embed::Embed; /// Test doc comment -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "$CARGO_MANIFEST_DIR/examples/public/"] struct Asset; @@ -26,7 +26,7 @@ fn iter_works() { fn trait_works_generic() { trait_works_generic_helper::(); } -fn trait_works_generic_helper() { +fn trait_works_generic_helper() { let mut num_files = 0; for file in E::iter() { assert!(E::get(file.as_ref()).is_some()); diff --git a/tests/lib.rs b/tests/lib.rs index b3d6433..9dc47bd 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,10 +1,14 @@ -use rust_embed::RustEmbed; +use rust_embed::{Embed, RustEmbed}; /// Test doc comment -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] struct Asset; +#[derive(RustEmbed)] +#[folder = "examples/public/"] +struct AssetOld; + #[test] fn get_works() { assert!(Asset::get("index.html").is_some(), "index.html should exist"); @@ -12,6 +16,14 @@ fn get_works() { assert!(Asset::get("images/llama.png").is_some(), "llama.png should exist"); } +// Todo remove this test and rename RustEmbed trait to Embed on a new major release +#[test] +fn get_old_name_works() { + assert!(AssetOld::get("index.html").is_some(), "index.html should exist"); + assert!(AssetOld::get("gg.html").is_none(), "gg.html should not exist"); + assert!(AssetOld::get("images/llama.png").is_some(), "llama.png should exist"); +} + /// Using Windows-style path separators (`\`) is acceptable #[test] fn get_windows_style() { @@ -35,7 +47,7 @@ fn iter_works() { fn trait_works_generic() { trait_works_generic_helper::(); } -fn trait_works_generic_helper() { +fn trait_works_generic_helper() { let mut num_files = 0; for file in E::iter() { assert!(E::get(file.as_ref()).is_some()); diff --git a/tests/metadata.rs b/tests/metadata.rs index 30bddf9..b28d947 100644 --- a/tests/metadata.rs +++ b/tests/metadata.rs @@ -1,8 +1,8 @@ -use rust_embed::{EmbeddedFile, RustEmbed}; +use rust_embed::{Embed, EmbeddedFile}; use sha2::Digest; use std::{fs, time::SystemTime}; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] struct Asset; diff --git a/tests/metadata_only.rs b/tests/metadata_only.rs index 77a7117..00e692a 100644 --- a/tests/metadata_only.rs +++ b/tests/metadata_only.rs @@ -1,6 +1,6 @@ -use rust_embed::{EmbeddedFile, RustEmbed}; +use rust_embed::{Embed, EmbeddedFile}; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] #[metadata_only = true] struct Asset; diff --git a/tests/mime_guess.rs b/tests/mime_guess.rs index b3cab50..669856c 100644 --- a/tests/mime_guess.rs +++ b/tests/mime_guess.rs @@ -1,6 +1,6 @@ -use rust_embed::{EmbeddedFile, RustEmbed}; +use rust_embed::{Embed, EmbeddedFile}; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] struct Asset; diff --git a/tests/path_traversal_attack.rs b/tests/path_traversal_attack.rs index 63e9bee..b487787 100644 --- a/tests/path_traversal_attack.rs +++ b/tests/path_traversal_attack.rs @@ -1,6 +1,6 @@ -use rust_embed::RustEmbed; +use rust_embed::Embed; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] struct Assets; @@ -12,7 +12,7 @@ fn path_traversal_attack_fails() { assert!(Assets::get("../basic.rs").is_none()); } -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/axum-spa/"] struct AxumAssets; diff --git a/tests/prefix.rs b/tests/prefix.rs index b8664fa..427d863 100644 --- a/tests/prefix.rs +++ b/tests/prefix.rs @@ -1,6 +1,6 @@ -use rust_embed::RustEmbed; +use rust_embed::Embed; -#[derive(RustEmbed)] +#[derive(Embed)] #[folder = "examples/public/"] #[prefix = "prefix/"] struct Asset;