Skip to content

Commit

Permalink
Document SCALE macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neopallium committed Nov 27, 2024
1 parent 2df9cf7 commit 19437b6
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions rust/tw_scale/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ macro_rules! replace_expr {
};
}

/// Macro to implement `ToScale` trait for a struct.
///
/// # Example
/// ```rust
/// use tw_scale::{impl_struct_scale, ToScale};
///
/// impl_struct_scale!(
/// #[derive(Debug, Default, Clone, Ord, PartialOrd, Eq, PartialEq)]
/// pub struct TestStruct {
/// id: u8,
/// id2: u8,
/// data: Vec<u8>,
/// }
/// );
/// ```
#[macro_export]
macro_rules! impl_struct_scale {
// "New Type" struct
Expand Down Expand Up @@ -58,6 +73,26 @@ macro_rules! impl_struct_scale {
}
}

/// Macro to implement `ToScale` trait for an enum.
///
/// # Example
/// ```rust
/// use tw_scale::{impl_enum_scale, ToScale};
///
/// impl_enum_scale!(
/// #[derive(Debug, Default, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
/// pub enum TestEnum {
/// #[default]
/// Variant0 = 0,
/// Variant1(u8) = 1,
/// Variant10 = 10,
/// StructVariant {
/// id: u8,
/// id2: u8,
/// } = 11,
/// }
/// );
/// ```
#[macro_export]
macro_rules! impl_enum_scale {
(
Expand All @@ -72,7 +107,6 @@ macro_rules! impl_enum_scale {
})? = $variant_index:expr,
)*
}
$($rest:tt)*
) => {
$(#[$enum_meta])*
#[repr(u8)]
Expand Down Expand Up @@ -119,8 +153,10 @@ mod tests {
use crate::ToScale;

impl_struct_scale!(
/// Test struct.
#[derive(Debug, Default, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct TestStruct {
/// Field id.
id: u8,
id2: u8,
data: Vec<u8>,
Expand All @@ -141,8 +177,9 @@ mod tests {
}

impl_struct_scale!(
/// Test new type struct.
#[derive(Debug, Default, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct TestNewType(u8);
pub struct TestNewType(pub u8);
);

#[test]
Expand All @@ -151,16 +188,18 @@ mod tests {
}

impl_enum_scale!(
/// Test enum.
#[derive(Debug, Default, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum TestEnum {
/// Default variant.
#[default]
Variant0 = 0,
Variant1(u8) = 1,
/// Tuple variant.
Variant1(u8) = 0x01,
/// Variant with index 10.
Variant10 = 10,
Struct {
id: u8,
id2: u8,
} = 11,
/// Struct variant.
Struct { id: u8, id2: u8 } = 11,
}
);

Expand Down

0 comments on commit 19437b6

Please sign in to comment.