mapper

![github]![crates-io]![docs-rs]

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


Example

```rust use mapper::Mapper;

fn mapaccountid(accountid: &u16) -> String{ accountid.to_string() }

[derive(Mapper)]

[to(Person)]

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 } ```


Disclaimer

Default behavior

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

[derive(Mapper)]

[to(Person)]

struct User{ pub name: String } struct Person{ pub name: String } Generate 🔄 : rust impl Mapper for User{ fn to(&self)->Person{ Person{name: self.name.clone()} } } ```

To struct attribute

To field attribute

DestinationType

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

Generics

You can specify destination type with generics, these generics should be compatible with the fields of your src struct : ```rust

[derive(Mapper)]

[to(Person::)]

struct User { name: String, age: u8 } struct Person { name: T, age: U } ```

Field

Optional parameter, target the destination type field

With

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>

Generics

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