Documentation

mapper

githubcrates-iodocs-rs

This library provides a convenient derive macro for implementing [mapper_api::Mapper] trait and generate mapping without boilerplate.

Example

use mapper::Mapper;

fn map_account_id(account_id: &u16) -> String{
    account_id.to_string()
}

#[derive(Mapper)]
#[to(Person)]
struct User{
    #[to(Person, field=_name)]
    pub name: String,
    #[to(Person, with=map_account_id)]
    pub account_id: u16,
    pub age: u8
}
struct Person{
    pub _name: String,
    pub account_id: String,
    pub age: u8
}

Disclaimer

  • Macro works only on C style struct like : struct MyStruct{field: u8}
  • Mapper doesn't handle generics
  • Mapper doesn't handle nested properties

Default behavior

Default behavior is to take each field of annotated struct and clone those fields in the destination struct initializer :

#[derive(Mapper)]
#[to(Person)]
struct User{
    pub name: String
}
struct Person{
    pub name: String
}

Generate 🔄 :

impl Mapper<Person> for User{
    fn to(&self)->Person{
        Person{name: self.name.clone()}
    }
}

To struct attribute

  • You can specify multiple destination types in this attribute : #[to(Animal, Vehicle)]

To field attribute

  • You can put multiple to attribute by field
  • Syntax of this attribute : #[to(<DestinationType>, field=<destination_field>, with=<transformation_function>)]
  • This attribute is forbidden if you use only

DestinationType

This parameter is mandatory and have to be present in the To struct attribute

Field

Optional parameter, target the destination type field

With

Optional parameter, provide a function to transform the annotated field to the destination field

License: MIT OR Apache-2.0