~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. impl/src/lib.rs +12 -18
  2. tests/lib.rs +1 -0
impl/src/lib.rs CHANGED
@@ -83,7 +83,7 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
83
83
  }
84
84
  }
85
85
 
86
- fn help() {
86
+ fn help() -> ! {
87
87
  panic!("#[derive(RustEmbed)] should contain one attribute like this #[folder = \"examples/public/\"]");
88
88
  }
89
89
 
@@ -95,22 +95,14 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
95
95
  _ => {}
96
96
  },
97
97
  };
98
+
98
- let ident = &ast.ident;
99
+ let attribute = ast.attrs
99
- if ast.attrs.len() == 0 || ast.attrs.len() > 1 {
100
- help();
100
+ .iter()
101
- }
102
- let value = &ast.attrs[0].value;
101
+ .map(|attr| &attr.value)
102
+ .find(|value| value.name() == "folder");
103
- let literal_value = match value {
103
+ let literal_value = match attribute {
104
- &MetaItem::NameValue(ref attr_name, ref value) => {
104
+ Some(&MetaItem::NameValue(_, ref literal)) => literal,
105
- if attr_name == "folder" {
106
- value
107
- } else {
108
- panic!("#[derive(RustEmbed)] attribute name must be folder");
109
- }
110
- }
111
- _ => {
105
+ _ => help()
112
- panic!("#[derive(RustEmbed)] attribute name must be folder");
113
- }
114
106
  };
115
107
  let folder_path = match literal_value {
116
108
  &Lit::Str(ref val, _) => val.clone(),
@@ -118,6 +110,7 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
118
110
  panic!("#[derive(RustEmbed)] attribute value must be a string literal");
119
111
  }
120
112
  };
113
+
121
114
  if !Path::new(&folder_path).exists() {
122
115
  panic!(
123
116
  "#[derive(RustEmbed)] folder '{}' does not exist. cwd: '{}'",
@@ -125,7 +118,8 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
125
118
  std::env::current_dir().unwrap().to_str().unwrap()
126
119
  );
127
120
  };
121
+
128
- generate_assets(ident, folder_path)
122
+ generate_assets(&ast.ident, folder_path)
129
123
  }
130
124
 
131
125
  #[proc_macro_derive(RustEmbed, attributes(folder))]
tests/lib.rs CHANGED
@@ -1,6 +1,7 @@
1
1
  #[macro_use]
2
2
  extern crate rust_embed;
3
3
 
4
+ /// Test doc comment
4
5
  #[derive(RustEmbed)]
5
6
  #[folder = "examples/public/"]
6
7
  struct Asset;