#[derive(MapFrom)]
{
// Attributes available to this derive:
#[map_from]
#[map]
}
Expand description
Derive macro that generates impl From<Source> for Target by mapping struct fields.
Place #[derive(MapFrom)] on your target struct along with #[map_from(SourceType)]
to auto-generate the From implementation.
ยงExample
use struct_mapper::MapFrom;
struct UserEntity {
name: String,
email: String,
}
#[derive(MapFrom)]
#[map_from(UserEntity)]
struct UserResponse {
name: String,
email: String,
}
let entity = UserEntity {
name: "Khushi".to_string(),
email: "khushi@gmail.com".to_string(),
};
let response: UserResponse = entity.into();
assert_eq!(response.name, "Khushi");ยงField Attributes
| Attribute | Description |
|---|---|
#[map(from = "name")] | Map from a differently-named source field |
#[map(skip, default)] | Skip this field, use Default::default() |
#[map(into)] | Call .into() on the source value |
#[map(with = "fn_path")] | Apply a custom conversion function |
Attributes can be combined: #[map(from = "old_name", with = "convert_fn")]
Derive macro that generates impl From<Source> for Target by mapping fields.
ยงUsage
โ
use struct_mapper::MapFrom;
struct UserEntity {
name: String,
email: String,
}
#[derive(MapFrom)]
#[map_from(UserEntity)]
struct UserResponse {
name: String,
email: String,
}
// Now you can do:
let entity = UserEntity { name: "Alice".into(), email: "a@b.com".into() };
let response: UserResponse = entity.into();ยงField Attributes
#[map(from = "source_field")]โ Map from a differently-named source field#[map(skip, default)]โ Skip this field, useDefault::default()#[map(into)]โ Call.into()on the source field value#[map(with = "path::to::fn")]โ Apply a custom conversion function