mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 13:57:58 +00:00
Make sr-api-macros and srml-support renaming of crates (#1757)
Procedural do not support `$crate` to get access to the crate where the macro is defined. We use a hack to re-export the crate under a known name. With rust edition 2018, people started to rename their crates in `Cargo.toml`, but that breaks the re-export. This commit introduces `proc-maco-crate` that supports finding the requested crate name, even if it was renamed.
This commit is contained in:
@@ -18,11 +18,12 @@
|
||||
//! Proc macro helpers for procedural macros
|
||||
// end::description[]
|
||||
|
||||
extern crate proc_macro;
|
||||
|
||||
// reexport proc macros
|
||||
pub use srml_support_procedural_tools_derive::*;
|
||||
pub use quote;
|
||||
|
||||
use proc_macro_crate::crate_name;
|
||||
use syn::parse::Error;
|
||||
use quote::quote;
|
||||
|
||||
pub mod syn_ext;
|
||||
|
||||
@@ -50,7 +51,6 @@ macro_rules! custom_keyword {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// FIXME #1569, remove the following functions, which are copied from sr-api-macros
|
||||
use proc_macro2::{TokenStream, Span};
|
||||
use syn::Ident;
|
||||
@@ -59,7 +59,7 @@ fn generate_hidden_includes_mod_name(unique_id: &str) -> Ident {
|
||||
Ident::new(&format!("sr_api_hidden_includes_{}", unique_id), Span::call_site())
|
||||
}
|
||||
|
||||
/// Generates the access to the `subtrate_client` crate.
|
||||
/// Generates the access to the `srml-support` crate.
|
||||
pub fn generate_crate_access(unique_id: &str, def_crate: &str) -> TokenStream {
|
||||
if ::std::env::var("CARGO_PKG_NAME").unwrap() == def_crate {
|
||||
quote::quote!( crate )
|
||||
@@ -70,18 +70,28 @@ pub fn generate_crate_access(unique_id: &str, def_crate: &str) -> TokenStream {
|
||||
}
|
||||
|
||||
/// Generates the hidden includes that are required to make the macro independent from its scope.
|
||||
pub fn generate_hidden_includes(unique_id: &str, def_crate: &str, crate_id: &str) -> TokenStream {
|
||||
let crate_id = Ident::new(crate_id, Span::call_site());
|
||||
pub fn generate_hidden_includes(unique_id: &str, def_crate: &str) -> TokenStream {
|
||||
if ::std::env::var("CARGO_PKG_NAME").unwrap() == def_crate {
|
||||
TokenStream::new()
|
||||
} else {
|
||||
let mod_name = generate_hidden_includes_mod_name(unique_id);
|
||||
quote::quote!(
|
||||
#[doc(hidden)]
|
||||
mod #mod_name {
|
||||
pub extern crate #crate_id as hidden_include;
|
||||
|
||||
match crate_name(def_crate) {
|
||||
Ok(name) => {
|
||||
let name = Ident::new(&name, Span::call_site());
|
||||
quote::quote!(
|
||||
#[doc(hidden)]
|
||||
mod #mod_name {
|
||||
pub extern crate #name as hidden_include;
|
||||
}
|
||||
)
|
||||
},
|
||||
Err(e) => {
|
||||
let err = Error::new(Span::call_site(), &e).to_compile_error();
|
||||
quote!( #err )
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
}.into()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user