o2o 0.2.4

Object to Object mapper for Rust
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
Object to Object mapper for Rust<!-- omit from toc --> 
================================
[<img alt="github.com" src="https://github.com/Artem-Romanenia/o2o/workflows/Build/badge.svg" height="25">](https://github.com/Artem-Romanenia/o2o/)
[<img alt="crates.io" src="https://img.shields.io/crates/v/o2o.svg?style=for-the-badge&color=2f4d28&labelColor=f9f7ec&logo=rust&logoColor=black" height="25">](https://crates.io/crates/o2o)

## Content<!-- omit from toc --> 

- [Quick pitch]#quick-pitch
- [Brief description]#brief-description
- [Examples]#examples
  - [Simplest Case]#simplest-case
  - [Different field name]#different-field-name
  - [Different field type]#different-field-type
  - [Nested structs]#nested-structs
  - [Nested collection]#nested-collection
  - [Assymetric fields (skipping and providing default values)]#assymetric-fields-skipping-and-providing-default-values
- [Expressions]#expressions
  - [Expressions for struct level instructions]#expressions-for-struct-level-instructions
  - [Expressions for member level instructions]#expressions-for-member-level-instructions
- [More examples]#more-examples
  - [Slightly complex example]#slightly-complex-example
  - [Flatened children]#flatened-children
  - [Tuple structs]#tuple-structs
  - [Struct kind hints]#struct-kind-hints
  - [Generics]#generics
  - [Where clauses]#where-clauses
  - [Mapping to multiple structs]#mapping-to-multiple-structs
  - [Avoiding proc macro attribute name collisions (alternative instruction syntax)]#avoiding-proc-macro-attribute-name-collisions-alternative-instruction-syntax
  - [Additional o2o instruction available via `#[o2o(...)]` syntax]#additional-o2o-instruction-available-via-o2o-syntax
    - [Primitive type conversions]#primitive-type-conversions
    - [Repeat instructions]#repeat-instructions
    - [Wrapper structs and `#[o2o(wrapper)]` instruction]#wrapper-structs-and-o2owrapper-instruction
    - [Wrapped structs and `#[o2o(wrapped)]` instruction]#wrapped-structs-and-o2owrapped-instruction
  - [Contributions]#contributions
    - [License]#license

## Quick pitch

```rust
impl From<Person> for PersonDto {
    fn from(value: Person) -> PersonDto {
        PersonDto {
            id: value.id,
            name: value.name,
            age: value.age,
        }
    }
}
```

Writing code like above is not the most exciting or rewarding part of working with Rust. If you're Ok with letting procedural macro write it for you, welcome to the rest of this page.

## Brief description

**o2o** procedural macro is able to generate implementation of 6 kinds of traits (currently for structs only):

``` rust
// #[from_owned()]
impl std::convert::From<A> for B { ... }

// #[from_ref()]
impl std::convert::From<&A> for B { ... }

// #[owned_into()]
impl std::convert::Into<A> for B { ... }

// #[ref_into()]
impl std::convert::Into<A> for &B { ... }

// #[owned_into_existing()]
impl o2o::traits::IntoExisting<A> for B { ... }

// #[ref_into_existing()]
impl o2o::traits::IntoExisting<A> for &B { ... }
```

It also has shortcuts to configure multiple trait implementations with fewer lines of code:

|                              | #[map()] | #[from()]  | #[into()] | #[map_owned()] | #[map_ref()] | #[into_existing()] |
| ---------------------------- | -------- | ---------- | --------- | ---------------| ------------ | -------------------|
| **#[from_owned()]**          | ✔️       | ✔️          || ✔️             |||
| **#[from_ref()]**            | ✔️       | ✔️          ||| ✔️            ||
| **#[owned_into()]**          | ✔️       || ✔️         | ✔️             |||
| **#[ref_into()]**            | ✔️       || ✔️         || ✔️            ||
| **#[owned_into_existing()]** |||||| ✔️                 |
| **#[ref_into_existing()]**   |||||| ✔️                 |

With that, let's look at some examples.

## Examples

### Simplest Case

``` rust
use o2o::o2o;

struct Person {
    id: u32,
    name: String,
    age: u8
}

#[derive(o2o)]
#[map_owned(Person)]
struct PersonDto {
    id: u32,
    name: String,
    age: u8
}
```
From here on, generated code is produced by [rust-analyzer: Expand macro recursively](https://rust-analyzer.github.io/manual.html#expand-macro-recursively) command
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<Person> for PersonDto {
      fn from(value: Person) -> PersonDto {
          PersonDto {
              id: value.id,
              name: value.name,
              age: value.age,
          }
      }
  }
  impl std::convert::Into<Person> for PersonDto {
      fn into(self) -> Person {
          Person {
              id: self.id,
              name: self.name,
              age: self.age,
          }
      }
  }
  ```
</details>

With the above code you should be able to do this:

``` rust
let person = Person { id: 123, name: "John".into(), age: 42 };
let dto: PersonDto = person.into();
// and this:
let dto = PersonDto { id: 123, name: "John".into(), age: 42 };
let person: Person = dto.into();
```

### Different field name

``` rust
struct Entity {
    some_int: i32,
    another_int: i16,
}

#[derive(o2o)]
#[from_ref(Entity)]
#[ref_into_existing(Entity)]
struct EntityDto {
    some_int: i32,
    #[map(another_int)]
    different_int: i16,
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<&Entity> for EntityDto {
      fn from(value: &Entity) -> EntityDto {
          EntityDto {
              some_int: value.some_int,
              different_int: value.another_int,
          }
      }
  }
  impl o2o::traits::IntoExisting<Entity> for &EntityDto {
      fn into_existing(self, other: &mut Entity) {
          other.some_int = self.some_int;
          other.another_int = self.different_int;
      }
  }
  ```
</details>

### Different field type

``` rust
struct Entity {
    some_int: i32,
    val: i16,
    str: String
}

#[derive(o2o)]
#[map(Entity)]
struct EntityDto {
    some_int: i32,
    #[from(~.to_string())] // Tilde allows to append code at the end of the right side of field initialization for From<T> impls
    #[into(~.parse::<i16>().unwrap())] // here it's the same but for Into<T> impls
    val: String,
    // Here Into and From are symmetric, so it has to be only specified once.
    // Note that .clone() is only needed for borrowing impls, so we use #[map_ref()]
    #[map_ref(~.clone())] 
    str: String
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<Entity> for EntityDto {
      fn from(value: Entity) -> EntityDto {
          EntityDto {
              some_int: value.some_int,
              val: value.val.to_string(),
              str: value.str, // no .clone()
          }
      }
  }
  impl std::convert::From<&Entity> for EntityDto {
      fn from(value: &Entity) -> EntityDto {
          EntityDto {
              some_int: value.some_int,
              val: value.val.to_string(),
              str: value.str.clone(),
          }
      }
  }
  impl std::convert::Into<Entity> for EntityDto {
      fn into(self) -> Entity {
          Entity {
              some_int: self.some_int,
              val: self.val.parse::<i16>().unwrap(),
              str: self.str, // no .clone()
          }
      }
  }
  impl std::convert::Into<Entity> for &EntityDto {
      fn into(self) -> Entity {
          Entity {
              some_int: self.some_int,
              val: self.val.parse::<i16>().unwrap(),
              str: self.str.clone(),
          }
      }
  }
  ```
</details>

### Nested structs

``` rust
struct Entity {
    some_int: i32,
    child: Child,
}
struct Child {
    child_int: i32,
}

#[derive(o2o)]
#[from_owned(Entity)]
struct EntityDto {
    some_int: i32,
    #[map(~.into())]
    child: ChildDto
}

#[derive(o2o)]
#[from_owned(Child)]
struct ChildDto {
    child_int: i32,
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<Entity> for EntityDto {
      fn from(value: Entity) -> EntityDto {
          EntityDto {
              some_int: value.some_int,
              child: value.child.into(),
          }
      }
  }
  
  impl std::convert::From<Child> for ChildDto {
      fn from(value: Child) -> ChildDto {
          ChildDto {
              child_int: value.child_int,
          }
      }
  }
  ```
</details>

### Nested collection

``` rust
struct Entity {
    some_int: i32,
    children: Vec<Child>,
}
struct Child {
    child_int: i32,
}

#[derive(o2o)]
#[map_owned(Entity)]
struct EntityDto {
    some_int: i32,
    // Here field name as well as type are different, so we pass in field name and tilde inline expression.
    // Also, it doesn't hurt to use member level instruction #[map()], 
    // which is broader than struct level instruction #[map_owned]
    #[map(children, ~.iter().map(|p|p.into()).collect())]
    children_vec: Vec<ChildDto>
}

#[derive(o2o)]
#[map_ref(Child)]
struct ChildDto {
    child_int: i32,
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<Entity> for EntityDto {
      fn from(value: Entity) -> EntityDto {
          EntityDto {
              some_int: value.some_int,
              children_vec: value.children.iter().map(|p| p.into()).collect(),
          }
      }
  }
  impl std::convert::Into<Entity> for EntityDto {
      fn into(self) -> Entity {
          Entity {
              some_int: self.some_int,
              children: self.children_vec.iter().map(|p| p.into()).collect(),
          }
      }
  }
  impl std::convert::From<&Child> for ChildDto {
      fn from(value: &Child) -> ChildDto {
          ChildDto {
              child_int: value.child_int,
          }
      }
  }
  impl std::convert::Into<Child> for &ChildDto {
      fn into(self) -> Child {
          Child {
              child_int: self.child_int,
          }
      }
  }
  ```
</details>

### Assymetric fields (skipping and providing default values)

**o2o** is able to handle scenarios when either of the structs has a field that the other struct doesn't have.

For the scenario where you put **o2o** instructions on a struct that contains extra field:
``` rust
struct Person {
    id: i32,
    full_name: String,
    age: i8,
}

#[derive(o2o)]
#[map_owned(Person)]
struct PersonDto {
    id: i32,
    full_name: String,
    age: i8,
    // (|| None) below provides default value when creating PersonDto from Person
    // It could have been omited if we only needed to create Person from PersonDto
    #[ghost(|| None)]
    zodiac_sign: Option<ZodiacSign>
}
enum ZodiacSign {}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<Person> for PersonDto {
      fn from(value: Person) -> PersonDto {
          PersonDto {
              id: value.id,
              full_name: value.full_name,
              age: value.age,
              zodiac_sign: (|| None)(),
          }
      }
  }
  impl std::convert::Into<Person> for PersonDto {
      fn into(self) -> Person {
          Person {
              id: self.id,
              full_name: self.full_name,
              age: self.age,
          }
      }
  }
  ```
</details>

In a reverse case, you need to use a struct level `#[ghost()]` instruction:
``` rust
#[derive(o2o)]
#[map_owned(PersonDto)]
#[ghost(zodiac_sign: || { None })] // #[ghost()] struct level instruction accepts only braced closures.
struct Person {
    id: i32,
    full_name: String,
    age: i8,
}

struct PersonDto {
    id: i32,
    full_name: String,
    age: i8,
    zodiac_sign: Option<ZodiacSign>
}
enum ZodiacSign {}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<PersonDto> for Person {
      fn from(value: PersonDto) -> Person {
          Person {
              id: value.id,
              full_name: value.full_name,
              age: value.age,
          }
      }
  }
  impl std::convert::Into<PersonDto> for Person {
      fn into(self) -> PersonDto {
          PersonDto {
              id: self.id,
              full_name: self.full_name,
              age: self.age,
              zodiac_sign: (|| None)(),
          }
      }
  }
  ```
</details>

## Expressions

### Expressions for struct level instructions

```rust
#[ghost(field: { None })]

// field: None,

#[ghost(field: || { None })]

// field: (|| None)(),

#[ghost(field: { @.get_value() })]

// field: self.get_value(),

#[ghost(field: |x| { x.get_value() })]

// field: (|x: &Type| x.get_value())(&self),
```

### Expressions for member level instructions

```rust
#[map({ None })]

// field: None,

#[map(~.clone())]

// field: value.field.clone(),

#[map(@.get_value())]

// field: value.get_value(),

#[map(|x| x.get_value())]

// field: (|x: &Type| x.get_value())(&value),
```

## More examples

### Slightly complex example

``` rust
struct Employee {
    id: i32,
    first_name: String,
    last_name: String,
    subordinate_of: Box<Employee>,
    subordinates: Vec<Box<Employee>>
}
impl Employee {
    fn get_full_name(&self) -> String {
        format!("{} {}", self.first_name, self.last_name)
    }
}

#[derive(o2o)]
#[map(Employee)]
#[ghost(
    // o2o supports closures with one input parameter.
    // This parameter represents instance on the other side of the conversion.
    first_name: |x| {x.get_first_name()},
    last_name: |x| {x.get_last_name()}
)]
struct EmployeeDto {
    #[map(id)]
    employee_id: i32,
    // '@.' is another flavor of 'inline expression'. 
    // @ also represents instance on the other side of the conversion.
    #[ghost(@.get_full_name())]
    full_name: String,

    #[from(|x| Box::new(x.subordinate_of.as_ref().into()))]
    #[into(subordinate_of, |x| Box::new(x.reports_to.as_ref().into()))]
    reports_to: Box<EmployeeDto>,

    #[map(~.iter().map(|p|Box::new(p.as_ref().into())).collect())]
    subordinates: Vec<Box<EmployeeDto>>
}
impl EmployeeDto {
    fn get_first_name(&self) -> String {
        self.full_name.split_whitespace().collect::<Vec<&str>>()[0].into()
    }
    fn get_last_name(&self) -> String {
        self.full_name.split_whitespace().collect::<Vec<&str>>()[1].into()
    }
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<Employee> for EmployeeDto {
      fn from(value: Employee) -> EmployeeDto {
          EmployeeDto {
              employee_id: value.id,
              full_name: value.get_full_name(),
              reports_to: (|x: &Employee| Box::new(x.subordinate_of.as_ref().into()))(&value),
              subordinates: value.subordinates.iter().map(|p| Box::new(p.as_ref().into())).collect(),
          }
      }
  }
  impl std::convert::From<&Employee> for EmployeeDto {
      fn from(value: &Employee) -> EmployeeDto {
          EmployeeDto {
              employee_id: value.id,
              full_name: value.get_full_name(),
              reports_to: (|x: &Employee| Box::new(x.subordinate_of.as_ref().into()))(value),
              subordinates: value.subordinates.iter().map(|p| Box::new(p.as_ref().into())).collect(),
          }
      }
  }
  impl std::convert::Into<Employee> for EmployeeDto {
      fn into(self) -> Employee {
          Employee {
              id: self.employee_id,
              subordinate_of: (|x: &EmployeeDto| Box::new(x.reports_to.as_ref().into()))(&self),
              subordinates: self.subordinates.iter().map(|p| Box::new(p.as_ref().into())).collect(),
              first_name: (|x: &EmployeeDto| x.get_first_name())(&self),
              last_name: self.get_last_name(),
          }
      }
  }
  impl std::convert::Into<Employee> for &EmployeeDto {
      fn into(self) -> Employee {
          Employee {
              id: self.employee_id,
              subordinate_of: (|x: &EmployeeDto| Box::new(x.reports_to.as_ref().into()))(self),
              subordinates: self.subordinates.iter().map(|p| Box::new(p.as_ref().into())).collect(),
              first_name: (|x: &EmployeeDto| x.get_first_name())(self),
              last_name: self.get_last_name(),
          }
      }
  }
  ```
</details>

### Flatened children

When the instructions are put on the side that contains flatened properties, conversion `From<T>` and `IntoExisting<T>` only requires usage of a member level `#[child(...)]` instruction, which accepts a path to the unflatened field (*without* the field name itself).
``` rust
struct Car {
    number_of_doors: i8,
    vehicle: Vehicle
}
struct Vehicle {
    number_of_seats: i16,
    machine: Machine,
}
struct Machine {
    brand: String,
    year: i16
}

#[derive(o2o)]
#[from_owned(Car)]
#[ref_into_existing(Car)]
struct CarDto {
    number_of_doors: i8,

    #[child(vehicle)]
    number_of_seats: i16,

    #[child(vehicle.machine)]
    #[map_ref(~.clone())]
    brand: String,

    #[child(vehicle.machine)]
    year: i16
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<Car> for CarDto {
      fn from(value: Car) -> CarDto {
          CarDto {
              number_of_doors: value.number_of_doors,
              number_of_seats: value.vehicle.number_of_seats,
              brand: value.vehicle.machine.brand,
              year: value.vehicle.machine.year,
          }
      }
  }
  impl o2o::traits::IntoExisting<Car> for &CarDto {
      fn into_existing(self, other: &mut Car) {
          other.number_of_doors = self.number_of_doors;
          other.vehicle.number_of_seats = self.number_of_seats;
          other.vehicle.machine.brand = self.brand.clone();
          other.vehicle.machine.year = self.year;
      }
  }
  ```
</details>

When you need an `Into<T>` conversion, **o2o** also expects you to provide types for flatened properties via struct level `#[children(...)]` instruction:

``` rust
#[derive(o2o)]
#[owned_into(Car)]
#[children(vehicle: Vehicle, vehicle.machine: Machine)]
struct CarDto {
    number_of_doors: i8,

    #[child(vehicle)]
    number_of_seats: i16,

    #[child(vehicle.machine)]
    brand: String,

    #[child(vehicle.machine)]
    year: i16
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::Into<Car> for CarDto {
      fn into(self) -> Car {
          Car {
              number_of_doors: self.number_of_doors,
              vehicle: Vehicle {
                  number_of_seats: self.number_of_seats,
                  machine: Machine {
                      brand: self.brand,
                      year: self.year,
                  },
              },
          }
      }
  }
  ```
</details>

The reverse case, where you have to put **o2o** insturctions on the side that has field that are being flatened, is slightly tricky:

``` rust
#[derive(o2o)]
#[owned_into(CarDto)]
struct Car {
    number_of_doors: i8,
    #[parent]
    vehicle: Vehicle
}

#[derive(o2o)]
#[owned_into_existing(CarDto)]
struct Vehicle {
    number_of_seats: i16,
    #[parent]
    machine: Machine,
}

#[derive(o2o)]
#[owned_into_existing(CarDto)]
struct Machine {
    brand: String,
    year: i16
}

// CarDto has to implement `Default` trait in this case.
#[derive(Default)]
struct CarDto {
    number_of_doors: i8,
    number_of_seats: i16,
    brand: String,
    year: i16
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::Into<CarDto> for Car {
      fn into(self) -> CarDto {
          let mut obj = CarDto {
              number_of_doors: self.number_of_doors,
              ..Default::default()
          };
          self.vehicle.into_existing(&mut obj);
          obj
      }
  }
  impl o2o::traits::IntoExisting<CarDto> for Vehicle {
      fn into_existing(self, other: &mut CarDto) {
          other.number_of_seats = self.number_of_seats;
          self.machine.into_existing(other);
      }
  }
  impl o2o::traits::IntoExisting<CarDto> for Machine {
      fn into_existing(self, other: &mut CarDto) {
          other.brand = self.brand;
          other.year = self.year;
      }
  }
  ```
</details>

### Tuple structs

``` rust
struct TupleEntity(i32, String);

#[derive(o2o)]
#[map_ref(TupleEntity)]
struct TupleEntityDto(i32, #[map_ref(~.clone())] String);
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<&TupleEntity> for TupleEntityDto {
      fn from(value: &TupleEntity) -> TupleEntityDto {
          TupleEntityDto(value.0, value.1.clone())
      }
  }
  impl std::convert::Into<TupleEntity> for &TupleEntityDto {
      fn into(self) -> TupleEntity {
          TupleEntity(self.0, self.1.clone())
      }
  }
  ```
</details>

As long as Rust allows following syntax, easy conversion between tuple and named structs can be done if placing **o2o** instructions on named side:

``` rust
struct TupleEntity(i32, String);

#[derive(o2o)]
#[map_ref(TupleEntity)]
struct EntityDto {
    #[map_ref(0)]
    some_int: i32, 
    #[map_ref(1, ~.clone())]
    some_str: String
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<&TupleEntity> for EntityDto {
      fn from(value: &TupleEntity) -> EntityDto {
          EntityDto {
              some_int: value.0,
              some_str: value.1.clone(),
          }
      }
  }
  impl std::convert::Into<TupleEntity> for &EntityDto {
      fn into(self) -> TupleEntity {
          TupleEntity {
              0: self.some_int,
              1: self.some_str.clone(),
          }
      }
  }
  ```
</details>

### Struct kind hints

By default, **o2o** will suppose that the struct on the other side is the same kind of struct that the original one is. In order to convert between named and tuple structs when you need to place instructions on a tuple side, you`ll need to use Struct Kind Hint:

``` rust
#[derive(o2o)]
#[map_owned(EntityDto as {})]
struct TupleEntity(#[map(some_int)] i32, #[map(some_str)] String);

struct EntityDto{
    some_int: i32, 
    some_str: String
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<EntityDto> for TupleEntity {
      fn from(value: EntityDto) -> TupleEntity {
          TupleEntity(value.some_int, value.some_str)
      }
  }
  impl std::convert::Into<EntityDto> for TupleEntity {
      fn into(self) -> EntityDto {
          EntityDto {
              some_int: self.0,
              some_str: self.1,
          }
      }
  }
  ```
</details>

### Generics

``` rust
struct Entity<T> {
    some_int: i32,
    something: T,
}

#[derive(o2o)]
#[map_owned(Entity::<f32>)]
struct EntityDto {
    some_int: i32,
    something: f32
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<Entity<f32>> for EntityDto {
      fn from(value: Entity<f32>) -> EntityDto {
          EntityDto {
              some_int: value.some_int,
              something: value.something,
          }
      }
  }
  impl std::convert::Into<Entity<f32>> for EntityDto {
      fn into(self) -> Entity<f32> {
          Entity::<f32> {
              some_int: self.some_int,
              something: self.something,
          }
      }
  }
  ```
</details>

### Where clauses

``` rust
struct Child<T> {
    child_int: i32,
    something: T,
}

#[derive(o2o)]
#[map_owned(Child::<T>)]
#[where_clause(T: Clone)]
struct ChildDto<T> {
    child_int: i32,
    #[map(something, ~.clone())]
    stuff: T,
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl<T> std::convert::From<Child<T>> for ChildDto<T> where T: Clone, {
      fn from(value: Child<T>) -> ChildDto<T> {
          ChildDto {
              child_int: value.child_int,
              stuff: value.something.clone(),
          }
      }
  }
  impl<T> std::convert::Into<Child<T>> for ChildDto<T> where T: Clone, {
      fn into(self) -> Child<T> {
          Child::<T> {
              child_int: self.child_int,
              something: self.stuff.clone(),
          }
      }
  }
  ```
</details>

### Mapping to multiple structs

``` rust
struct Person {
    full_name: String,
    age: i32,
    country: String,
}

struct PersonModel {
    full_name: String,
    age: i32,
    place_of_birth: String,
}

#[derive(o2o)]
#[ref_into(Person)]
#[ref_into(PersonModel)]
struct PersonDto {
    // 'Default' member level instruction applies to all types
    #[into(full_name, ~.clone())]
    name: String,
    age: i32,
    // 'Dedicated' member level instruction applies to a specific type only
    #[into(Person| country, ~.clone())]
    #[into(PersonModel| ~.clone())]
    place_of_birth: String,
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::Into<Person> for &PersonDto {
      fn into(self) -> Person {
          Person {
              full_name: self.name.clone(),
              age: self.age,
              country: self.place_of_birth.clone(),
          }
      }
  }
  impl std::convert::Into<PersonModel> for &PersonDto {
      fn into(self) -> PersonModel {
          PersonModel {
              full_name: self.name.clone(),
              age: self.age,
              place_of_birth: self.place_of_birth.clone(),
          }
      }
  }
  ```
</details>

### Avoiding proc macro attribute name collisions (alternative instruction syntax)

**o2o** proc macro declares a lot of attributes, some of which have pretty broad meaning (e.g. from, into, map, child, parent etc.), so if you have to use it with some other proc macro, there is a chance that these attributes can collide and it would not be clear to what proc macro they should apply.
For this scenario, **o2o** supports two alternative syntaxes (syntacies?):

Below, all three variants of **o2o** proc macro application will produce the same generated code:
``` rust
struct Entity {
    some_int: i32,
    val: i16,
    str: String
}
// =====================================================================
#[derive(o2o)]
#[map(Entity)]
struct EntityDto {
    some_int: i32,
    #[from(~.to_string())]
    #[into(~.parse::<i16>().unwrap())]
    val: String,
    #[map_ref(~.clone())] 
    str: String
}
// =====================================================================
#[derive(o2o)]
#[o2o(map(Entity))]
struct EntityDto {
    some_int: i32,
    #[o2o(from(~.to_string()))]
    #[o2o(into(~.parse::<i16>().unwrap()))]
    val: String,
    #[o2o(map_ref(~.clone()))] 
    str: String
}
// =====================================================================
#[derive(o2o)]
#[o2o(map(Entity))]
struct EntityDto {
    some_int: i32,
    #[o2o(
        from(~.to_string()),
        into(~.parse::<i16>().unwrap()),
    )]
    val: String,
    #[o2o(map_ref(~.clone()))] 
    str: String
}
// =====================================================================
```

This syntax applies to all supported struct and member level instructions.

### Additional o2o instruction available via `#[o2o(...)]` syntax

#### Primitive type conversions

``` rust
struct Entity {
    some_int: i32,
    some_float: f32
}

#[derive(o2o)]
#[o2o(map_ref(Entity))]
struct EntityDto {
    #[o2o(as_type(i32))]
    some_int: i16,
    #[o2o(as_type(some_float, f32))]
    another_int: i16
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<&Entity> for EntityDto {
      fn from(value: &Entity) -> EntityDto {
          EntityDto {
              some_int: value.some_int as i16,
              another_int: value.some_float as i16,
          }
      }
  }
  impl std::convert::Into<Entity> for &EntityDto {
      fn into(self) -> Entity {
          Entity {
              some_int: self.some_int as i32,
              some_float: self.another_int as f32,
          }
      }
  }
  ```
</details>

This will work with all types that support 'as' conversion.

#### Repeat instructions

```rust
struct Car {
    number_of_doors: i8,
    vehicle: Vehicle
}
struct Vehicle {
    number_of_seats: i16,
    can_fly: bool,
    needs_driver: bool,
    horsepower: i32,
    top_speed: f32,
    machine: Machine,
}
struct Machine {
    id: i32,
    brand: String,
    year: i16,
    weight: f32,
    length: f32,
    width: f32,
    height: f32,
}

#[derive(o2o)]
#[map_ref(Car)]
#[children(vehicle: Vehicle, vehicle.machine: Machine)]
#[ghost(vehicle.machine@id: || { 321 })]
struct CarDto {
    number_of_doors: i8,

    // #[o2o(repeat)] will repeat all instructions for this member to the following members, 
    // until there is a #[o2o(stop_repeat)] or the members run out.
    #[o2o(repeat)] #[child(vehicle)]
    number_of_seats: i16,
    can_fly: bool,
    needs_driver: bool,
    horsepower: i32,
    top_speed: f32,
    #[o2o(stop_repeat)]

    // You can also specify what specific types of instructions to repeat
    // (supported values are 'map', 'child', 'parent', 'ghost')
    #[o2o(repeat(child))] #[child(vehicle.machine)]
    #[map(~.clone())]
    brand: String,
    year: i16,
    weight: f32,
    length: f32,
    width: f32,
    height: f32,
    #[o2o(stop_repeat)]

    #[o2o(repeat)] #[ghost(|| {123})]
    useless_param: i32,
    useless_param_2: i32,
    useless_param_3: i32,
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<&Car> for CarDto {
      fn from(value: &Car) -> CarDto {
          CarDto {
              number_of_doors: value.number_of_doors,
              number_of_seats: value.vehicle.number_of_seats,
              can_fly: value.vehicle.can_fly,
              needs_driver: value.vehicle.needs_driver,
              horsepower: value.vehicle.horsepower,
              top_speed: value.vehicle.top_speed,
              brand: value.vehicle.machine.brand.clone(),
              year: value.vehicle.machine.year,
              weight: value.vehicle.machine.weight,
              length: value.vehicle.machine.length,
              width: value.vehicle.machine.width,
              height: value.vehicle.machine.height,
              useless_param: (|| 123)(),
              useless_param_2: (|| 123)(),
              useless_param_3: (|| 123)(),
          }
      }
  }
  impl std::convert::Into<Car> for &CarDto {
      fn into(self) -> Car {
          Car {
              number_of_doors: self.number_of_doors,
              vehicle: Vehicle {
                  number_of_seats: self.number_of_seats,
                  can_fly: self.can_fly,
                  needs_driver: self.needs_driver,
                  horsepower: self.horsepower,
                  top_speed: self.top_speed,
                  machine: Machine {
                      brand: self.brand.clone(),
                      year: self.year,
                      weight: self.weight,
                      length: self.length,
                      width: self.width,
                      height: self.height,
                      id: (|| 321)(),
                  },
              },
          }
      }
  }
  ```
</details>

#### Wrapper structs and `#[o2o(wrapper)]` instruction

```rust
#[derive(o2o)]
#[map_owned(Vec<u8>)]
struct PayloadWrapper {
    #[o2o(wrapper)]
    payload: Vec<u8>,
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<Vec<u8>> for PayloadWrapper {
      fn from(value: Vec<u8>) -> PayloadWrapper {
          PayloadWrapper { payload: value }
      }
  }
  impl std::convert::Into<Vec<u8>> for PayloadWrapper {
      fn into(self) -> Vec<u8> {
          self.payload
      }
  }
  ```
</details>

If you need 'ref' implementation, you may need to provide an inline expression:

```rust
#[derive(o2o)]
#[map(String)]
struct StringWrapper {
    #[o2o(wrapper(~.clone()))]
    str: String
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<String> for StringWrapper {
      fn from(value: String) -> StringWrapper {
          StringWrapper { str: value }
      }
  }
  impl std::convert::From<&String> for StringWrapper {
      fn from(value: &String) -> StringWrapper {
          StringWrapper { str: value.clone() }
      }
  }
  impl std::convert::Into<String> for StringWrapper {
      fn into(self) -> String {
          self.str
      }
  }
  impl std::convert::Into<String> for &StringWrapper {
      fn into(self) -> String {
          self.str.clone()
      }
  }
  ```
</details>

#### Wrapped structs and `#[o2o(wrapped)]` instruction

```rust
#[derive(o2o)]
#[map_owned(StuffWrapper as {})]
#[o2o(wrapped(payload))]
struct Stuff(i32);

struct StuffWrapper {
    payload: Stuff,
}
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<StuffWrapper> for Stuff {
      fn from(value: StuffWrapper) -> Stuff {
          value.payload
      }
  }
  impl std::convert::Into<StuffWrapper> for Stuff {
      fn into(self) -> StuffWrapper {
          StuffWrapper { payload: self }
      }
  }
  ```
</details>

'Ref' implementation

```rust
#[derive(Clone, o2o)]
#[map(StuffWrapper as ())]
#[o2o(wrapped(0, ~.clone()))]
struct Stuff {
    thing: i32
}

struct StuffWrapper(Stuff);
```
<details>
  <summary>View generated code</summary>

  ``` rust
  impl std::convert::From<StuffWrapper> for Stuff {
      fn from(value: StuffWrapper) -> Stuff {
          value.0
      }
  }
  impl std::convert::From<&StuffWrapper> for Stuff {
      fn from(value: &StuffWrapper) -> Stuff {
          value.0.clone()
      }
  }
  impl std::convert::Into<StuffWrapper> for Stuff {
      fn into(self) -> StuffWrapper {
          StuffWrapper(self)
      }
  }
  impl std::convert::Into<StuffWrapper> for &Stuff {
      fn into(self) -> StuffWrapper {
          StuffWrapper(self.clone())
      }
  }
  ```
</details>


### Contributions

All issues, questions, pull requests are extremely welcome.

#### License

<sup>
Licensed under either an <a href="LICENSE-APACHE">Apache License, Version
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
</sup>

<br>

<sub>
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
be dual licensed as above, without any additional terms or conditions.
</sub>