π struct-mapper
Derive macro to auto-generate From<Source> for your structs
β zero boilerplate field mapping.
π Documentation Β· π¦ Crates.io Β· π Report Bug Β· π‘ Request Feature
Stop writing tedious manual From implementations for struct-to-struct conversions. struct-mapper generates them at compile time with zero runtime overhead.
use MapFrom;
// That's it! Now you can do:
let entity = UserEntity ;
let response: UserResponse = entity.into;
No runtime cost. No reflection. Just a clean
impl From<>generated at compile time.
β¨ Why struct-mapper?
Every Rust backend developer writes this dozens of times:
π¦ Installation
Add to your Cargo.toml:
[]
= "0.1"
Minimum Supported Rust Version: 1.71.0
π Features
1οΈβ£ Basic Mapping β Same Name, Same Type
Fields with matching names are mapped automatically. No attributes needed.
use MapFrom;
let target: Target = Source .into;
assert_eq!;
2οΈβ£ Renamed Fields β #[map(from = "...")]
When source and target field names differ:
use MapFrom;
3οΈβ£ Skip + Default β #[map(skip, default)]
For fields that don't exist in the source struct:
use MapFrom;
4οΈβ£ Nested Conversion β #[map(into)]
For fields where the source type implements Into<TargetType>:
use MapFrom;
5οΈβ£ Custom Function β #[map(with = "...")]
For complex transformations using any function:
use MapFrom;
π Combine Everything
All attributes work together seamlessly:
use MapFrom;
π Attribute Reference
| Attribute | Applies To | Description |
|---|---|---|
#[map_from(Type)] |
Struct | Source type to generate From<Type> for |
#[map(from = "name")] |
Field | Map from a differently-named source field |
#[map(skip, default)] |
Field | Skip this field, use Default::default() |
#[map(into)] |
Field | Call .into() on the source value |
#[map(with = "fn")] |
Field | Apply a custom conversion function |
π‘ Tip: Attributes can be combined:
#[map(from = "old_name", with = "convert_fn")]
π‘οΈ Error Messages
struct-mapper provides clear, actionable error messages that tell you exactly what went wrong and how to fix it:
error: missing `#[map_from(SourceType)]` attribute.
Add `#[map_from(YourSourceStruct)]` to specify which struct to map from.
Example:
#[derive(MapFrom)]
#[map_from(UserEntity)]
struct UserResponse { ... }
error: `#[map(skip)]` requires `#[map(skip, default)]`.
When skipping a field, you must provide a default value.
Fix: #[map(skip, default)]
π Comparison
How does struct-mapper compare to alternatives?
| Feature | struct-mapper | derive-into | more-convert | structural-convert |
|---|---|---|---|---|
| Same-field mapping | β | β | β | β |
| Field renaming | β | β | β | β οΈ |
| Skip + default | β | β οΈ | β οΈ | β οΈ |
Nested .into() |
β | β | β | β οΈ |
| Custom function | β | β | β οΈ | β οΈ |
| Clear error messages | β | β | β | β |
| Clean syntax | β | β οΈ | β οΈ | β οΈ |
| Compile-time only | β | β | β | β |
| Zero runtime deps | β | β | β | β |
πΊοΈ Roadmap
-
Fromβ infallible struct conversion - Field renaming, skipping, nesting, custom functions
- Clear compile-time error messages
-
TryFromβ fallible conversions (v0.2) - Enum variant mapping (
v0.3) - Bi-directional mapping (
v0.4)
β οΈ Limitations (v0.1)
- Only
From(infallible conversion).TryFromis planned for v0.2. - Only named struct fields. Tuple structs and enums are not yet supported.
- Generics on the target struct are supported; generic source types require manual annotation.
π€ Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'feat: add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
π¨βπ» Author
Deendayal Kumawat
π License
Licensed under either of:
- Apache License, Version 2.0 β LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0
- MIT License β LICENSE-MIT or http://opensource.org/licenses/MIT
at your option.
β If you find this useful, please consider giving it a star! β
Made with β€οΈ in Rust