Skip to content

Commit

Permalink
lib: re-export RustEmbed as Embed
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrossh committed May 11, 2024
1 parent 36bf70e commit 4527045
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 44 deletions.
4 changes: 2 additions & 2 deletions examples/actix.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 2 additions & 2 deletions examples/axum-spa/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions examples/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -46,7 +46,7 @@ async fn not_found() -> Html<&'static str> {
Html("<h1>404</h1><p>Not Found</p>")
}

#[derive(RustEmbed)]
#[derive(Embed)]
#[folder = "examples/public/"]
struct Asset;

Expand Down
4 changes: 2 additions & 2 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rust_embed::RustEmbed;
use rust_embed::Embed;

#[derive(RustEmbed)]
#[derive(Embed)]
#[folder = "examples/public/"]
struct Asset;

Expand Down
2 changes: 1 addition & 1 deletion examples/poem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions examples/rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion examples/salvo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions examples/warp.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
12 changes: 7 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```
Expand Down Expand Up @@ -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;
```
Expand All @@ -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/*"]
Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
///
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tests/include_exclude.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rust_embed::RustEmbed;
use rust_embed::Embed;

#[derive(RustEmbed)]
#[derive(Embed)]
#[folder = "examples/public/"]
struct AllAssets;

Expand All @@ -13,7 +13,7 @@ fn get_works() {
assert_eq!(AllAssets::iter().count(), 7);
}

#[derive(RustEmbed)]
#[derive(Embed)]
#[folder = "examples/public/"]
#[include = "*.html"]
#[include = "images/*"]
Expand All @@ -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/*"]
Expand All @@ -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"]
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions tests/interpolated_path.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -26,7 +26,7 @@ fn iter_works() {
fn trait_works_generic() {
trait_works_generic_helper::<Asset>();
}
fn trait_works_generic_helper<E: rust_embed::RustEmbed>() {
fn trait_works_generic_helper<E: rust_embed::Embed>() {
let mut num_files = 0;
for file in E::iter() {
assert!(E::get(file.as_ref()).is_some());
Expand Down
18 changes: 15 additions & 3 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
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");
assert!(Asset::get("gg.html").is_none(), "gg.html should not exist");
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() {
Expand All @@ -35,7 +47,7 @@ fn iter_works() {
fn trait_works_generic() {
trait_works_generic_helper::<Asset>();
}
fn trait_works_generic_helper<E: rust_embed::RustEmbed>() {
fn trait_works_generic_helper<E: rust_embed::Embed>() {
let mut num_files = 0;
for file in E::iter() {
assert!(E::get(file.as_ref()).is_some());
Expand Down
4 changes: 2 additions & 2 deletions tests/metadata.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 2 additions & 2 deletions tests/metadata_only.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/mime_guess.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rust_embed::{EmbeddedFile, RustEmbed};
use rust_embed::{Embed, EmbeddedFile};

#[derive(RustEmbed)]
#[derive(Embed)]
#[folder = "examples/public/"]
struct Asset;

Expand Down
6 changes: 3 additions & 3 deletions tests/path_traversal_attack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rust_embed::RustEmbed;
use rust_embed::Embed;

#[derive(RustEmbed)]
#[derive(Embed)]
#[folder = "examples/public/"]
struct Assets;

Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions tests/prefix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rust_embed::RustEmbed;
use rust_embed::Embed;

#[derive(RustEmbed)]
#[derive(Embed)]
#[folder = "examples/public/"]
#[prefix = "prefix/"]
struct Asset;
Expand Down

0 comments on commit 4527045

Please sign in to comment.