-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace.rs
50 lines (39 loc) · 909 Bytes
/
replace.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use one_dto_mapper::{TryFrom, TryInto};
struct UserName(String);
impl TryFrom<String> for UserName {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(UserName(value))
}
}
impl From<UserName> for String {
fn from(value: UserName) -> Self {
value.0
}
}
struct PersonDto {
name: String,
}
#[derive(TryFrom)]
#[try_from(T = PersonDto, Error = String)]
struct FromPerson {
name: UserName,
#[try_from(replace = "0u16")]
age: u16,
}
#[derive(TryFrom, TryInto)]
#[try_from(T = PersonDto, Error = String)]
#[try_into(T = PersonDto, Error = String)]
struct IntoFromPerson {
#[try_into(infallible)]
name: UserName,
#[try_into(skip)]
#[try_from(replace = "0u16")]
age: u16,
}
fn main() {
let p = PersonDto {
name: "Joe".to_string(),
};
let p2 = IntoFromPerson::try_from(p).unwrap();
}