Derive Macro model_mapper_macros::Mapper

source ·
#[derive(Mapper)]
{
    // Attributes available to this derive:
    #[mapper]
}
Expand description

Derives From and/or TryFrom another type to this one, or vice versa.

A mapper attribute is required at type-level and it’s optional at field or variant level.

Attributes can be set directly if only one type is involved in the conversion:

#[mapper(from, into, ty = OtherType, ignore = field_1, ignore = field_2)]

Or they can be wrapped in a derive attribute to allow for multiple types:

#[mapper(derive(try_from, ty = OtherType, ignore = field_1))]
#[mapper(derive(into, ty = YetAnotherType))]

If multiple types are involved, both variant and field level attributes can also be wrapped in a when attribute and must set the ty they refer to:

#[mapper(when(ty = OtherType, try_with = with::try_remove_option))]
#[mapper(when(ty = YetAnotherType, skip))]

The following attributes are available:

Type level attributes
  • ty = String (mandatory): The other type to derive the conversion
  • ignore = field_1 (optional, multiple): Additional fields (for structs with named fields) or variants (for enums) the other type has and this one doesn’t *
  • from (optional): Wether to derive From the other type for self
  • into (optional): Wether to derive From self for the other type
  • try_from (optional): Wether to derive TryFrom the other type for self
  • try_into (optional): Wether to derive TryFrom self for the other type
Variant level attributes
  • ignore = field_1 (optional, multiple): Additional variants that the other enum has and this one doesn’t *
  • skip (optional): Wether to skip this variant because the other enum doesn’t have it *
  • rename = "OtherVariant" (optional): To rename this variant on the other enum
Field level attributes
  • skip (optional): Wether to skip this field because the other type doesn’t have it *
  • rename = "other_field" (optional): To rename this field on the other type
  • with = mod::my_function (optional): If the field type doesn’t implement Into the other, this property allows you to customize the behavior by providing a conversion function
  • try_with = mod::my_function (optional): If the field type doesn’t implement TryInto the other, this property allows you to customize the behavior by providing a conversion function

* When ignoring or skipping fields or variants it might be required that the enum or the field type implements Default in order to properly populate it.

Examples

#[derive(Default, Mapper)]
#[mapper(try_from, into, ty = ResponseModel, ignore = Unknown)]
pub enum CustomResponse {
    #[default]
    Empty,
    #[mapper(rename = Text)]
    Message(String),
    #[mapper(ignore = internal)]
    Data {
        id: i64,
        #[mapper(rename = "text")]
        message: String,
        #[mapper(with = with::option)]
        #[mapper(try_with = with::try_option)]
        status: Option<i16>,
        #[mapper(skip)]
        random: bool,
    },
    #[mapper(skip)]
    Error,
}