| Crates.io | allfeat-midds-v2-codegen |
| lib.rs | allfeat-midds-v2-codegen |
| version | 0.1.1 |
| created_at | 2025-08-10 21:39:07.87796+00 |
| updated_at | 2025-08-14 22:36:52.899534+00 |
| description | Procedural macros for MIDDS v2 types generation |
| homepage | https://allfeat.org |
| repository | https://github.com/Allfeat/allfeat-sdk |
| max_upload_size | |
| id | 1789323 |
| size | 90,508 |
A Rust procedural macro crate that enables automatic transformation of data structures between std Rust types and Substrate runtime types.
The runtime_midds macro is the core of the MIDDS V2 dual-mode compilation system. It automatically generates two versions of your data structures:
String, Vec<T>)BoundedVec)✨ Automatic Type Transformation
String → BoundedVec<u8, ConstU32<N>>Vec<T> → BoundedVec<T, ConstU32<N>>Option<T> wrappers🏗️ Flexible Structure Support
🔧 Configurable Bounds
#[runtime_bound(N)]🚀 Automatic Trait Derivation
use allfeat_midds_v2_codegen::runtime_midds;
#[runtime_midds]
pub struct MyData {
#[runtime_bound(256)]
pub title: String,
#[runtime_bound(64)]
pub tags: Vec<String>,
pub id: u64, // No transformation
}
This generates:
Std Mode (cargo build):
pub struct MyData {
pub title: String,
pub tags: Vec<String>,
pub id: u64,
}
Runtime Mode (cargo build --features="runtime"):
pub struct MyData {
pub title: BoundedVec<u8, ConstU32<256>>,
pub tags: BoundedVec<BoundedVec<u8, ConstU32<256>>, ConstU32<64>>,
pub id: u64,
}
#[runtime_midds]
pub struct Identifier(#[runtime_bound(32)] String);
#[runtime_midds]
pub struct TrackList(#[runtime_bound(100)] Vec<u64>);
#[runtime_midds]
pub enum WorkType {
Original,
#[runtime_bound(512)]
Medley(Vec<u64>), // All fields use this bound
#[runtime_bound(256)]
Remix(String, u32), // Both fields use this bound
Adaptation(u64), // No transformation needed
}
#[runtime_midds]
pub struct OptionalData {
#[runtime_bound(128)]
pub maybe_title: Option<String>,
#[runtime_bound(32)]
pub maybe_list: Option<Vec<u32>>,
#[runtime_bound(64)]
pub nested: Option<Option<String>>, // Recursive transformation
}
For types that need special handling during transformation, use the #[as_runtime_type] attribute:
#[runtime_midds]
pub struct MyStruct {
#[as_runtime_type(path = "iswc")]
pub iswc: Iswc, // Uses iswc::RuntimeIswc in runtime mode
#[as_runtime_type]
pub custom_field: CustomType, // Uses RuntimeCustomType in runtime mode
}
| Original Type | Runtime Transformation |
|---|---|
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>>> |
&str |
BoundedVec<u8, ConstU32<N>> |
| Other types | No transformation |
#[runtime_midds]
struct Example {
#[runtime_bound(256)] // This field only
title: String,
#[runtime_bound(64)] // This field only
tags: Vec<String>,
id: u64, // No bound needed
}
#[runtime_midds]
enum Example {
Simple,
#[runtime_bound(128)] // Applies to ALL fields in this variant
Complex(String, Vec<u32>, Option<String>),
}
When runtime feature is enabled:
#[derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
parity_scale_codec::DecodeWithMemTracking,
scale_info::TypeInfo,
parity_scale_codec::MaxEncodedLen,
Debug,
Clone,
PartialEq,
Eq
)]
When runtime feature is disabled:
#[derive(Debug, Clone, PartialEq, Eq)]
The macro provides helpful compile-time errors:
#[runtime_midds]
struct Example {
title: String, // ❌ Error: Missing #[runtime_bound(N)]
}
Error: String fields require #[runtime_bound(N)] attribute.
#[runtime_midds]
struct Example {
#[runtime_bound("invalid")] // ❌ Error: Must be integer
title: String,
}
#[runtime_midds]
union MyUnion { // ❌ Error: Unions not supported
a: u32,
b: f32,
}
#[runtime_bound(32)] // Short identifiers (ISWC, ISRC)
#[runtime_bound(256)] // Titles, names, descriptions
#[runtime_bound(1024)] // Large collections (track lists)
#[runtime_midds]
pub struct Track {
#[runtime_bound(12)] // ISRC length
pub isrc: String,
#[runtime_bound(256)] // Reasonable title length
pub title: String,
}
/// Musical work with industry-standard bounds
#[runtime_midds]
pub struct MusicalWork {
/// ISWC code (11 characters max)
#[runtime_bound(11)]
pub iswc: String,
/// Work title (256 characters max)
#[runtime_bound(256)]
pub title: String,
}
#[runtime_midds]
pub struct ComplexType {
#[runtime_bound(100)]
pub nested_options: Option<Vec<Option<String>>>,
#[runtime_bound(50)]
pub deep_nesting: Option<Option<Vec<u64>>>,
}
#[runtime_midds]
#[serde(rename_all = "camelCase")] // Other attributes preserved
pub struct SerializableData {
#[runtime_bound(256)]
#[serde(rename = "trackTitle")]
pub title: String,
}
#[runtime_midds]
pub struct GenericContainer<T> {
pub data: T, // Generic types passed through
#[runtime_bound(64)]
pub metadata: Vec<String>, // Still transformed
}
#[runtime_midds]
#[cfg_attr(feature = "web", wasm_bindgen(inspectable))]
pub struct WebCompatible {
#[runtime_bound(256)]
#[cfg_attr(feature = "web", wasm_bindgen(getter_with_clone))]
pub title: String,
}
The macro generates feature-gated code blocks:
#[cfg(feature = "runtime")]
// Runtime version with BoundedVec types
#[cfg(not(feature = "runtime"))]
// Std version with standard types
#[runtime_bound(N)] attributes are removed from final output#[as_runtime_type] attributes are processed and removedThe macro performs deep analysis of type structures:
Option<T> wrappersString, Vec<T>, &str)#[as_runtime_type]Set the environment variable to see generated code:
RUST_LOG=debug cargo expand
String and Vec<T> fields have #[runtime_bound(N)]runtime and web featuresThe macro includes comprehensive test coverage:
# Test the macro itself
cd midds-v2-codegen
cargo test
# Test generated code in different modes
cargo test --no-default-features --features "runtime"
cargo test --features "web"
When contributing to the macro:
# Clone and setup
git clone https://github.com/allfeat/allfeat-sdk
cd allfeat-sdk/midds-v2/midds-v2-codegen
# Run tests
cargo test
# Check generated output
cargo expand --manifest-path ../Cargo.toml
This crate is part of the Allfeat SDK and is licensed under the GNU General Public License v3.0.