Module :: component_model
Revolutionary type-safe component assignment for Rust. Build complex objects with zero boilerplate using derive macros and type-driven field setting. Perfect for configuration builders, fluent APIs, and object composition patterns.
๐ Why Component Model?
Traditional struct initialization is verbose and error-prone:
#
# ;
#
// Traditional approach - repetitive and fragile
let config = Config
;
// Builder pattern - lots of boilerplate
let config = new
.host
.port
.build;
Component Model approach - Clean, type-safe, zero boilerplate:
use Assign;
// Set components by type - no field names needed!
let mut config = default;
config.assign; // Automatically sets String field
config.assign; // Automatically sets i32 field
// Or use fluent style
let config = default
.impute
.impute;
โจ Key Features
- ๐ฏ Type-driven assignment - Set fields by component type, not field name
- ๐ง Zero boilerplate - Derive macros generate all implementations automatically
- ๐ Fluent APIs - Chainable
impute()
method for builder patterns - ๐ก๏ธ Type safety - All assignments checked at compile time
- ๐ Flexible conversion - Accepts any type convertible to target field type
- ๐ฆ Multiple assignment - Set multiple components with
ComponentsAssign
- โก Popular types support - Built-in support for Duration, PathBuf, SocketAddr, and more
- ๐๏ธ ComponentModel derive - Unified derive macro combining all functionality
๐ Quick Start
Add to your Cargo.toml
:
[ ]
= "0.4"
Feature Flags
Component Model follows granular feature gating for minimal builds:
[ ]
# Minimal version - no features enabled by default
= { = "0.4", = false }
# Enable specific features as needed
= { = "0.4", = [ "derive_component_model" ] }
# Or enable all features (default)
= { = "0.4", = [ "full" ] }
Available features:
enabled
- Master switch for core functionalityfull
- All features (enabled by default)derive_component_model
- Unified ComponentModel derive macroderive_component_assign
- Basic Assign derive macroderive_components_assign
- Multiple component assignmentderive_component_from
- Component creation from single valuesderive_from_components
- Component creation from multiple values
๐ Core Concepts
1. Basic Assignment with ComponentModel
use ;
2. Popular Types Support
ComponentModel provides built-in support for popular Rust types with intelligent conversion:
use ;
use Duration;
use PathBuf;
3. Enum Fields in Structs
ComponentModel works with structs that contain enum fields, enabling type-safe enum assignment:
use ;
Complex Enum Fields
use ;
use Duration;
Note: Direct ComponentModel derive on enums is planned for future releases. Currently, enums work as field types in structs with ComponentModel.
4. Fluent Builder Pattern
# use ;
#
#
let person = default
.impute // Chainable assignment
.impute; // Returns Self for chaining
5. Multiple Component Assignment
use ;
let mut config = default;
config.assign; // String component
config.assign; // i32 component
6. Manual Implementation (Advanced)
For custom behavior, implement traits manually:
use *;
let config = default
.impute // String
.impute; // usize pool_size
HTTP Client Builders
use ;
use Duration;
let client = default
.impute
.impute; // Duration from fractional seconds
Game Entity Systems
use ;
// Initialize components
let mut player = default;
player.assign;
player.assign;
๐งช Examples
Explore the examples directory for comprehensive usage patterns:
000_basic_assignment.rs
- Basic component assignment001_fluent_builder.rs
- Fluent builder pattern002_multiple_components.rs
- Multiple component handling003_component_from.rs
- Component creation patterns004_working_example.rs
- Real-world usage scenarioscomponent_model_trivial.rs
- Minimal example
๐ Supported Popular Types
ComponentModel includes built-in intelligent conversion for:
Type | Input Types | Example |
---|---|---|
Duration |
u64 , f64 , (u64, u32) |
config.assign( 30u64 ) |
PathBuf |
&str , String |
config.assign( "/path/file" ) |
SocketAddr |
Coming soon | String parsing planned |
HashMap |
Framework ready | Vec conversion planned |
HashSet |
Framework ready | Vec conversion planned |
โ ๏ธ Important Limitations
Type Ambiguity: When a struct has multiple fields of the same type, assign()
becomes ambiguous and won't compile. This is by design for type safety.
# use ;
#
// This won't compile due to ambiguity:
// let mut config = Config::default();
// config.assign( "localhost" ); // Error: which String field?
๐ Available Derive Macros
ComponentModel
- โญ Recommended - Unified derive combining all functionalityAssign
- Basic component assignment by typeComponentsAssign
- Multiple component assignment from tuplesComponentFrom
- Create objects from single componentsFromComponents
- Create objects from multiple components
๐ฏ Real-World Use Cases
Configuration Management with Popular Types
use ;
use Duration;
use PathBuf;
let config = default
.impute // String
.impute // i32
.impute; // Duration from seconds
HTTP Client Builders
use ;
use Duration;
let client = default
.impute
.impute; // Duration from fractional seconds
Game Entity Systems
use ;
// Initialize components
let mut player = default;
player.assign;
player.assign;
๐งช Examples
Explore the examples directory for comprehensive usage patterns:
000_basic_assignment.rs
- Basic component assignment001_fluent_builder.rs
- Fluent builder pattern002_multiple_components.rs
- Multiple component handling003_component_from.rs
- Component creation patterns004_working_example.rs
- Real-world usage scenarioscomponent_model_trivial.rs
- Minimal example
๐ Supported Popular Types
ComponentModel includes built-in intelligent conversion for:
Type | Input Types | Example |
---|---|---|
Duration |
u64 , f64 , (u64, u32) |
config.assign( 30u64 ) |
PathBuf |
&str , String |
config.assign( "/path/file" ) |
SocketAddr |
Coming soon | String parsing planned |
HashMap |
Framework ready | Vec conversion planned |
HashSet |
Framework ready | Vec conversion planned |
โ ๏ธ Important Limitations
Type Ambiguity: When a struct has multiple fields of the same type, assign()
becomes ambiguous and won't compile. This is by design for type safety.
# use ;
#
// This won't compile due to ambiguity:
// let mut config = Config::default();
// config.assign( "localhost" ); // Error: which String field?
Workarounds:
- Use different types when possible (e.g.,
String
vsPathBuf
) - Use direct field assignment:
config.host = "localhost".to_string();
- Implement manual
Assign
traits for specific use cases
๐ Learn More
- ๐ Examples - Step-by-step examples showing all features
- ๐ API Docs - Complete API reference
- ๐ Source Code - Contribute or report issues
- ๐ฌ Discord - Get help and discuss
Made with โค๏ธ as part of the wTools ecosystem