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.
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.
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.
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.
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.
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
.
Need support or have feedback? Contact us.
Some rights reserved. This library is published under the Apache License Version 2.0.
© Procivis AG, https://www.procivis.ch.