~repos /rust-embed

#rust#proc-macro#http

git clone https://pyrossh.dev/repos/rust-embed.git

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


41a4d0a4 Peter

7 years ago
Merge pull request #40 from vemoo/avoid-vec
Files changed (5) hide show
  1. Cargo.toml +1 -1
  2. examples/actix.rs +6 -13
  3. examples/basic.rs +1 -1
  4. readme.md +4 -4
  5. src/lib.rs +9 -5
Cargo.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "rust-embed"
3
- version = "3.0.2"
3
+ version = "4.0.0"
4
4
  description = "Rust Custom Derive Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev"
5
5
  readme = "readme.md"
6
6
  documentation = "https://docs.rs/rust-embed"
examples/actix.rs CHANGED
@@ -3,8 +3,8 @@ extern crate actix_web;
3
3
  extern crate rust_embed;
4
4
  extern crate mime_guess;
5
5
 
6
- use actix_web::{App, HttpRequest, HttpResponse, server};
7
6
  use actix_web::http::Method;
7
+ use actix_web::{server, App, Body, HttpRequest, HttpResponse};
8
8
  use mime_guess::guess_mime_type;
9
9
 
10
10
  #[derive(RustEmbed)]
@@ -13,11 +13,9 @@ struct Asset;
13
13
 
14
14
  fn handle_embedded_file(path: &str) -> HttpResponse {
15
15
  match Asset::get(path) {
16
- Some(content) => {
16
+ Some(content) => HttpResponse::Ok()
17
- HttpResponse::Ok()
18
- .content_type(guess_mime_type(path).as_ref())
17
+ .content_type(guess_mime_type(path).as_ref())
19
- .body(content)
18
+ .body(Body::from_slice(content.as_ref())),
20
- }
21
19
  None => HttpResponse::NotFound().body("404 Not Found"),
22
20
  }
23
21
  }
@@ -32,13 +30,8 @@ fn dist(req: HttpRequest) -> HttpResponse {
32
30
  }
33
31
 
34
32
  fn main() {
35
- server::new(|| {
36
- App::new().route("/", Method::GET, index).route(
33
+ server::new(|| App::new().route("/", Method::GET, index).route("/dist/{_:.*}", Method::GET, dist))
37
- "/dist/{_:.*}",
38
- Method::GET,
39
- dist,
40
- )
41
- }).bind("127.0.0.1:8000")
34
+ .bind("127.0.0.1:8000")
42
35
  .unwrap()
43
36
  .run();
44
37
  }
examples/basic.rs CHANGED
@@ -7,5 +7,5 @@ struct Asset;
7
7
 
8
8
  fn main() {
9
9
  let index_html = Asset::get("index.html").unwrap();
10
- println!("{:?}", std::str::from_utf8(&index_html));
10
+ println!("{:?}", std::str::from_utf8(index_html.as_ref()));
11
11
  }
readme.md CHANGED
@@ -13,7 +13,7 @@ You can use this to embed your css, js and images into a single executable which
13
13
 
14
14
  ```
15
15
  [dependencies]
16
- rust-embed="3.0.2"
16
+ rust-embed="4.0.0"
17
17
  ```
18
18
 
19
19
  ## Documentation
@@ -23,9 +23,9 @@ You need to add the custom derive macro RustEmbed to your struct with an attribu
23
23
  #[folder = "examples/public/"]
24
24
  struct Asset;
25
25
  ```
26
- This macro adds a single static method `get` to your type. This method allows you to get your assets from the fs during dev and from the binary during release. It takes the file path as string and returns an optional vector of u8.
26
+ This macro adds a single static method `get` to your type. This method allows you to get your assets from the fs during dev and from the binary during release. It takes the file path as string and returns an `Option` with the bytes.
27
27
  ```rust
28
- pub fn get(file_path: &str) -> Option<Vec<u8>>
28
+ pub fn get(file_path: &str) -> Option<impl AsRef<[u8]>>
29
29
  ```
30
30
 
31
31
  ## Usage
@@ -39,7 +39,7 @@ struct Asset;
39
39
 
40
40
  fn main() {
41
41
  let index_html = Asset::get("index.html").unwrap();
42
- println!("{:?}", std::str::from_utf8(&index_html));
42
+ println!("{:?}", std::str::from_utf8(index_html.as_ref()));
43
43
  }
44
44
  ```
45
45
 
src/lib.rs CHANGED
@@ -7,15 +7,15 @@ extern crate syn;
7
7
  extern crate walkdir;
8
8
 
9
9
  use proc_macro::TokenStream;
10
- use syn::*;
11
10
  use quote::Tokens;
12
11
  use std::path::Path;
12
+ use syn::*;
13
13
 
14
14
  #[cfg(debug_assertions)]
15
15
  fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
16
16
  quote!{
17
17
  impl #ident {
18
- pub fn get(file_path: &str) -> Option<Vec<u8>> {
18
+ pub fn get(file_path: &str) -> Option<impl AsRef<[u8]>> {
19
19
  use std::fs::File;
20
20
  use std::io::Read;
21
21
  use std::path::Path;
@@ -63,13 +63,13 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
63
63
  let key = if std::path::MAIN_SEPARATOR == '\\' { key.replace('\\', "/") } else { key };
64
64
  let canonical_path_str = canonical_path.to_str();
65
65
  let value = quote!{
66
- #key => Some(include_bytes!(#canonical_path_str).to_vec()),
66
+ #key => Some(&include_bytes!(#canonical_path_str)[..]),
67
67
  };
68
68
  values.push(value);
69
69
  }
70
70
  quote!{
71
71
  impl #ident {
72
- pub fn get(file_path: &str) -> Option<Vec<u8>> {
72
+ pub fn get(file_path: &str) -> Option<impl AsRef<[u8]>> {
73
73
  match file_path {
74
74
  #(#values)*
75
75
  _ => None,
@@ -115,7 +115,11 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
115
115
  }
116
116
  };
117
117
  if !Path::new(&folder_path).exists() {
118
+ panic!(
118
- panic!("#[derive(RustEmbed)] folder '{}' does not exist. cwd: '{}'", folder_path, std::env::current_dir().unwrap().to_str().unwrap());
119
+ "#[derive(RustEmbed)] folder '{}' does not exist. cwd: '{}'",
120
+ folder_path,
121
+ std::env::current_dir().unwrap().to_str().unwrap()
122
+ );
119
123
  };
120
124
  generate_assets(ident, folder_path)
121
125
  }