![github] ![crates-io] ![docs-rs]
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 : ```rust
struct User{
pub name: String
}
struct Person{
pub name: String
}
Generate 🔄 :
rust
impl Mapper
#[to(Animal, Vehicle)]
#[to(<DestinationType>, field=<destination_field>, with=<transformation_function>)]
This parameter is mandatory and have to be present in the To struct attribute.
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
Optional parameter, target the destination type field
Optional parameter, provide a function to transform the annotated field to the destination field. Signature of the function should be :
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