Crates.io | automapper |
lib.rs | automapper |
version | 0.0.2 |
source | src |
created_at | 2024-11-23 16:24:35.539414 |
updated_at | 2024-11-23 17:29:10.754006 |
description | A simple convention based object-to-object mapper for Rust |
homepage | |
repository | https://github.com/mustakimali/automapper |
max_upload_size | |
id | 1458561 |
size | 4,061 |
A convention based object-object mapper for Rust. This uses Json rustdoc generated by cargo doc
.
rustdoc.json
using automapper-cli
.# instal
cargo install --locked automapper-cli
# generate rustdoc.json in the root of a crate where you plan to use automapper
automapper-cli .
automapper
in your crate.Define some types and use automapper
to map between them.
// some types
pub struct SourceStruct {
pub a: i32,
pub b: u32,
pub s: String,
}
pub struct DestStruct {
pub a: i32,
pub b: u32,
pub s: String,
}
Use automapper
using auto implemented trait [AutoMapsTo] and [AutoMapsFrom]
automapper::map!(SourceStruct, DestStruct);
use automapper::{AutoMapsFrom, AutoMapsTo};
let input = SourceStruct { .. };
let output = DestStruct::map_from(input); // using AutoMapsFrom trait
let output: DestStruct = input.map_into(); // using AutoMapsTo trait
let output = input.map_into(); // using AutoMapsTo trait (type annotation isn't necessary)
Or, Use automapper
to generate mapping function
automapper::impl_map_fn!{
fn convert_to(SourceStruct -> DestStruct);
}
// this generates `convert_to` function like this
// fn convert_to(value: SourceStruct) -> DestStruct { .. }
let input = SourceStruct { .. };
let output = convert_to(input);
// output is DestStruct { .. }