Crates.io | mapper |
lib.rs | mapper |
version | 1.1.4 |
source | src |
created_at | 2022-12-12 19:04:33.692188 |
updated_at | 2022-12-28 00:50:13.187652 |
description | Mapping macro to help to reduce mapping boilerplate |
homepage | |
repository | https://github.com/sbailleul/mapper |
max_upload_size | |
id | 735162 |
size | 27,463 |
This library provides a convenient derive macro for implementing [mapper_api::Mapper
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
}
Macro works only on structs
Mapper doesn't handle nested properties
Default behavior is to take each field of annotated struct and clone those fields in the destination struct initializer by implementing [mapper_api::Mapper
#[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()}
}
}
Two mapping types are available :
Two mapping strategies are available :
Generate automatic mapping for specified strategies.
#[to(Animal, Vehicle)]
#[to(Animal, strategy=into, strategy=mapper)]
Complete automatic mapping configuration set on parent struct or provide additive mapping or exclude field from any mappings
You can set multiple to attribute by field
This attribute is forbidden if you use only DestinationType
Type of the mapping destination. Mandatory argument unless field is unconditionally excluded.
You can specify destination type with generics, these generics should be compatible with the fields of your src struct :
#[derive(Mapper)]
#[to(Person::<String, u8>)]
struct User {
name: String,
age: u8
}
struct Person<T, U> {
name: T,
age: U
}
Trigger additive mapping for mapping destination and specified strategy e.g:
#[derive(Mapper)]
struct User(#[to(Person, strategy=into)]u16, String);
struct Person(u16);
Generate 🔄 :
impl Into<Person> for User{
fn into(self)->Person{
Person{0:self.0}
}
}
Optional parameter, specify if the field is excluded for mapping, there is 2 kind of exclusion.
#[derive(Mapper)]
#[to(Person)]
struct User(u16, #[to(exclude)]String);
struct Person(u16);
Generate 🔄 :
impl Mapper<Person> for User{
fn into(self)->Person{
Person{0:self.0}
}
}
#[derive(Mapper)]
#[to(Person,Account, strategy=into)]
struct User(u16, #[to(Person,exclude)]String);
struct Account(u16, String);
struct Person(u16);
Generate 🔄 :
impl Into<Person> for User{
fn into(self)->Person{
Person{0:self.0}
}
}
impl Into<Account> for User{
fn into(self)->Account{
Account{0:self.0, 1: self.1}
}
}
Optional parameter, target the destination type field e.g :
#[derive(Mapper)]
#[to(Person)]
struct User{
#[to(Person, field=0)
name: String
};
struct Person( String);
Generate 🔄 :
impl Mapper<Person> for User{
fn to(&self)->Person{
Person{0:self.name.clone()}
}
}
Optional parameter, provide a function to transform the annotated field to the destination field.
You can specify strategy used by with function as following : with(<strategy>)
if you use with without specifying strategy : with
mapper strategy will be used by default
Signature of the function should be in regards of used strategy :
fn foo_mapping(val: &<src_field_type>)-><dst_field_type>
fn foo_mapping(val: <src_field_type>)-><dst_field_type>
You can use generics in your function if the generic types constraint respect the source field type and destination field type :
fn gen_func<T: Display>(val: &T)->String{
val.to_string()
}
#[derive(Mapper)]
#[to(Person)]
struct User {
#[to(Person, with=gen_func)]
age: u16,
}
struct Person {
age: String,
}
License: MIT OR Apache-2.0