Skip to content

Commit 536c54c

Browse files
authored
Document the #[module] macro (#135)
* Document the #[module] macro * Obey the fmt * Review feedback * More docs * Tweak wording * tweak wording * whitespace * Tweak docstring
1 parent c6350fc commit 536c54c

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

proc-macro/src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,58 @@ use synstructure::{
3030
Structure,
3131
};
3232

33+
/// Register type sizes for [EventsDecoder](struct.EventsDecoder.html) and set the `MODULE`.
34+
///
35+
/// The `module` macro registers the type sizes of the associated types of a trait so that [EventsDecoder](struct.EventsDecoder.html)
36+
/// can decode events of that type when received from Substrate. It also sets the `MODULE` constant
37+
/// to the name of the trait (must match the name of the Substrate pallet) that enables the [Call](), [Event]() and [Store]() macros to work.
38+
///
39+
/// If you do not want an associated type to be registered, likely because you never expect it as part of a response payload to be decoded, use `#[module(ignore)]` on the type.
40+
///
41+
/// Example:
42+
///
43+
/// ```ignore
44+
/// #[module]
45+
/// pub trait Herd: Husbandry {
46+
/// type Hooves: HoofCounter;
47+
/// type Wool: WoollyAnimal;
48+
/// #[module(ignore)]
49+
/// type Digestion: EnergyProducer + std::fmt::Debug;
50+
/// }
51+
/// ```
52+
///
53+
/// The above will produce the following code:
54+
///
55+
/// ```ignore
56+
/// pub trait Herd: Husbandry {
57+
/// type Hooves: HoofCounter;
58+
/// type Wool: WoollyAnimal;
59+
/// #[module(ignore)]
60+
/// type Digestion: EnergyProducer + std::fmt::Debug;
61+
/// }
62+
///
63+
/// const MODULE: &str = "Herd";
64+
///
65+
/// // `EventsDecoder` extension trait.
66+
/// pub trait HerdEventsDecoder {
67+
/// // Registers this modules types.
68+
/// fn with_herd(&mut self);
69+
/// }
70+
///
71+
/// impl<T: Herd> HerdEventsDecoder for
72+
/// substrate_subxt::EventsDecoder<T>
73+
/// {
74+
/// fn with_herd(&mut self) {
75+
/// self.with_husbandry();
76+
/// self.register_type_size::<T::Hooves>("Hooves");
77+
/// self.register_type_size::<T::Wool>("Wool");
78+
/// }
79+
/// }
80+
/// ```
81+
///
82+
/// The following type sizes are registered by default: `bool, u8, u32, AccountId, AccountIndex,
83+
/// AuthorityId, AuthorityIndex, AuthorityWeight, BlockNumber, DispatchInfo, Hash, Kind,
84+
/// MemberCount, PhantomData, PropIndex, ProposalIndex, ReferendumIndex, SessionIndex, VoteThreshold`
3385
#[proc_macro_attribute]
3486
#[proc_macro_error]
3587
pub fn module(args: TokenStream, input: TokenStream) -> TokenStream {

proc-macro/src/module.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn events_decoder_trait_name(module: &syn::Ident) -> syn::Ident {
6969
fn with_module_ident(module: &syn::Ident) -> syn::Ident {
7070
format_ident!("with_{}", module.to_string().to_snake_case())
7171
}
72-
72+
/// Attribute macro that registers the type sizes used by the module; also sets the `MODULE` constant.
7373
pub fn module(_args: TokenStream, tokens: TokenStream) -> TokenStream {
7474
let input: Result<syn::ItemTrait, _> = syn::parse2(tokens.clone());
7575
let input = if let Ok(input) = input {
@@ -187,4 +187,46 @@ mod tests {
187187
let result = module(attr, input);
188188
utils::assert_proc_macro(result, expected);
189189
}
190+
191+
#[test]
192+
fn test_herd() {
193+
let attr = quote!(#[module]);
194+
let input = quote! {
195+
pub trait Herd: Husbandry {
196+
type Hoves: u8;
197+
type Wool: bool;
198+
#[module(ignore)]
199+
type Digestion: EnergyProducer + fmt::Debug;
200+
}
201+
};
202+
let expected = quote! {
203+
pub trait Herd: Husbandry {
204+
type Hoves: u8;
205+
type Wool: bool;
206+
#[module(ignore)]
207+
type Digestion: EnergyProducer + fmt::Debug;
208+
}
209+
210+
const MODULE: &str = "Herd";
211+
212+
/// `EventsDecoder` extension trait.
213+
pub trait HerdEventsDecoder {
214+
/// Registers this modules types.
215+
fn with_herd(&mut self);
216+
}
217+
218+
impl<T: Herd> HerdEventsDecoder for
219+
substrate_subxt::EventsDecoder<T>
220+
{
221+
fn with_herd(&mut self) {
222+
self.with_husbandry();
223+
self.register_type_size::<T::Hoves>("Hoves");
224+
self.register_type_size::<T::Wool>("Wool");
225+
}
226+
}
227+
};
228+
229+
let result = module(attr, input);
230+
utils::assert_proc_macro(result, expected);
231+
}
190232
}

src/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct RawEvent {
5959
pub data: Vec<u8>,
6060
}
6161

62-
/// Event decoder.
62+
/// Events decoder.
6363
#[derive(Debug)]
6464
pub struct EventsDecoder<T> {
6565
metadata: Metadata,

0 commit comments

Comments
 (0)