![github] ![crates-io] ![docs-rs] ![mapper-ci]
This library provides a convenient derive macro for implementing [mapper_api::Mapper
```rust use mapper::Mapper;
fn mapaccountid(accountid: &u16) -> String{ accountid.to_string() }
struct User{ #[to(Person, field=name)] pub name: String, #[to(Person, with=mapaccountid)] pub accountid: u16, pub age: u8 } struct Person{ pub name: String, pub accountid: String, pub age: u8 } ```
Default behavior is to take each field of annotated struct and clone those fields in the destination struct initializer by implementing [mapper_api::Mapper
struct User{
pub name: String
}
struct Person{
pub name: String
}
Generate 🔄 :
rust
impl Mapper
Two mapping types are available : - Automatic, generate mapping for destinations specified in to struct attributes, all fields of the source are used for mapping if they are not explicitly excluded. You can't use automatic mapping if additive mapping is already used for destination and strategy - Additive, generate mapping for destinations specified in to field attributes, only annotated fields are mapped. You can't use additive mapping if automatic mapping is already used for destination and strategy
Two mapping strategies are available :
- mapper(default), map source to destination without consuming source, generate implementation of [mapper_api::Mapper
Generate automatic mapping for specified strategies.
- You can set multiple to attribute by struct
- Specify one or multiple destination types in this attribute : #[to(Animal, Vehicle)]
- Specify one or multiple mapping strategies in this attribute : #[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 : ```rust
struct User {
name: String,
age: u8
}
struct Person
Trigger additive mapping for mapping destination and specified strategy e.g:
````rust
struct User(#[to(Person, strategy=into)]u16, String); struct Person(u16); ```` Generate 🔄 :
rust
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. - Unconditionally exclusion, exclude field of any kind of mapping e.g :
````rust
struct User(u16, #[to(exclude)]String); struct Person(u16); ```` Generate 🔄 :
rust
impl Mapper<Person> for User{
fn into(self)->Person{
Person{0:self.0}
}
}
struct User(u16, #[to(Person,exclude)]String); struct Account(u16, String); struct Person(u16); ```` Generate 🔄 :
rust
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 : ````rust
struct User{ #[to(Person, field=0) name: String }; struct Person( String); ```` Generate 🔄 :
rust
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 :
- with(mapper) | with :
rust
fn foo_mapping(val: &<src_field_type>)-><dst_field_type>
- with(into) :
rust
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 :
rust
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