-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc3b682
commit 4b464cc
Showing
2 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use inflector::Inflector; | ||
use proc_macro2::{Span, TokenStream as TokenStream2}; | ||
use proc_macro_crate::{crate_name, FoundCrate}; | ||
use syn::{spanned::Spanned, Error, Ident, ItemImpl, Path, Result}; | ||
/// Should a qualified trait path be required? | ||
/// | ||
/// e.g. `path::Trait` is qualified and `Trait` is not. | ||
pub enum RequireQualifiedTraitPath { | ||
Yes, | ||
No, | ||
} | ||
/// Extract the trait that is implemented by the given `ItemImpl`. | ||
pub fn extract_impl_trait(impl_: &ItemImpl, require: RequireQualifiedTraitPath) -> Result<&Path> { | ||
impl_ | ||
.trait_ | ||
.as_ref() | ||
.map(|v| &v.1) | ||
.ok_or_else(|| Error::new(impl_.span(), "Only implementation of traits are supported!")) | ||
.and_then(|p| { | ||
if p.segments.len() > 1 || matches!(require, RequireQualifiedTraitPath::No) { | ||
Ok(p) | ||
} else { | ||
Err(Error::new( | ||
p.span(), | ||
"The implemented trait has to be referenced with a path, \ | ||
e.g. `impl xcq_extension_core::ExtensionCore for Runtime`.", | ||
)) | ||
} | ||
}) | ||
} | ||
|
||
/// Generates the name of the module that contains the trait declaration for the runtime. | ||
pub fn generate_runtime_mod_name_for_trait(trait_: &Ident) -> Ident { | ||
Ident::new( | ||
&format!("runtime_decl_for_{}", trait_.to_string().to_snake_case()), | ||
Span::call_site(), | ||
) | ||
} | ||
|
||
/// Generate the crate access for the crate using 2018 syntax. | ||
/// | ||
/// If `frame` is in scope, it will use `polkadot_sdk_frame::deps::<def_crate>`. Else, it will try | ||
/// and find `<def_crate>` directly. | ||
pub fn generate_access_from_frame_or_crate(def_crate: &str) -> Result<syn::Path, Error> { | ||
if let Some(path) = get_frame_crate_path(def_crate) { | ||
Ok(path) | ||
} else if let Some(path) = get_sdk_crate_path(def_crate) { | ||
Ok(path) | ||
} else { | ||
let ident = match crate_name(def_crate) { | ||
Ok(FoundCrate::Itself) => { | ||
let name = def_crate.to_string().replace("-", "_"); | ||
Ok(syn::Ident::new(&name, Span::call_site())) | ||
} | ||
Ok(FoundCrate::Name(name)) => Ok(Ident::new(&name, Span::call_site())), | ||
Err(e) => Err(Error::new(Span::call_site(), e)), | ||
}?; | ||
|
||
Ok(syn::Path::from(ident)) | ||
} | ||
} | ||
|
||
/// Generates the path to the frame crate deps. | ||
fn get_frame_crate_path(def_crate: &str) -> Option<syn::Path> { | ||
// This does not work if the frame crate is renamed. | ||
if let Ok(FoundCrate::Name(name)) = crate_name(&"polkadot-sdk-frame").or_else(|_| crate_name(&"frame")) { | ||
let path = format!("{}::deps::{}", name, def_crate.to_string().replace("-", "_")); | ||
Some(syn::parse_str::<syn::Path>(&path).expect("is a valid path; qed")) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn get_sdk_crate_path(def_crate: &str) -> Option<syn::Path> { | ||
if let Ok(FoundCrate::Name(name)) = crate_name(&"polkadot-sdk") { | ||
let path = format!("{}::{}", name, def_crate.to_string()).replace("-", "_"); | ||
Some(syn::parse_str::<syn::Path>(&path).expect("is a valid path; qed")) | ||
} else { | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters