#conventions #mapper #rustdoc #json #docs #step #object-to-object

automapper

A simple convention based object-to-object mapper for Rust

1 unstable release

new 0.0.2 Nov 23, 2024
0.0.1 Nov 23, 2024

#727 in Development tools

MIT/Apache

8KB
117 lines

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 { .. }

Dependencies

~2–10MB
~104K SLoC