3 releases
0.1.9 | Feb 11, 2025 |
---|---|
0.1.8 | Oct 23, 2024 |
0.1.7 | Sep 25, 2024 |
#265 in Authentication
383 downloads per month
11KB
276 lines

One DTO Mapper
Dependency of the Procivis One Core, a complete solution capable of powering every element of the digital identity credential lifecycle. See the complete solution architecture.
Derives From
and TryFrom
implementations for types with similar shape.
See examples for how to use it.
Infallible conversions
Mapping Optional<T>
into T
unwrap_or
attributes can be used to map Optional<T>
into T
using default value.
Example:
struct OptionalDto {
age: Option<u16>,
}
#[derive(From)]
#[from(OptionalDto)]
struct FromOptionalDto {
#[from(unwrap_or = "16")]
age: u16,
}
age
in FromOptionalDto
will be set to 16
if original value is None
. More examples can be found here and here.
This attribute cannot be combined with with_fn
or with_fn_ref
attributes.
Field renaming
The rename
attribute can be used in cases where the field name is different across structures.
Example:
struct PersonDto {
name: String,
}
#[derive(Into, From)]
#[into(PersonDto)]
#[from(PersonDto)]
struct AnotherPerson {
#[into(rename = "name")]
#[from(rename = "name")]
full_name: String,
}
In this case full_name
will be mapped to name
.
More examples can be found here and here.
Default value for field
The replace
attribute can be used in cases where the source data type does not have any value to be used as a source for mapping. In this case, you can specify a static value that will always be used instead.
Example:
struct PersonDto {
name: String,
}
#[derive(From)]
#[from(PersonDto)]
struct FromPerson {
name: String,
#[from(replace = "0u16")]
age: u16,
}
In this case age
will be allways assigned 0
when FromPerson
is created from PersonDto
.
More examples can be found here and here.
Failable conversions
TryFrom
and TryInto
macros can be used to generate failable conversions using TryFrom
trait. They support the same feature set as From
macro. Examples can be found here.
Force infallible conversion
infallible
attribute can be used nn cases when infallible conversion should be used.
Example:
struct PersonDto {
name: String,
age: u16,
}
#[derive(TryFrom)]
#[try_from(T = PersonDto, Error = String)]
struct Person {
name: UserName,
#[try_from(infallible)]
age: u16,
}
In this case age
will be converted using From
trait instead of TryFrom
.
Support
Need support or have feedback? Contact us.
License
Some rights reserved. This library is published under the Apache License Version 2.0.
© Procivis AG, https://www.procivis.ch.
Dependencies
~0.6–1MB
~23K SLoC