~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 (2) hide show
  1. Cargo.toml +1 -1
  2. examples/rocket.rs +18 -24
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.2.3", default-features = false, optional = true }
62
62
  poem = { version = "1.0.8", default-features = false, optional = true }
63
63
 
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
  }