@@ -16,6 +16,7 @@ use syn::{parse_macro_input, Data, DeriveInput, Expr, ExprLit, Fields, Lit, Meta
16
16
17
17
fn embedded (
18
18
ident : & syn:: Ident , relative_folder_path : Option < & str > , absolute_folder_path : String , prefix : Option < & str > , includes : & [ String ] , excludes : & [ String ] ,
19
+ crate_path : & syn:: Path ,
19
20
) -> syn:: Result < TokenStream2 > {
20
21
extern crate rust_embed_utils;
21
22
@@ -57,9 +58,9 @@ fn embedded(
57
58
}
58
59
} ) ;
59
60
let value_type = if cfg ! ( feature = "compression" ) {
60
- quote ! { fn ( ) -> rust_embed :: EmbeddedFile }
61
+ quote ! { fn ( ) -> #crate_path :: EmbeddedFile }
61
62
} else {
62
- quote ! { rust_embed :: EmbeddedFile }
63
+ quote ! { #crate_path :: EmbeddedFile }
63
64
} ;
64
65
let get_value = if cfg ! ( feature = "compression" ) {
65
66
quote ! { |idx| ( ENTRIES [ idx] . 1 ) ( ) }
@@ -70,7 +71,7 @@ fn embedded(
70
71
#not_debug_attr
71
72
impl #ident {
72
73
/// Get an embedded file and its metadata.
73
- pub fn get( file_path: & str ) -> :: std:: option:: Option <rust_embed :: EmbeddedFile > {
74
+ pub fn get( file_path: & str ) -> :: std:: option:: Option <#crate_path :: EmbeddedFile > {
74
75
#handle_prefix
75
76
let key = file_path. replace( "\\ " , "/" ) ;
76
77
const ENTRIES : & ' static [ ( & ' static str , #value_type) ] = & [
@@ -92,18 +93,18 @@ fn embedded(
92
93
}
93
94
94
95
#not_debug_attr
95
- impl rust_embed :: RustEmbed for #ident {
96
- fn get( file_path: & str ) -> :: std:: option:: Option <rust_embed :: EmbeddedFile > {
96
+ impl #crate_path :: RustEmbed for #ident {
97
+ fn get( file_path: & str ) -> :: std:: option:: Option <#crate_path :: EmbeddedFile > {
97
98
#ident:: get( file_path)
98
99
}
99
- fn iter( ) -> rust_embed :: Filenames {
100
- rust_embed :: Filenames :: Embedded ( #ident:: names( ) )
100
+ fn iter( ) -> #crate_path :: Filenames {
101
+ #crate_path :: Filenames :: Embedded ( #ident:: names( ) )
101
102
}
102
103
}
103
104
} )
104
105
}
105
106
106
- fn dynamic ( ident : & syn:: Ident , folder_path : String , prefix : Option < & str > , includes : & [ String ] , excludes : & [ String ] ) -> TokenStream2 {
107
+ fn dynamic ( ident : & syn:: Ident , folder_path : String , prefix : Option < & str > , includes : & [ String ] , excludes : & [ String ] , crate_path : & syn :: Path ) -> TokenStream2 {
107
108
let ( handle_prefix, map_iter) = if let :: std:: option:: Option :: Some ( prefix) = prefix {
108
109
(
109
110
quote ! { let file_path = file_path. strip_prefix( #prefix) ?; } ,
@@ -128,7 +129,7 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includ
128
129
#[ cfg( debug_assertions) ]
129
130
impl #ident {
130
131
/// Get an embedded file and its metadata.
131
- pub fn get( file_path: & str ) -> :: std:: option:: Option <rust_embed :: EmbeddedFile > {
132
+ pub fn get( file_path: & str ) -> :: std:: option:: Option <#crate_path :: EmbeddedFile > {
132
133
#handle_prefix
133
134
134
135
#declare_includes
@@ -144,8 +145,8 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includ
144
145
return :: std:: option:: Option :: None ;
145
146
}
146
147
147
- if rust_embed :: utils:: is_path_included( & rel_file_path, INCLUDES , EXCLUDES ) {
148
- rust_embed :: utils:: read_file_from_fs( & canonical_file_path) . ok( )
148
+ if #crate_path :: utils:: is_path_included( & rel_file_path, INCLUDES , EXCLUDES ) {
149
+ #crate_path :: utils:: read_file_from_fs( & canonical_file_path) . ok( )
149
150
} else {
150
151
:: std:: option:: Option :: None
151
152
}
@@ -158,26 +159,27 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includ
158
159
#declare_includes
159
160
#declare_excludes
160
161
161
- rust_embed :: utils:: get_files( :: std:: string:: String :: from( #folder_path) , INCLUDES , EXCLUDES )
162
+ #crate_path :: utils:: get_files( :: std:: string:: String :: from( #folder_path) , INCLUDES , EXCLUDES )
162
163
. map( |e| #map_iter)
163
164
}
164
165
}
165
166
166
167
#[ cfg( debug_assertions) ]
167
- impl rust_embed :: RustEmbed for #ident {
168
- fn get( file_path: & str ) -> :: std:: option:: Option <rust_embed :: EmbeddedFile > {
168
+ impl #crate_path :: RustEmbed for #ident {
169
+ fn get( file_path: & str ) -> :: std:: option:: Option <#crate_path :: EmbeddedFile > {
169
170
#ident:: get( file_path)
170
171
}
171
- fn iter( ) -> rust_embed :: Filenames {
172
+ fn iter( ) -> #crate_path :: Filenames {
172
173
// the return type of iter() is unnamable, so we have to box it
173
- rust_embed :: Filenames :: Dynamic ( :: std:: boxed:: Box :: new( #ident:: iter( ) ) )
174
+ #crate_path :: Filenames :: Dynamic ( :: std:: boxed:: Box :: new( #ident:: iter( ) ) )
174
175
}
175
176
}
176
177
}
177
178
}
178
179
179
180
fn generate_assets (
180
181
ident : & syn:: Ident , relative_folder_path : Option < & str > , absolute_folder_path : String , prefix : Option < String > , includes : Vec < String > , excludes : Vec < String > ,
182
+ crate_path : & syn:: Path ,
181
183
) -> syn:: Result < TokenStream2 > {
182
184
let embedded_impl = embedded (
183
185
ident,
@@ -186,12 +188,13 @@ fn generate_assets(
186
188
prefix. as_deref ( ) ,
187
189
& includes,
188
190
& excludes,
191
+ crate_path,
189
192
) ;
190
193
if cfg ! ( feature = "debug-embed" ) {
191
194
return embedded_impl;
192
195
}
193
196
let embedded_impl = embedded_impl?;
194
- let dynamic_impl = dynamic ( ident, absolute_folder_path, prefix. as_deref ( ) , & includes, & excludes) ;
197
+ let dynamic_impl = dynamic ( ident, absolute_folder_path, prefix. as_deref ( ) , & includes, & excludes, crate_path ) ;
195
198
196
199
Ok ( quote ! {
197
200
#embedded_impl
@@ -273,6 +276,11 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> syn::Result<TokenStream2> {
273
276
_ => return Err ( syn:: Error :: new_spanned ( ast, "RustEmbed can only be derived for unit structs" ) ) ,
274
277
} ;
275
278
279
+ let crate_path: syn:: Path = find_attribute_values ( ast, "crate_path" )
280
+ . last ( )
281
+ . map ( |v| syn:: parse_str ( & v) . unwrap ( ) )
282
+ . unwrap_or_else ( || syn:: parse_str ( "rust_embed" ) . unwrap ( ) ) ;
283
+
276
284
let mut folder_paths = find_attribute_values ( ast, "folder" ) ;
277
285
if folder_paths. len ( ) != 1 {
278
286
return Err ( syn:: Error :: new_spanned (
@@ -331,10 +339,18 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> syn::Result<TokenStream2> {
331
339
return Err ( syn:: Error :: new_spanned ( ast, message) ) ;
332
340
} ;
333
341
334
- generate_assets ( & ast. ident , relative_path. as_deref ( ) , absolute_folder_path, prefix, includes, excludes)
342
+ generate_assets (
343
+ & ast. ident ,
344
+ relative_path. as_deref ( ) ,
345
+ absolute_folder_path,
346
+ prefix,
347
+ includes,
348
+ excludes,
349
+ & crate_path,
350
+ )
335
351
}
336
352
337
- #[ proc_macro_derive( RustEmbed , attributes( folder, prefix, include, exclude) ) ]
353
+ #[ proc_macro_derive( RustEmbed , attributes( folder, prefix, include, exclude, crate_path ) ) ]
338
354
pub fn derive_input_object ( input : TokenStream ) -> TokenStream {
339
355
let ast = parse_macro_input ! ( input as DeriveInput ) ;
340
356
match impl_rust_embed ( & ast) {
0 commit comments