#[derive(Mapper)]
{
// Attributes available to this derive:
#[mapper]
}
Expand description
Derive mapper functions to convert between types.
A mapper attribute is required at type-level and it’s optional at field or variant level.
The following attributes are available:
§Type level attributes
ty = PathType(mandatory): The other type to derive the conversionfrom(optional): Whether to deriveFromthe other type for selfcustom(optional): Derive a custom function instead of the traitcustom = from_other(optional): Derive a custom function instead of the trait, with the given name
into(optional): Whether to deriveFromself for the other typecustom(optional): Derive a custom function instead of the traitcustom = from_other(optional): Derive a custom function instead of the trait, with the given name
try_from(optional): Whether to deriveTryFromthe other type for selfcustom(optional): Derive a custom function instead of the traitcustom = from_other(optional): Derive a custom function instead of the trait, with the given name
try_into(optional): Whether to deriveTryFromself for the other typecustom(optional): Derive a custom function instead of the traitcustom = from_other(optional): Derive a custom function instead of the trait, with the given name
add(optional, multiple): Additional fields (for structs with named fields) or variants (for enums) the other type has and this one doesn’t ¹field = other_field(mandatory): The field or variant namety = bool(optional): The field type, mandatory forintoandtry_intoif no default value is provideddefault(optional): The field or variant will be populated usingDefault::default()(mandatory for enums, with or without value)value = true(optional): The field or variant will be populated with the given expression instead
ignore_extra(optional): Whether to ignore all extra fields (for structs) or variants (for enums) of the other type ²
§Variant level attributes
rename = OtherVariant(optional): To rename this variant on the other enumadd(optional, multiple): Additional fields of the variant that the other type variant has and this one doesn’t ¹field = other_field(mandatory): The field namety = bool(optional): The field type, mandatory forintoandtry_intoif no default value is provideddefault(optional): The field or variant will be populated usingDefault::default()value = true(optional): The field or variant will be populated with the given expression instead
skip(optional): Whether to skip this variant because the other enum doesn’t have itdefault(mandatory): The field or variant will be populated usingDefault::default()value = get_default_value()(optional): The field or variant will be populated with the given expression instead
ignore_extra(optional): Whether to ignore all extra fields of the other variant (only valid for from and try_from) ²
§Field level attributes
rename = other_name(optional): To rename this field on the other typeskip(optional): Whether to skip this field because the other type doesn’t have itdefault(optional): The field or variant will be populated usingDefault::default()value = get_default_value()(optional): The field or variant will be populated with the given expression instead
Additional hints on how to map fields:
opt(optional): The field is anOptionand the inner value shall be mapped ³iter(optional): The field is an iterator and the inner value shall be mapped ³map(optional): The field is a hashmap-like iterator and the inner value shall be mapped ³with = mod::my_function(optional): If the field type doesn’t implementIntoorTryIntothe other, this property allows you to customize the behavior by providing a conversion functionfrom_with = mod::my_function(optional): The same as above but only for thefromortry_fromderivesfrom_with = mod::my_function(optional): The same as above but only for thefromortry_fromderives
¹ When providing additional fields without defaults, the From and TryFrom traits can’t be derived and
a custom function will be required instead. When deriving into or try_into, the ty must be provided as well.
² When ignoring fields or variants it might be required that the enum or the struct implements Default
in order to properly populate it.
³ Hints can be nested, for example: opt(vec), vec(opt(with = "my_custom_fn"))
§Example
#[derive(Mapper)]
#[mapper(from, ty = Entity)]
pub struct Model {
id: i64,
name: String,
#[mapper(skip(default))]
surname: Option<String>,
}Other advanced use cases are available on the examples folder.