-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
runtime-sdk: Implement automatic Module derivation
- Loading branch information
Showing
12 changed files
with
104 additions
and
67 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
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,54 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
/// Deriver for the `Module` trait. | ||
pub struct DeriveModule { | ||
/// Items specifying the module configuration. | ||
module_cfg: Vec<syn::ImplItem>, | ||
} | ||
|
||
impl DeriveModule { | ||
pub fn new() -> Box<Self> { | ||
Box::new(Self { module_cfg: vec![] }) | ||
} | ||
} | ||
|
||
impl super::Deriver for DeriveModule { | ||
fn preprocess(&mut self, item: syn::ImplItem) -> Option<syn::ImplItem> { | ||
match item { | ||
syn::ImplItem::Type(ref ty) => { | ||
match ty.ident.to_string().as_str() { | ||
"Error" | "Event" | "Parameters" => { | ||
self.module_cfg.push(item); | ||
None // Take the item. | ||
} | ||
_ => Some(item), // Return the item. | ||
} | ||
} | ||
syn::ImplItem::Const(ref cnst) => { | ||
match cnst.ident.to_string().as_str() { | ||
"NAME" | "VERSION" => { | ||
self.module_cfg.push(item); | ||
None // Take the item. | ||
} | ||
_ => Some(item), // Return the item. | ||
} | ||
} | ||
_ => Some(item), // Return the item. | ||
} | ||
} | ||
|
||
fn derive(&mut self, generics: &syn::Generics, ty: &Box<syn::Type>) -> TokenStream { | ||
if self.module_cfg.is_empty() { | ||
return quote! {}; | ||
} | ||
let module_cfg = &self.module_cfg; | ||
|
||
quote! { | ||
#[automatically_derived] | ||
impl #generics sdk::module::Module for #ty { | ||
#(#module_cfg)* | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
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