~repos /rust-embed

#rust#proc-macro#http

git clone https://pyrossh.dev/repos/rust-embed.git
Discussions: https://groups.google.com/g/rust-embed-devs

rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.


Files changed (4) hide show
  1. .github/workflows/test.yml +2 -0
  2. Cargo.toml +1 -2
  3. examples/rocket.rs +18 -24
  4. readme.md +2 -2
.github/workflows/test.yml CHANGED
@@ -36,11 +36,13 @@ jobs:
36
36
  cargo test --test interpolated_path --features "interpolate-folder-path"
37
37
  cargo test --test interpolated_path --features "interpolate-folder-path" --release
38
38
  cargo build --example basic
39
+ cargo build --example rocket --features rocket
39
40
  cargo build --example actix --features actix
40
41
  cargo build --example axum --features axum-ex
41
42
  cargo build --example warp --features warp-ex
42
43
  cargo test --test lib --release
43
44
  cargo build --example basic --release
45
+ cargo build --example rocket --features rocket --release
44
46
  cargo build --example actix --features actix --release
45
47
  cargo build --example axum --features axum-ex --release
46
48
  cargo build --example warp --features warp-ex --release
Cargo.toml CHANGED
@@ -57,7 +57,7 @@ mime_guess = { version = "2", optional = true }
57
57
  hex = { version = "0.4.3", optional = true }
58
58
  tokio = { version = "1.0", optional = true, features = ["macros", "rt-multi-thread"] }
59
59
  warp = { version = "0.3", default-features = false, optional = true }
60
- rocket = { version = "0.4.5", default-features = false, optional = true }
60
+ rocket = { version = "0.5.0-rc.1", default-features = false, optional = true }
61
61
  axum = { version = "0.4", default-features = false, features = ["http1"], optional = true }
62
62
  poem = { version = "1.0.8", default-features = false, optional = true }
63
63
 
@@ -69,7 +69,6 @@ debug-embed = ["rust-embed-impl/debug-embed", "rust-embed-utils/debug-embed"]
69
69
  interpolate-folder-path = ["rust-embed-impl/interpolate-folder-path"]
70
70
  compression = ["rust-embed-impl/compression", "include-flate"]
71
71
  include-exclude = ["rust-embed-impl/include-exclude", "rust-embed-utils/include-exclude"]
72
- nightly = ["rocket"]
73
72
  actix = ["actix-web", "mime_guess"]
74
73
  warp-ex = ["warp", "tokio", "mime_guess"]
75
74
  axum-ex = ["axum", "tokio", "mime_guess"]
examples/rocket.rs CHANGED
@@ -1,13 +1,12 @@
1
- #![feature(decl_macro, proc_macro_hygiene)]
2
1
  #[macro_use]
3
2
  extern crate rocket;
4
3
 
5
- use rocket::http::{ContentType, Status};
4
+ use rocket::http::ContentType;
6
- use rocket::response;
5
+ use rocket::response::content::Html;
7
6
  use rust_embed::RustEmbed;
8
7
 
8
+ use std::borrow::Cow;
9
9
  use std::ffi::OsStr;
10
- use std::io::Cursor;
11
10
  use std::path::PathBuf;
12
11
 
13
12
  #[derive(RustEmbed)]
@@ -15,30 +14,25 @@ use std::path::PathBuf;
15
14
  struct Asset;
16
15
 
17
16
  #[get("/")]
18
- fn index<'r>() -> response::Result<'r> {
17
+ fn index() -> Option<Html<Cow<'static, [u8]>>> {
19
- Asset::get("index.html").map_or_else(
18
+ let asset = Asset::get("index.html")?;
20
- || Err(Status::NotFound),
19
+ Some(Html(asset.data))
21
- |d| response::Response::build().header(ContentType::HTML).sized_body(Cursor::new(d.data)).ok(),
22
- )
23
20
  }
24
21
 
25
22
  #[get("/dist/<file..>")]
26
- fn dist<'r>(file: PathBuf) -> response::Result<'r> {
23
+ fn dist(file: PathBuf) -> Option<(ContentType, Cow<'static, [u8]>)> {
27
24
  let filename = file.display().to_string();
28
- Asset::get(&filename).map_or_else(
25
+ let asset = Asset::get(&filename)?;
29
- || Err(Status::NotFound),
30
- |d| {
31
- let ext = file
26
+ let content_type = file
32
- .as_path()
33
- .extension()
27
+ .extension()
34
- .and_then(OsStr::to_str)
28
+ .and_then(OsStr::to_str)
35
- .ok_or_else(|| Status::new(400, "Could not get file extension"))?;
29
+ .and_then(ContentType::from_extension)
36
- let content_type = ContentType::from_extension(ext).ok_or_else(|| Status::new(400, "Could not get file content type"))?;
30
+ .unwrap_or(ContentType::Bytes);
31
+
37
- response::Response::build().header(content_type).sized_body(Cursor::new(d.data)).ok()
32
+ Some((content_type, asset.data))
38
- },
39
- )
40
33
  }
41
34
 
35
+ #[rocket::launch]
42
- fn main() {
36
+ fn rocket() -> _ {
43
- rocket::ignite().mount("/", routes![index, dist]).launch();
37
+ rocket::build().mount("/", routes![index, dist])
44
38
  }
readme.md CHANGED
@@ -151,9 +151,9 @@ Note: To run the `actix-web` example:
151
151
 
152
152
  `cargo run --example actix --features actix`
153
153
 
154
- Note: To run the `rocket` example, add the `nightly` feature flag and run on a nightly build:
154
+ Note: To run the `rocket` example:
155
155
 
156
- `cargo +nightly run --example rocket --features nightly`
156
+ `cargo run --example rocket --features rocket`
157
157
 
158
158
  Note: To run the `warp` example:
159
159