Document the #[module] macro (#135)

* Document the #[module] macro

* Obey the fmt

* Review feedback

* More docs

* Tweak wording

* tweak wording

* whitespace

* Tweak docstring
This commit is contained in:
David
2020-07-07 09:19:44 +02:00
committed by GitHub
parent c6350fcc11
commit 536c54c843
3 changed files with 96 additions and 2 deletions
+43 -1
View File
@@ -69,7 +69,7 @@ fn events_decoder_trait_name(module: &syn::Ident) -> syn::Ident {
fn with_module_ident(module: &syn::Ident) -> syn::Ident {
format_ident!("with_{}", module.to_string().to_snake_case())
}
/// Attribute macro that registers the type sizes used by the module; also sets the `MODULE` constant.
pub fn module(_args: TokenStream, tokens: TokenStream) -> TokenStream {
let input: Result<syn::ItemTrait, _> = syn::parse2(tokens.clone());
let input = if let Ok(input) = input {
@@ -187,4 +187,46 @@ mod tests {
let result = module(attr, input);
utils::assert_proc_macro(result, expected);
}
#[test]
fn test_herd() {
let attr = quote!(#[module]);
let input = quote! {
pub trait Herd: Husbandry {
type Hoves: u8;
type Wool: bool;
#[module(ignore)]
type Digestion: EnergyProducer + fmt::Debug;
}
};
let expected = quote! {
pub trait Herd: Husbandry {
type Hoves: u8;
type Wool: bool;
#[module(ignore)]
type Digestion: EnergyProducer + fmt::Debug;
}
const MODULE: &str = "Herd";
/// `EventsDecoder` extension trait.
pub trait HerdEventsDecoder {
/// Registers this modules types.
fn with_herd(&mut self);
}
impl<T: Herd> HerdEventsDecoder for
substrate_subxt::EventsDecoder<T>
{
fn with_herd(&mut self) {
self.with_husbandry();
self.register_type_size::<T::Hoves>("Hoves");
self.register_type_size::<T::Wool>("Wool");
}
}
};
let result = module(attr, input);
utils::assert_proc_macro(result, expected);
}
}