🛠️ MIDDS Types Codegen
Procedural macros for generating bounded string and collection types with Substrate and WebAssembly compatibility.
Overview
This crate provides macros to generate bounded types that work seamlessly with Substrate runtime and JavaScript bindings, solving wasm_bindgen
's generic parameter limitations.
Macros
#[midds_string(bound)]
Generates bounded string types with UTF-8 validation.
use midds_string;
;
// With regex validation
;
// Usage
let mut title = from_str.unwrap;
title.push_str.unwrap;
assert_eq!;
#[midds_collection(type, bound)]
Generates bounded collection types.
use midds_collection;
;
// Usage
let mut producers = new;
producers.push.unwrap;
producers.push.unwrap;
assert_eq!;
Generated Features
Rust API
- Standard trait implementations (
Clone
,PartialEq
,Encode
,Decode
, etc.) - String/collection manipulation methods
- Type-specific error handling
- Substrate runtime compatibility
JavaScript API (with js
feature)
wasm_bindgen
bindings- Property getters/setters
- Type-safe method exports
- Serde serialization support
Key Benefits
- ✅ Compile-time bounds: Capacity limits enforced at compile time
- ✅ Runtime validation: UTF-8 and bounds checking at runtime
- ✅ WASM compatibility: JavaScript bindings without generic limitations
- ✅ Memory efficient: Wraps
sp_runtime::BoundedVec
- ✅ Type safety: Strong typing with comprehensive error handling
Error Handling
Each generated type includes a specific error enum:
// For strings
// For collections
Dependencies
- proc-macro2 - Procedural macro utilities
- quote - Code generation
- syn - Rust syntax parsing