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
extern crate o2o_impl;

use proc_macro::TokenStream;
use o2o_impl::expand::derive;
use syn::{parse_macro_input, DeriveInput};

/// ### Object to Object mapper for Rust
/// **o2o** can implement `std::convert::From<T>`, `std::convert::Into<T>`, and custom `o2o::traits::IntoExisting<T>` traits via procedural macro. It can be best explained through examples, so...
///
/// #### Simplest Case
/// 
/// ``` rust
/// use o2o::o2o;
/// 
/// struct Entity {
///     some_int: i32,
///     another_int: i16,
/// }
/// 
/// #[derive(o2o)]
/// #[map(Entity)]
/// struct EntityDto {
///     some_int: i32,
///     another_int: i16,
/// }
/// ```
/// For the above code **o2o** will generate following trait impls:
///
/// ``` rust
/// impl std::convert::From<Entity> for EntityDto { ... }
/// impl std::convert::From<&Entity> for EntityDto { ... }
/// impl std::convert::Into<Entity> for EntityDto { ... }
/// impl std::convert::Into<Entity> for &EntityDto { ... }
/// ```
/// #### Different field name
/// 
/// ``` rust
/// struct Entity {
///     some_int: i32,
///     another_int: i16,
/// }
/// 
/// #[derive(o2o)]
/// #[map(Entity)]
/// struct EntityDto {
///     some_int: i32,
///     #[map(another_int)]
///     different_int: i16,
/// }
/// ```
/// 
/// #### Different field type
/// 
/// ``` rust
/// struct Entity {
///     some_int: i32,
///     value: i16,
/// }
/// 
/// #[derive(o2o)]
/// #[map(Entity)]
/// struct EntityDto {
///     some_int: i32,
///     #[from(value.to_string())] //here `value` is a field of Entity struct
///     #[into(value.parse::<i16>().unwrap())] //here `value` is a field of EntityDto struct
///     value: String,
/// }
/// ```
/// #### Nested structs
/// 
/// ``` rust
/// struct Entity {
///     some_int: i32,
///     child: Child,
/// }
/// struct Child {
///     child_int: i32,
/// }
/// 
/// #[derive(o2o)]
/// #[map_owned(Entity)]
/// struct EntityDto {
///     some_int: i32,
///     #[map(child.into())]
///     child: ChildDto
/// }
/// 
/// #[derive(o2o)]
/// #[map_owned(Child)]
/// struct ChildDto {
///     child_int: i32,
/// }
/// ```
/// 
/// #### Nested collection
/// 
/// ``` rust
/// struct Entity {
///     some_int: i32,
///     children: Vec<Child>,
/// }
/// struct Child {
///     child_int: i32,
/// }
/// 
/// #[derive(o2o)]
/// #[map(Entity)]
/// struct EntityDto {
///     some_int: i32,
///     #[map(children.iter().map(|p|p.into()).collect())]
///     children: Vec<ChildDto>
/// }
/// 
/// #[derive(o2o)]
/// #[map(Child)]
/// struct ChildDto {
///     child_int: i32,
/// }
/// ```
/// 
/// For more examples, visit [github.com](https://github.com/Artem-Romanenia/o2o)
#[proc_macro_derive(o2o, attributes(
    owned_into, ref_into, into, 
    from_owned, from_ref, from, 
    map_owned, map_ref, map, 
    owned_into_existing, ref_into_existing, into_existing,
    child, children, parent, ghost, panic_debug_info, where_clause, o2o))]
pub fn derive_o2o(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    derive(&input)
        .unwrap_or_else(|err| err.to_compile_error())
        .into()
}