Skip to main content

MapFrom

Derive Macro MapFrom 

Source
#[derive(MapFrom)]
{
    // Attributes available to this derive:
    #[map_from]
    #[map]
}
Expand description

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, use Default::default()
  • #[map(into)] — Call .into() on the source field value
  • #[map(with = "path::to::fn")] — Apply a custom conversion function