Expand description
§MIDDS V2 Codegen - Procedural Macro for Dual-Mode Type Generation
This crate provides the runtime_midds procedural macro that enables automatic
transformation of Rust data structures between std and Substrate runtime modes.
§Overview
The core functionality revolves around the #[runtime_midds] attribute macro that:
- Generates two versions of each annotated type (std and runtime)
- Automatically transforms
StringandVec<T>fields toBoundedVecin runtime mode - Adds appropriate trait derivations for each compilation mode
- Supports complex nested structures and enums
§Key Features
§Type Transformations
String→BoundedVec<u8, ConstU32<N>>Vec<T>→BoundedVec<T, ConstU32<N>>Option<String>→Option<BoundedVec<u8, ConstU32<N>>>Option<Vec<T>>→Option<BoundedVec<T, ConstU32<N>>>- Recursive transformation for nested
Optiontypes
§Bound Specification
Use #[runtime_bound(N)] attributes to specify maximum sizes:
- On struct fields for field-level bounds
- On enum variants for variant-level bounds (applies to all fields in that variant)
§Trait Derivations
- Runtime mode:
Encode,Decode,DecodeWithMemTracking,TypeInfo,MaxEncodedLen,Debug,Clone,PartialEq,Eq - Std mode:
Debug,Clone,PartialEq,Eq
§Usage Examples
§Basic Struct
use allfeat_midds_v2_codegen::runtime_midds;
#[runtime_midds]
pub struct MyStruct {
#[runtime_bound(256)]
pub title: String,
#[runtime_bound(64)]
pub tags: Vec<String>,
pub id: u64, // No transformation
}§Newtype Struct
use allfeat_midds_v2_codegen::runtime_midds;
#[runtime_midds]
pub struct Identifier(#[runtime_bound(32)] String);§Enum with Bounds
use allfeat_midds_v2_codegen::runtime_midds;
#[runtime_midds]
pub enum WorkType {
Original,
#[runtime_bound(512)]
Medley(Vec<u64>),
#[runtime_bound(256)]
Adaptation(String, u32),
}§Optional Fields
use allfeat_midds_v2_codegen::runtime_midds;
#[runtime_midds]
pub struct OptionalData {
#[runtime_bound(128)]
pub optional_title: Option<String>,
#[runtime_bound(32)]
pub optional_list: Option<Vec<u32>>,
}§Architecture
The crate is organized into several modules:
- [
attribute] - Parsing and validation of#[runtime_bound(N)]attributes - [
transform] - Type transformation logic between std and runtime modes - [
generate] - Code generation utilities for structs and enums - [
error] - Comprehensive error handling with detailed diagnostics
Attribute Macros§
- runtime_
midds - Attribute macro that transforms String and Vec
fields to BoundedVec when runtime feature is enabled.