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
//! Derive a highly configurable constructor for your struct
//!
//! [](https://github.com/Alorel/fancy_constructor-rs/actions/workflows/test.yml?query=branch%3Amaster)
//! [](https://crates.io/crates/fancy_constructor)
//! [](https://libraries.io/cargo/fancy_constructor)
//! [](https://coveralls.io/github/Alorel/fancy_constructor-rs)
//!
//! # Examples
//!
//! <details><summary>Basic</summary>
//!
//! ```
//! use fancy_constructor::new;
//! #[derive(new, PartialEq, Eq, Debug)]
//! struct MyStruct {
//! foo: String,
//! bar: u8,
//! }
//!
//! let a = MyStruct::new("#[derive(new)]".into(), 55);
//! let b = MyStruct { foo: "#[derive(new)]".into(), bar: 55 };
//! assert_eq!(a, b);
//! ```
//!
//! Outputs:
//! ```
//! impl MyStruct {
//! pub fn new(foo: String, bar: u8) -> Self {
//! Self { foo, bar }
//! }
//! }
//! ````
//!
//! </details>
//! <details><summary>Options showcase</summary>
//!
//! ```
//! # use fancy_constructor::new;
//! # use std::sync::Arc;
//! # #[derive(Eq, PartialEq, Copy, Clone, Default, Debug)]
//! # struct Whatever;
//! #[derive(new, PartialEq, Eq, Debug)]
//! #[new(vis(pub(crate)), name(construct), comment("Foo"), bounds(T: Clone))]
//! struct MyStruct<T> {
//! #[new(into)]
//! a: T,
//!
//! #[new(val("Bar".into()))]
//! b: String,
//!
//! #[new(clone)]
//! c: Arc<Whatever>,
//!
//! #[new(default)]
//! d: Vec<u8>,
//! }
//!
//! let we = Arc::new(Whatever::default());
//! let a = MyStruct::<String>::construct("A", &we);
//! let b = MyStruct {a: "A".into(), b: "Bar".into(), c: we, d: vec![]};
//! assert_eq!(a, b);
//! ```
//!
//! Outputs:
//!
//! ```
//! impl<T> MyStruct<T> {
//! /// Foo
//! pub(crate) fn construct(a: impl Into<T>, c: &Arc<Whatever>) -> Self where T: Clone {
//! Self {
//! a: a.into(),
//! b: "Bar".into(),
//! c: c.clone(),
//! d: Default::default(),
//! }
//! }
//! }
//! ````
//!
//! </details>
//! <details><summary>Private const fn</summary>
//!
//! ```
//! # use fancy_constructor::new;
//! #[derive(new, PartialEq, Eq, Debug)]
//! #[new(const_fn, vis())]
//! struct Foo(u8);
//!
//! const FOO: Foo = Foo::new(128);
//! assert_eq!(FOO, Foo(128));
//! ```
//!
//! Outputs:
//!
//! ```
//! impl Foo {
//! const fn new(f1: u8) -> Self {
//! Self(f1)
//! }
//! }
//! ````
//!
//! </details>
//! <details><summary>Computed values</summary>
//!
//! ```
//! # use fancy_constructor::new;
//! #[derive(new)]
//! struct Foo {
//! is_bar: bool,
//! #[new(val(if is_bar { 100 } else { 5 }))]
//! barness_level: u8,
//! }
//!
//! assert_eq!(Foo::new(true).barness_level, 100);
//! assert_eq!(Foo::new(false).barness_level, 5);
//! ```
//!
//! </details>
//!
//! <details><summary>Custom constructor args</summary>
//!
//! ```
//! # use fancy_constructor::new;
//! #[derive(new)]
//! #[new(args(input_string: &str))]
//! struct Foo {
//! #[new(val(input_string.to_lowercase()))]
//! pub lowercase: String,
//!
//! #[new(val(input_string.to_uppercase()))]
//! pub uppercase: String,
//! }
//!
//! let foo = Foo::new("Foo");
//! assert_eq!(foo.lowercase.as_str(), "foo");
//! assert_eq!(foo.uppercase.as_str(), "FOO");
//! ```
//!
//! </details>
//! <details><summary>Renaming constructor args</summary>
//!
//! ```
//! # use fancy_constructor::new;
//! #[derive(new)]
//! struct MyNewtype(#[new(name(my_value))] u8);
//! ```
//!
//! Outputs:
//!
//! ```
//! impl MyNewtype {
//! pub fn new(my_value: u8) -> Self {
//! Self(my_value)
//! }
//! }
//! ````
//!
//! </details>
//! <details><summary>Enums</summary>
//!
//! ```
//! # use fancy_constructor::new;
//! #[derive(new, Eq, PartialEq, Debug)]
//! enum MyEnum {
//! #[new]
//! Foo { #[new(into)] bar: u8 },
//! Qux,
//! }
//!
//! assert_eq!(MyEnum::new(5), MyEnum::Foo { bar: 5 });
//! ```
//!
//! Outputs:
//!
//! ```
//! impl MyEnum {
//! pub fn new(bar: Into<u8>) -> Self {
//! Self::Foo { bar: bar.into() }
//! }
//! }
//! ````
//!
//! </details>
//!
//! <details><summary>Deriving the Default trait</summary>
//!
//! The [`Default`](::core::default::Default) trait can be derived if the constructor ends up with no arguments:
//!
//! ```
//! # use fancy_constructor::new;
//! #[derive(new, PartialEq, Eq, Debug)]
//! #[new(default, name(construct))]
//! struct Foo {
//! #[new(val(u8::MAX))]
//! bar: u8,
//! }
//!
//! assert_eq!(Foo::construct(), Foo::default());
//! ```
//!
//! Outputs:
//!
//! ```
//! impl Default for Foo {
//! fn default() -> Self {
//! Foo::construct()
//! }
//! }
//! ````
//!
//! Attempting to use the option when the constructor has arguments will result in a compile error:
//!
//! ```compile_fail
//! # use fancy_constructor::new;
//! #[derive(new)]
//! #[new(default)]
//! struct Foo {
//! bar: u8,
//! }
//! ```
//!
//! </details>
//!
//! <details><summary>Invalid inputs</summary>
//!
//! ```compile_fail
//! #[derive(fancy_constructor::new)]
//! enum Foo {
//! Bar, // no variants marked with `#[new]`
//! }
//! ```
//!
//! ```compile_fail
//! #[derive(fancy_constructor::new)]
//! enum Foo {
//! #[new] Bar, // multiple variants marked with `#[new]`
//! #[new] Qux,
//! }
//! ```
//!
//! ```compile_fail
//! #[derive(fancy_constructor::new)]
//! union Foo { // Unions not supported
//! bar: u8,
//! qux: u8,
//! }
//! ```
//!
//! </details>
extern crate proc_macro;
use crateFieldsSource;
use Ident;
use ;
const ATTR_NAME: &str = "new";
/// See [crate-level docs](crate) for a usage and output example.
///
/// # Container options
///
/// | Opt | Default | Description |
/// | --- | --- | --- |
/// | `new(const_fn)` | `false` | Whether to make the constructor a `const fn` |
/// | `default` | `false` | Generate a [`Default`](::core::default::Default) implementation if the constructor ends up with no arguments. |
/// | `new(vis(visibility))` | `pub` | The visibility of the constructor |
/// | `new(name(ident))` | `new` | Constructor fn name |
/// | `new(comment(literal))` | | A doc comment to add to the constructor |
/// | `new(bounds(T: Whatever))` | | Generic type bounds for the implementation |
/// | `new(args(foo: u8, bar: String))` | | Additional arguments to add to the **beginning** of the args list |
///
/// # Field options
///
/// | Opt | Description |
/// | --- | --- |
/// | `new(default)` | Omit the field from the constructor and use [`Default`](::core::default::Default) |
/// | `new(clone)` | Make the argument pass-by-reference and clone it |
/// | `new(into)` | Make the argument [`Into<T>`](::core::convert::Into) |
/// | `new(name(ident))` | Rename the function argument - useful for newtype structs |
/// | `new(val(expr))` | Initialise the value with the following expression instead of a constructor argument |
///
type FmtTuple = ;