4 releases (breaking)

0.4.0 Feb 9, 2024
0.3.0 Feb 8, 2024
0.2.0 Jan 23, 2024
0.1.0 Jan 23, 2024

#123 in #derive-debug

Download history 12/week @ 2024-01-21 9/week @ 2024-02-04 5/week @ 2024-02-18 11/week @ 2024-02-25 2/week @ 2024-03-10 65/week @ 2024-03-31

67 downloads per month
Used in mapstruct

MIT license

47KB
1K SLoC

Mapstruct-Rs

A proc macro that generates new structs with the variations of the fields you want. You use it by annotating a struct with #[derive(Mapstruct)] and then you can use the mapstruct(...) macro to generate new structs. The syntax is similar to the struct definition syntax, but you only specify the fields and generics you want to change. You use the + operator to add a field or generic, the - operator to remove a field or generic and the ~ operator to change the type or name of a field or generic.

Example Struct

use mapstruct_rs::Mapstruct;

#[derive(MapStruct)]
#[mapstruct(
    #[derive(Debug)]
    struct Y<
        +'a,
    > {
        ~id -> pub id,
        ~name: &'a str,
        ~some: &'a str,
        +last_name: &'a str,
        -height,
    }
)]
struct X {
    id: i64,
    name: String,
    age: i32,
    height: f32,
    some: String,
}

The above code will generate the following struct:

#[derive(Debug)]
struct Y<'a> {
    pub id: i64,
    name: &'a str,
    age: i32,
    some: &'a str,
    last_name: &'a str,
}

Example Enum

use mapstruct_rs::Mapstruct;

#[derive(MapStruct)]
#[mapstruct(
    #[derive(Debug)]
    enum Y {
        A {
            id: i64,
        },
        ~B(~i32, _),
        +D(i8),
    }
)]
enum X {
    A(i64),
    B(i32, i8),
    C(i16),
}

The above code will generate the following enum:

#[derive(Debug)]
enum Y {
    A {
        id: i64,
    },
    B(i32),
    C(i16),
    D(i8)
}

Dependencies

~0.4–0.8MB
~19K SLoC