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
use super::GenEnum;
use super::GenStruct;
use super::GenerateMod;
use super::Impl;
use super::ImplFor;
use super::StreamBuilder;
use super::StringOrIdent;
use crate::parse::GenericConstraints;
use crate::parse::Generics;
use crate::prelude::Ident;
use crate::prelude::TokenStream;
#[must_use]
/// The generator is used to generate code.
///
/// Often you will want to use [`impl_for`] to generate an `impl <trait_name> for <target_name()>`.
///
/// [`impl_for`]: #method.impl_for
pub struct Generator {
name: Ident,
generics: Option<Generics>,
generic_constraints: Option<GenericConstraints>,
stream: StreamBuilder,
}
impl Generator {
pub(crate) fn new(
name: Ident,
generics: Option<Generics>,
generic_constraints: Option<GenericConstraints>,
) -> Self {
Self {
name,
generics,
generic_constraints,
stream: StreamBuilder::new(),
}
}
/// Return the name for the struct or enum that this is going to be implemented on.
#[must_use]
pub fn target_name(&self) -> Ident {
self.name.clone()
}
/// Generate an `impl <target_name>` implementation. See [`Impl`] for more information.
///
/// This will default to the type that is associated with this generator. If you need to generate an impl for another type you can use `impl_for_other_type`
pub fn r#impl(&mut self) -> Impl<'_, Self> {
Impl::with_parent_name(self)
}
/// Generate an `impl <target_name>` implementation. See [`Impl`] for more information.
///
/// Alias for [`impl`] which doesn't need a `r#` prefix.
///
/// [`impl`]: #method.impl
pub fn generate_impl(&mut self) -> Impl<'_, Self> {
Impl::with_parent_name(self)
}
/// Generate an `for <trait_name> for <target_name>` implementation. See [`ImplFor`] for more information.
///
/// This will default to the type that is associated with this generator. If you need to generate an impl for another type you can use `impl_trait_for_other_type`
pub fn impl_for(
&mut self,
trait_name: impl Into<String>,
) -> ImplFor<'_, Self> {
ImplFor::new(
self,
self.name.clone().into(),
Some(trait_name.into().into()),
)
}
/// Generate an `impl <type_name>` block. See [`ImplFor`] for more information.
/// ```
/// # use virtue::prelude::*;
/// # let mut generator = Generator::with_name("Baz");
/// generator.impl_for_other_type("Foo");
///
/// // will output:
/// // impl Foo { }
/// # generator.assert_eq("impl Foo { }");
/// ```
pub fn impl_for_other_type(
&mut self,
type_name: impl Into<StringOrIdent>,
) -> ImplFor<'_, Self> {
ImplFor::new(self, type_name.into(), None)
}
/// Generate an `impl <trait_name> for <type_name>` block. See [`ImplFor`] for more information.
/// ```
/// # use virtue::prelude::*;
/// # let mut generator = Generator::with_name("Baz");
/// generator.impl_trait_for_other_type("Foo", "Bar");
///
/// // will output:
/// // impl Foo for Bar { }
/// # generator.assert_eq("impl Foo for Bar { }");
/// ```
pub fn impl_trait_for_other_type(
&mut self,
trait_name: impl Into<StringOrIdent>,
type_name: impl Into<StringOrIdent>,
) -> ImplFor<'_, Self> {
ImplFor::new(self, type_name.into(), Some(trait_name.into()))
}
/// Generate an `for <..lifetimes> <trait_name> for <target_name>` implementation. See [`ImplFor`] for more information.
///
/// Note:
/// - Lifetimes should _not_ have the leading apostrophe.
/// - `trait_name` should _not_ have custom lifetimes. These will be added automatically.
///
/// ```
/// # use virtue::prelude::*;
/// # let mut generator = Generator::with_name("Bar");
/// generator.impl_for_with_lifetimes("Foo", ["a", "b"]);
///
/// // will output:
/// // impl<'a, 'b> Foo<'a, 'b> for StructOrEnum { }
/// # generator.assert_eq("impl < 'a , 'b > Foo < 'a , 'b > for Bar { }");
/// ```
///
/// The new lifetimes are not associated with any existing lifetimes. If you want this behavior you can call `.impl_for_with_lifetimes(...).new_lifetimes_depend_on_existing()`
///
/// ```
/// # use virtue::prelude::*;
/// # let mut generator = Generator::with_name("Bar").with_lifetime("a");
/// // given a derive on `struct<'a> Bar<'a>`
/// generator
/// .impl_for_with_lifetimes("Foo", ["b"])
/// .new_lifetimes_depend_on_existing();
///
/// // will output:
/// // impl<'a, 'b> Foo<'b> for Bar<'a> where 'b: 'a { }
/// # generator.assert_eq("impl < 'b , 'a > Foo < 'b > for Bar < 'a > where 'b : 'a { }");
/// ```
pub fn impl_for_with_lifetimes<ITER, T>(
&mut self,
trait_name: T,
lifetimes: ITER,
) -> ImplFor<'_, Self>
where
ITER: IntoIterator,
ITER::Item: Into<String>,
T: Into<StringOrIdent>,
{
ImplFor::new(self, self.name.clone().into(), Some(trait_name.into()))
.with_lifetimes(lifetimes)
}
/// Generate a struct with the given name. See [`GenStruct`] for more info.
pub fn generate_struct(
&mut self,
name: impl Into<String>,
) -> GenStruct<'_, Self> {
GenStruct::new(self, name)
}
/// Generate an enum with the given name. See [`GenEnum`] for more info.
pub fn generate_enum(
&mut self,
name: impl Into<String>,
) -> GenEnum<'_, Self> {
GenEnum::new(self, name)
}
/// Generate a `mod <name> { ... }`. See [`GenerateMod`] for more info.
pub fn generate_mod(
&mut self,
mod_name: impl Into<String>,
) -> GenerateMod<'_, Self> {
GenerateMod::new(self, mod_name)
}
/// Export the current stream to a file, making it very easy to debug the output of a derive macro.
/// This will try to find rust's `target` directory, and write `target/generated/<crate_name>/<name>_<file_postfix>.rs`.
///
/// Will return `true` if the file is written, `false` otherwise.
///
/// The outputted file is unformatted. Use `cargo fmt -- target/generated/<crate_name>/<file>.rs` to format the file.
#[must_use]
pub fn export_to_file(
&self,
crate_name: &str,
file_postfix: &str,
) -> bool {
use std::io::Write;
if let Ok(var) = std::env::var("CARGO_MANIFEST_DIR") {
let mut path = std::path::PathBuf::from(var);
loop {
{
let mut path = path.clone();
path.push("target");
if path.exists() {
path.push("generated");
path.push(crate_name);
if std::fs::create_dir_all(&path).is_err() {
return false;
}
path.push(format!("{}_{}.rs", self.target_name(), file_postfix));
let result = std::fs::File::create(path);
if let Ok(mut file) = result {
let _ = file.write_all(self.stream.stream.to_string().as_bytes());
return true;
}
}
}
if let Some(parent) = path.parent() {
path = parent.into();
} else {
break;
}
}
}
false
}
/// # Errors
///
/// Returns an error if the operation fails.
/// Consume the contents of this generator. This *must* be called, or else the generator will panic on drop.
pub fn finish(mut self) -> crate::prelude::Result<TokenStream> {
Ok(std::mem::take(&mut self.stream).stream)
}
}
#[cfg(feature = "proc-macro2")]
impl Generator {
/// Create a new generator with the name `name`. This is useful for testing purposes in combination with the `assert_eq` function.
pub fn with_name(name: &str) -> Self {
Self::new(
Ident::new(name, crate::prelude::Span::call_site()),
None,
None,
)
}
/// Add a lifetime to this generator.
pub fn with_lifetime(
mut self,
lt: &str,
) -> Self {
self.generics
.get_or_insert_with(|| Generics(Vec::new()))
.push(crate::parse::Generic::Lifetime(crate::parse::Lifetime {
ident: crate::prelude::Ident::new(lt, crate::prelude::Span::call_site()),
constraint: Vec::new(),
}));
self
}
/// Assert that the generated code in this generator matches the given string. This is useful for testing purposes in combination with the `with_name` function.
///
/// # Panics
///
/// Panics if an internal invariant is violated.
pub fn assert_eq(
&self,
expected: &str,
) {
assert_eq!(expected, self.stream.stream.to_string());
}
}
impl Drop for Generator {
fn drop(&mut self) {
if !self.stream.stream.is_empty() && !std::thread::panicking() {
eprintln!(
"WARNING: Generator dropped but the stream is not empty. Please call `.finish()` on the generator"
);
}
}
}
impl super::Parent for Generator {
fn append(
&mut self,
builder: StreamBuilder,
) {
self.stream.append(builder);
}
fn name(&self) -> &Ident {
&self.name
}
fn generics(&self) -> Option<&Generics> {
self.generics.as_ref()
}
fn generic_constraints(&self) -> Option<&GenericConstraints> {
self.generic_constraints.as_ref()
}
}
#[cfg(test)]
mod test {
use proc_macro2::Span;
use crate::token_stream;
use super::*;
#[test]
fn impl_for_with_lifetimes() {
// No generics
let mut generator =
Generator::new(Ident::new("StructOrEnum", Span::call_site()), None, None);
let _ = generator.impl_for_with_lifetimes("Foo", ["a", "b"]);
let output = generator.finish().unwrap();
assert_eq!(
output
.into_iter()
.map(|v| v.to_string())
.collect::<String>(),
token_stream("impl<'a, 'b> Foo<'a, 'b> for StructOrEnum { }")
.map(|v| v.to_string())
.collect::<String>(),
);
// with simple generics
let mut generator = Generator::new(
Ident::new("StructOrEnum", Span::call_site()),
Generics::try_take(&mut token_stream("<T1, T2>")).unwrap(),
None,
);
let _ = generator.impl_for_with_lifetimes("Foo", ["a", "b"]);
let output = generator.finish().unwrap();
assert_eq!(
output
.into_iter()
.map(|v| v.to_string())
.collect::<String>(),
token_stream("impl<'a, 'b, T1, T2> Foo<'a, 'b> for StructOrEnum<T1, T2> { }")
.map(|v| v.to_string())
.collect::<String>()
);
// with lifetimes
let mut generator = Generator::new(
Ident::new("StructOrEnum", Span::call_site()),
Generics::try_take(&mut token_stream("<'alpha, 'beta>")).unwrap(),
None,
);
let _ = generator.impl_for_with_lifetimes("Foo", ["a", "b"]);
let output = generator.finish().unwrap();
assert_eq!(
output
.into_iter()
.map(|v| v.to_string())
.collect::<String>(),
token_stream(
"impl<'a, 'b, 'alpha, 'beta> Foo<'a, 'b> for StructOrEnum<'alpha, 'beta> { }"
)
.map(|v| v.to_string())
.collect::<String>()
);
}
#[test]
fn impl_for_with_trait_generics() {
let mut generator = Generator::new(
Ident::new("StructOrEnum", Span::call_site()),
Generics::try_take(&mut token_stream("<'a>")).unwrap(),
None,
);
let _ = generator.impl_for("Foo").with_trait_generics(["&'a str"]);
let output = generator.finish().unwrap();
assert_eq!(
output
.into_iter()
.map(|v| v.to_string())
.collect::<String>(),
token_stream("impl<'a> Foo<&'a str> for StructOrEnum<'a> { }")
.map(|v| v.to_string())
.collect::<String>(),
);
}
#[test]
fn impl_for_with_impl_generics() {
// with simple generics
let mut generator = Generator::new(
Ident::new("StructOrEnum", Span::call_site()),
Generics::try_take(&mut token_stream("<T1, T2>")).unwrap(),
None,
);
let _ = generator.impl_for("Foo").with_impl_generics(["Bar"]);
let output = generator.finish().unwrap();
assert_eq!(
output
.into_iter()
.map(|v| v.to_string())
.collect::<String>(),
token_stream("impl<T1, T2, Bar> Foo for StructOrEnum<T1, T2> { }")
.map(|v| v.to_string())
.collect::<String>()
);
// with lifetimes
let mut generator = Generator::new(
Ident::new("StructOrEnum", Span::call_site()),
Generics::try_take(&mut token_stream("<'alpha, 'beta>")).unwrap(),
None,
);
let _ = generator.impl_for("Foo").with_impl_generics(["Bar"]);
let output = generator.finish().unwrap();
assert_eq!(
output
.into_iter()
.map(|v| v.to_string())
.collect::<String>(),
token_stream("impl<'alpha, 'beta, Bar> Foo for StructOrEnum<'alpha, 'beta> { }")
.map(|v| v.to_string())
.collect::<String>()
);
}
}