~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/Cargo.toml +2 -2
  2. impl/src/lib.rs +22 -17
impl/Cargo.toml CHANGED
@@ -16,8 +16,8 @@ proc-macro = true
16
16
  [dependencies]
17
17
  rust-embed-utils = { version = "5.0.0", path = "../utils"}
18
18
 
19
- syn = "0.11"
19
+ syn = "1"
20
- quote = "0.3"
20
+ quote = "1"
21
21
  walkdir = "2.2.7"
22
22
 
23
23
  [dependencies.shellexpand]
impl/src/lib.rs CHANGED
@@ -9,12 +9,11 @@ extern crate shellexpand;
9
9
  extern crate walkdir;
10
10
 
11
11
  use proc_macro::TokenStream;
12
- use quote::Tokens;
13
12
  use std::path::Path;
14
- use syn::*;
13
+ use syn::{export::TokenStream2, Data, DeriveInput, Fields, Lit, Meta};
15
14
 
16
15
  #[cfg(all(debug_assertions, not(feature = "debug-embed")))]
17
- fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
16
+ fn generate_assets(ident: &syn::Ident, folder_path: String) -> TokenStream2 {
18
17
  quote! {
19
18
  impl #ident {
20
19
  pub fn get(file_path: &str) -> Option<std::borrow::Cow<'static, [u8]>> {
@@ -56,10 +55,10 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
56
55
  }
57
56
 
58
57
  #[cfg(any(not(debug_assertions), feature = "debug-embed"))]
59
- fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
58
+ fn generate_assets(ident: &syn::Ident, folder_path: String) -> TokenStream2 {
60
59
  extern crate rust_embed_utils;
61
60
 
62
- let mut match_values = Vec::<Tokens>::new();
61
+ let mut match_values = Vec::<TokenStream2>::new();
63
62
  let mut list_values = Vec::<String>::new();
64
63
 
65
64
  for rust_embed_utils::FileEntry { rel_path, full_canonical_path } in rust_embed_utils::get_files(folder_path) {
@@ -102,22 +101,29 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
102
101
  }
103
102
  }
104
103
 
105
- fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
104
+ fn impl_rust_embed(ast: &syn::DeriveInput) -> TokenStream2 {
106
- match ast.body {
105
+ match ast.data {
107
- Body::Enum(_) => panic!("RustEmbed cannot be derived for enums"),
108
- Body::Struct(ref data) => match data {
106
+ Data::Struct(ref data) => match data.fields {
109
- &VariantData::Unit => {}
107
+ Fields::Unit => {}
110
108
  _ => panic!("RustEmbed can only be derived for unit structs"),
111
109
  },
110
+ _ => panic!("RustEmbed can only be derived for unit structs"),
112
111
  };
113
112
 
113
+ let attribute = ast
114
+ .attrs
115
+ .iter()
116
+ .find(|value| value.path.is_ident("folder"))
114
- let attribute = ast.attrs.iter().map(|attr| &attr.value).find(|value| value.name() == "folder");
117
+ .expect("#[derive(RustEmbed)] should contain one attribute like this #[folder = \"examples/public/\"]");
118
+ let meta = attribute
119
+ .parse_meta()
120
+ .expect("#[derive(RustEmbed)] should contain one attribute like this #[folder = \"examples/public/\"]");
115
- let literal_value = match attribute {
121
+ let literal_value = match meta {
116
- Some(&MetaItem::NameValue(_, ref literal)) => literal,
122
+ Meta::NameValue(ref data) => &data.lit,
117
123
  _ => panic!("#[derive(RustEmbed)] should contain one attribute like this #[folder = \"examples/public/\"]"),
118
124
  };
119
125
  let folder_path = match literal_value {
120
- &Lit::Str(ref val, _) => val.clone(),
126
+ Lit::Str(ref val) => val.clone().value(),
121
127
  _ => {
122
128
  panic!("#[derive(RustEmbed)] attribute value must be a string literal");
123
129
  }
@@ -148,8 +154,7 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
148
154
 
149
155
  #[proc_macro_derive(RustEmbed, attributes(folder))]
150
156
  pub fn derive_input_object(input: TokenStream) -> TokenStream {
151
- let s = input.to_string();
152
- let ast = syn::parse_derive_input(&s).unwrap();
157
+ let ast: DeriveInput = syn::parse(input).unwrap();
153
158
  let gen = impl_rust_embed(&ast);
154
- gen.parse().unwrap()
159
+ gen.into()
155
160
  }