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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use try_convert_derive;
use ;
/** # derive-into
For more information, visit the [github repository](https://github.com/sharonex/derive-into/tree/darling-migration).
A derive macro for creating conversions between structs and enums with similar structures.
This crate provides the `#[derive(Convert)]` macro that automates implementations of
conversion traits (`From`, `Into`, `TryFrom`, `TryInto`) between types.
## Basic Usage
```rust
use derive_into::Convert;
#[derive(Convert)]
#[convert(into(path = "Destination"))]
struct Source {
id: u32,
#[convert(rename = "full_name")]
name: String,
}
struct Destination {
id: u32,
full_name: String,
}
// Usage: let destination: Destination = source.into();
```
## Attribute Reference
### Struct/Enum Level Attributes
| Attribute | Description |
|-----------|-------------|
| `#[convert(into(path = "Type"))]` | Implements `From<Self> for Type` |
| `#[convert(from(path = "Type"))]` | Implements `From<Type> for Self` |
| `#[convert(try_into(path = "Type"))]` | Implements `TryFrom<Self> for Type` |
| `#[convert(try_from(path = "Type"))]` | Implements `TryFrom<Type> for Self` |
Multiple conversion attributes can be specified for a single type:
```
use derive_into::Convert;
struct ApiModel {
version: String,
name: String,
}
struct DbModel {
version: String,
name: String,
}
#[derive(Convert)]
#[convert(into(path = "ApiModel"))]
#[convert(try_from(path = "DbModel"))]
struct DomainModel {
version: String,
name: String,
}
```
### Field Level Attributes
Field attributes can be applied at three different scopes:
1. **Global scope** - applies to all conversions
```text
#[convert(rename = "new_name")]
```
2. **Conversion type scope** - applies to a specific conversion type
```text
#[convert(try_from(skip))]
```
3. **Specific conversion scope** - applies to a specific conversion path
```text
#[convert(try_from(path = "ApiModel", skip))]
```
| Attribute | Description |
|-----------|-------------|
| `#[convert(rename = "new_name")]` | Maps field to different name in target |
| `#[convert(skip)]` | Excludes field from conversion |
| `#[convert(default)]` | Uses `Default::default()` for this field |
| `#[convert(unwrap)]` | Unwraps `Option` (`try_from` fails if `None`) |
| `#[convert(unwrap_or_default)]` | Automatically calls unwrap_or_default on `Option` value before converting it |
| `#[convert(with_func = "func_name")]` | Uses custom conversion function |
### Custom Conversion Functions
Functions specified with `with_func` must accept a reference to the source type:
```rust
use derive_into::Convert;
struct ValidatedType(String);
struct ApiModel {
field: String,
}
#[derive(Convert)]
#[convert(try_from(path = "ApiModel"))]
struct Product {
#[convert(try_from(rename = "field", with_func = "validate_field"))]
validated: ValidatedType,
}
fn validate_field(source: &ApiModel) -> Result<ValidatedType, String> {
Ok(ValidatedType(source.field.clone()))
}
```
## Type Conversion Behavior
* **Direct mapping**: Identical types are copied directly
* **Automatic conversion**: Uses `From`/`Into` for different types
* **Container types**: Handles `Option<T>`, `Vec<T>`, and `HashMap<K,V>`
* **Nested conversions**: Converts nested structs/enums automatically
## Container Type Examples
### Option and Vec
```rust
use derive_into::Convert;
struct Number(u8);
impl From<u8> for Number {
fn from(n: u8) -> Self {
Number(n)
}
}
#[derive(Convert)]
#[convert(into(path = "Target"))]
struct Source {
// Inner type u8 -> Number conversion happens automatically
optional: Option<u8>,
vector: Vec<u8>,
}
struct Target {
optional: Option<Number>, // Number implements From<u8>
vector: Vec<Number>,
}
```
### HashMap
```rust
use derive_into::Convert;
use std::collections::HashMap;
#[derive(Hash, Eq, PartialEq)]
struct CustomString(String);
impl From<String> for CustomString {
fn from(s: String) -> Self {
CustomString(s)
}
}
struct CustomInt(u32);
impl From<u32> for CustomInt {
fn from(i: u32) -> Self {
CustomInt(i)
}
}
#[derive(Convert)]
#[convert(into(path = "Target"))]
struct Source {
// Both keys and values convert if they implement From/Into
map: HashMap<String, u32>,
}
struct Target {
map: HashMap<CustomString, CustomInt>,
}
```
## Enum Conversion
```rust
use derive_into::Convert;
#[derive(Convert)]
#[convert(into(path = "TargetEnum"))]
enum SourceEnum {
Variant1(u32),
#[convert(rename = "RenamedVariant")]
Variant2 {
value: String,
#[convert(rename = "renamed_field")]
field: u8,
},
Unit,
}
enum TargetEnum {
Variant1(u32),
RenamedVariant {
value: String,
renamed_field: u8,
},
Unit,
}
```
Derive macro for generating conversion implementations between similar types.
The `Convert` derive macro generates implementations of standard conversion traits
(`From`, `Into`, `TryFrom`, `TryInto`) between structs and enums with similar structures.
# Examples
Basic struct conversion with field renaming:
```rust
use derive_into::Convert;
#[derive(Convert)]
#[convert(into(path = "Destination"))]
struct Source {
id: u32,
#[convert(rename = "full_name")]
name: String,
}
struct Destination {
id: u32,
full_name: String,
}
*/