simple_dto_mapper_derive
simple_dto_mapper_derive provides a custom derive macro DtoFrom that generates
impl From<Source> for Target to map model structs into DTOs. It focuses on a
small, explicit set of attributes for clarity and reliability.
Features
- Source struct:
#[dto(from = Type)](required) - Field rename:
#[dto(rename = "orig_name")] - Custom transform:
#[dto(transform_fn = path::to::function)](owned in → owned out) - Skip with default:
#[dto(skip)] - Infallible convert:
#[dto(into)](usesFrom<Src> for Dst) - Clear diagnostics: unknown/duplicate/conflicting attributes produce span-accurate compile errors (see
tests/ui)
Mapping Rules (at a glance)
-
Default (owned move)
Same name & same type →target = source.field
“Compatible type” means:- Identical type, or
#[dto(into)]whereFrom<SourceFieldType> for FieldTypeexists, or#[dto(transform_fn = ...)]provides an explicit conversion
-
Field attributes
#[dto(rename = "orig_name")]— read from another source field name#[dto(transform_fn = path)]— callpath(source.orig_name)before assignment#[dto(skip)]— initialize withDefault::default()#[dto(into)]— callInto::into(source.orig_name)(requiresFrom)
-
Struct attribute (required)
#[dto(from = Type)]— specify the source struct.
Usage
use DtoFrom;
Collections & Option
Collections and Option do not auto-convert inner elements. Use a transform_fn helper or implement From<Vec<T>> for Vec<U> if you prefer #[dto(into)].
// same type, no transform needed
pub keywords: ,
// Vec<SourceTag> → Vec<DtoTag> using a transform function
pub tags: ,
// Option<SourceAuthor> → Option<DtoAuthor>
pub author: ,
Installation
Add this to your Cargo.toml:
[]
= "0.1.1"
Diagnostics & Limitations
- Named-field structs only (tuple/unit structs & enums are not supported)
- Owned-only mapping (
impl From<Source> for Target) transform_fnmust beFnOnce(SrcField) -> DstFieldintorequiresFrom<SrcField> for DstFieldskiprequiresDefault- Clear errors for unknown/duplicate/conflicting attributes; see
tests/ui
License
Licensed under the MIT license.