automapper

Crates.ioautomapper
lib.rsautomapper
version0.0.2
sourcesrc
created_at2024-11-23 16:24:35.539414
updated_at2024-11-23 17:29:10.754006
descriptionA simple convention based object-to-object mapper for Rust
homepage
repositoryhttps://github.com/mustakimali/automapper
max_upload_size
id1458561
size4,061
Mohammad Mustakim Ali (mustakimali)

documentation

https://docs.rs/automapper/

README

automapper

A convention based object-object mapper for Rust. This uses Json rustdoc generated by cargo doc.

Usage

Step 1: Generate 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 .

Step 2: Use 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 { .. }
Commit count: 0

cargo fmt