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
//! Derive macros for some common patterns
//!
//! The currently implemented patterns are:
//! - Builder
//! - From
//!
//! # Builder
//! for the `Builder` macro it generates an impl with methods of the form:
//! ```text
//! fn field(mut self, value: impl Into<Type>) -> Self {
//! self.field = value.into()
//! self
//! }
//! ```
//!
//! An example of the generated code for a struct is:
//! ```text
//! #[derive(Default, Builder)]
//! struct Example {
//! item: String,
//! value: usize,
//! }
//!
//! // generated impl
//! impl Example {
//! fn item(mut self, value: impl Into<String>) -> Self {
//! self.item = value.into();
//! self
//! }
//!
//! fn value(mut self, value: impl Into<usize>) -> Self {
//! self.value = value.into();
//! self
//! }
//! }
//!
//! // using the values
//! fn func() {
//! let ex = Example::default()
//! .item("something")
//! .value(1);
//! ...
//! }
//! ```
//!
//! if you want to not include a field in the builder pattern use the `skip` attribute:
//! ```text
//! #[derive(Builder)]
//! struct Example {
//! #[builder(skip)]
//! item: String,
//! value: usize,
//! }
//! ```
//!
//! if you do not want to have the `Into` use the `no_into` attribute:
//! ```text
//! #[derive(Builder)]
//! struct Example {
//! #[builder(no_into)]
//! item: String,
//! value: usize,
//! }
//! ```
//!
//! if you need to alter the names of the associated methods use `prefix` and/or `rename`
//! attributes.
//! ```text
//! #[derive(Builder)]
//! #[builder(prefix = "set_")]
//! struct Example {
//! item: String,
//! #[builder(rename = "num")]
//! value: usize,
//! }
//!
//! // will generate
//! impl Example {
//! fn set_item(mut self, ..) -> Self {..}
//! fn num(mut self, ..) -> Self {..}
//! }
//! ```
//!
//! The Builder pattern is not defined for enums, unit-like struct, newtypes, and tuple structs
//!
//! # From
//! For the `From` derive it implements the trivial `From<Type>` implementations:
//! ```text
//! #[derive(From)]
//! enum Example {
//! Empty,
//! Number(f32),
//! Pair(String, String),
//! }
//!
//! // will generate
//! impl From<()> for Example {
//! fn from(value: ()) -> Self {
//! Example::Empty
//! }
//! }
//! impl From<f32> for Example {
//! fn from(value: f32) -> Self {
//! Example::Number(f32)
//! }
//! }
//! impl From<(String, String)> for Example {
//! fn from(value: (String, String)) -> Self {
//! Example::Pair(value.0, value.1)
//! }
//! }
//! ```
//!
//! For struct datatypes it uses tuples as the type to convert from:
//! ```text
//! #[derive(From)]
//! struct Example {
//! item: usize
//! value: String
//! }
//!
//! // generates
//! impl From<(usize, String)> for Example {
//! fn from(value: (usize, String)) -> Self {
//! Example {
//! item: value.0,
//! value: value.1,
//! }
//! }
//! }
//! ```
//!
//! If you need to not generate a `From` implementation use the `skip` attribute
//! ```text
//! #[derive(From)]
//! enum Example {
//! #[from(skip)]
//! Empty,
//! Number(f32),
//! Pair(String, String),
//! }
//! ```
use TokenStream;
use parse_macro_input;
use DeriveInput;
use impl_builder;
use impl_from;