token-metadata 0.1.0

Data structures to associate arbitrary metadata with [`proc_macro2`] tokens.
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
#![doc = include_str!("../README.md")]

extern crate alloc;

use ::alloc::vec::Vec;
use ::core::{
	fmt::Debug,
	ops::{
		Deref,
		DerefMut,
	},
};
use ::proc_macro2::{
	Delimiter,
	Group,
	Ident,
	Literal,
	Punct,
	Span,
	TokenStream,
	TokenTree,
	extra::DelimSpan,
};

/// A version of [`Group`] with arbitrary attached metadata. The tokens within
/// this [`GroupWithMetadata`] also have metadata.
///
/// The fields of this struct are public, unlike [`Group`]'s. There are no
/// getter/setter methods: use the fields directly.
///
///  See [`proc_macro2`]'s documentation for more information on [`Group`].
#[derive(Clone, Debug)]
pub struct GroupWithMetadata<G, I = G, P = G, L = I> {
	/// See [`Group::delimiter`].
	pub delimiter: Delimiter,
	/// A vector holding the tokens within this group, each with their own
	/// metadata.
	pub contents: TokenWithMetadataVec<G, I, P, L>,
	/// See [`Group::span`].
	/// This span covers the entire group, including the delimiters.
	pub span: Span,
	/// The associated metadata.
	pub metadata: G,
}

impl<G, I, P, L> GroupWithMetadata<G, I, P, L> {
	/// Strips the metadata from this [`GroupWithMetadata`], returning an
	/// ordinary [`Group`].
	///
	/// The tokens within the group will also be stripped of their metadata.
	#[must_use = "If you meant to drop this value, use `std::mem::drop` instead."]
	pub fn strip_metadata(self) -> Group {
		let mut group = Group::new(
			self.delimiter,
			token_with_metadata_iter_ext::iter_tokens_strip_metadata(self.contents).collect(),
		);
		group.set_span(self.span);
		group
	}

	/// See [`Group::delimiter`].
	#[inline(always)]
	#[deprecated(note = "Use the `delimiter` field directly.")]
	pub fn delimiter(&self) -> Delimiter {
		self.delimiter
	}

	/// See [`Group::span`].
	#[inline(always)]
	#[must_use]
	#[deprecated(note = "Use the `span` field directly.")]
	pub fn span(&self) -> Span {
		self.span
	}

	/// See [`Group::span_open`].
	#[must_use]
	pub fn span_open(&self) -> Span {
		// Unfortunately, proc-macro2 does not expose span_open/spam_close methods.
		// We need to create a dummy Group to access them.
		let mut dummy = Group::new(self.delimiter, TokenStream::new());
		dummy.set_span(self.span);
		dummy.span_open()
	}

	/// See [`Group::span_close`].
	#[must_use]
	pub fn span_close(&self) -> Span {
		let mut dummy = Group::new(self.delimiter, TokenStream::new());
		dummy.set_span(self.span);
		dummy.span_close()
	}

	/// See [`Group::delim_span`].
	#[must_use]
	pub fn delim_span(&self) -> DelimSpan {
		let mut dummy = Group::new(self.delimiter, TokenStream::new());
		dummy.set_span(self.span);
		dummy.delim_span()
	}

	/// See [`Group::set_span`].
	#[inline(always)]
	#[deprecated(note = "Use the `span` field directly.")]
	pub fn set_span(&mut self, span: Span) {
		self.span = span;
	}
}

impl<G, I, P, L> From<Group> for GroupWithMetadata<G, I, P, L>
where
	G: Default,
	I: Default,
	P: Default,
	L: Default,
{
	fn from(value: Group) -> Self {
		Self {
			delimiter: value.delimiter(),
			contents: token_with_metadata_iter_ext::iter_tokens_add_default_metadata(value.stream()).collect(),
			span: value.span(),
			metadata: G::default(),
		}
	}
}

impl<T> From<(Group, &T)> for GroupWithMetadata<T>
where
	T: Clone,
{
	fn from((value, metadata): (Group, &T)) -> Self {
		Self {
			delimiter: value.delimiter(),
			contents: token_with_metadata_iter_ext::iter_tokens_add_metadata(value.stream(), metadata).collect(),
			span: value.span(),
			metadata: metadata.clone(),
		}
	}
}

impl<G, I, P, L> From<(Group, &G, &I, &P, &L)> for GroupWithMetadata<G, I, P, L>
where
	G: Clone,
	I: Clone,
	P: Clone,
	L: Clone,
{
	fn from((value, g, i, p, l): (Group, &G, &I, &P, &L)) -> Self {
		Self {
			delimiter: value.delimiter(),
			contents: token_with_metadata_iter_ext::iter_tokens_add_specific_metadata(value.stream(), g, i, p, l).collect(),
			span: value.span(),
			metadata: g.clone(),
		}
	}
}

/// A simple struct holding a token and its associated metadata.
///
/// Though [`TokenWithMetadata::token`] is generic, it should be a token from
/// [`proc_macro2`] other than [`Group`] ([`Ident`], [`Punct`], or [`Literal`]).
///
/// To get the token without metadata, simply read the
/// [`TokenWithMetadata::token`] field.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct TokenWithMetadata<T, U> {
	/// The token.
	pub token: T,
	/// The associated metadata.
	pub metadata: U,
}

/// Allow accessing the token's methods directly.
impl<T, U> Deref for TokenWithMetadata<T, U> {
	type Target = T;

	#[inline(always)]
	fn deref(&self) -> &Self::Target {
		&self.token
	}
}

/// Allow accessing the token's methods directly.
impl<T, U> DerefMut for TokenWithMetadata<T, U> {
	#[inline(always)]
	fn deref_mut(&mut self) -> &mut Self::Target {
		&mut self.token
	}
}

impl<T, U: Default> From<T> for TokenWithMetadata<T, U> {
	#[inline(always)]
	fn from(token: T) -> Self {
		Self { token, metadata: U::default() }
	}
}

impl<T, U> From<(T, U)> for TokenWithMetadata<T, U> {
	#[inline(always)]
	fn from((token, metadata): (T, U)) -> Self {
		Self { token, metadata }
	}
}

/// A version of [`TokenTree`] with arbitrary attached metadata.
///
/// Each variant holds metadata of a different type. The types are:
/// - `G`: For [`GroupWithMetadata`]s (in [`proc_macro2`], [`Group`]s).
/// - `I`: For [`Ident`] tokens (defaults to the same type as `G`).
/// - `P`: For [`Punct`] tokens (defaults to the same type as `G`).
/// - `L`: For [`Literal`] tokens (defaults to the same type as `I`).
///
/// See [`proc_macro2`]'s documentation for more information.
#[derive(Clone, Debug)]
pub enum TokenTreeWithMetadata<G, I = G, P = G, L = I> {
	/// A [`GroupWithMetadata`] token with its corresponding metadata.
	/// The tokens within the group, correspondingly, also have metadata, with
	/// the same types.
	Group(GroupWithMetadata<G, I, P, L>),
	/// An [`Ident`] token with its corresponding metadata.
	Ident(TokenWithMetadata<Ident, I>),
	/// A [`Punct`] token with its corresponding metadata.
	Punct(TokenWithMetadata<Punct, P>),
	/// A [`Literal`] token with its corresponding metadata.
	Literal(TokenWithMetadata<Literal, L>),
}

impl<G, I, P, L> TokenTreeWithMetadata<G, I, P, L> {
	/// Strips the metadata from this [`TokenTreeWithMetadata`], returning an
	/// ordinary [`TokenTree`]. Child [`GroupWithMetadata`]s will also be
	/// stripped of their metadata.
	#[inline]
	#[must_use = "If you meant to drop this value, use `std::mem::drop` instead."]
	pub fn strip_metadata(self) -> TokenTree {
		match self {
			| TokenTreeWithMetadata::Group(group) => TokenTree::Group(group.strip_metadata()),
			| TokenTreeWithMetadata::Ident(twm) => TokenTree::Ident(twm.token),
			| TokenTreeWithMetadata::Punct(twm) => TokenTree::Punct(twm.token),
			| TokenTreeWithMetadata::Literal(twm) => TokenTree::Literal(twm.token),
		}
	}

	/// Gets the span of this token.
	///
	/// See [`TokenTree::span`].
	#[inline]
	#[must_use]
	pub fn span(&self) -> Span {
		match self {
			| TokenTreeWithMetadata::Group(group) => group.span,
			| TokenTreeWithMetadata::Ident(twm) => twm.token.span(),
			| TokenTreeWithMetadata::Punct(twm) => twm.token.span(),
			| TokenTreeWithMetadata::Literal(twm) => twm.token.span(),
		}
	}

	/// Sets the span of this token.
	///
	/// See [`TokenTree::set_span`].
	#[inline]
	pub fn set_span(&mut self, span: Span) {
		match self {
			| TokenTreeWithMetadata::Group(group) => group.span = span,
			| TokenTreeWithMetadata::Ident(twm) => twm.token.set_span(span),
			| TokenTreeWithMetadata::Punct(twm) => twm.token.set_span(span),
			| TokenTreeWithMetadata::Literal(twm) => twm.token.set_span(span),
		}
	}
}

impl<T> TokenTreeWithMetadata<T> {
	/// Consumes self and returns the metadata associated with this token.
	/// Only available when all metadata types are the same.
	#[inline]
	#[must_use = "If you meant to drop this value, use `std::mem::drop` instead."]
	pub fn get_metadata(self) -> T {
		match self {
			| Self::Group(group) => group.metadata,
			| Self::Ident(twm) => twm.metadata,
			| Self::Punct(twm) => twm.metadata,
			| Self::Literal(twm) => twm.metadata,
		}
	}

	/// Returns a reference to the metadata associated with this token.
	/// Only available when all metadata types are the same.
	#[inline]
	#[must_use]
	pub fn get_metadata_ref(&self) -> &T {
		match self {
			| Self::Group(group) => &group.metadata,
			| Self::Ident(twm) => &twm.metadata,
			| Self::Punct(twm) => &twm.metadata,
			| Self::Literal(twm) => &twm.metadata,
		}
	}

	/// Returns a mutable reference to the metadata associated with this token.
	/// Only available when all metadata types are the same.
	#[inline]
	#[must_use]
	pub fn get_metadata_mut(&mut self) -> &mut T {
		match self {
			| Self::Group(group) => &mut group.metadata,
			| Self::Ident(twm) => &mut twm.metadata,
			| Self::Punct(twm) => &mut twm.metadata,
			| Self::Literal(twm) => &mut twm.metadata,
		}
	}

	/// Sets the metadata associated with this token.
	/// Only available when all metadata types are the same.
	#[inline]
	pub fn set_metadata(&mut self, metadata: T) {
		match self {
			| Self::Group(group) => group.metadata = metadata,
			| Self::Ident(twm) => twm.metadata = metadata,
			| Self::Punct(twm) => twm.metadata = metadata,
			| Self::Literal(twm) => twm.metadata = metadata,
		}
	}
}

impl<G, I, P, L> From<TokenTree> for TokenTreeWithMetadata<G, I, P, L>
where
	G: Default,
	I: Default,
	P: Default,
	L: Default,
{
	#[inline]
	fn from(tree: TokenTree) -> Self {
		match tree {
			| TokenTree::Group(group) => Self::Group(group.into()),
			| TokenTree::Ident(ident) => Self::Ident(ident.into()),
			| TokenTree::Punct(punct) => Self::Punct(punct.into()),
			| TokenTree::Literal(literal) => Self::Literal(literal.into()),
		}
	}
}

impl<T> From<(TokenTree, &T)> for TokenTreeWithMetadata<T>
where
	T: Clone,
{
	#[inline]
	fn from((tree, metadata): (TokenTree, &T)) -> Self {
		match tree {
			| TokenTree::Group(group) => Self::Group((group, metadata).into()),
			| TokenTree::Ident(ident) => Self::Ident((ident, metadata.clone()).into()),
			| TokenTree::Punct(punct) => Self::Punct((punct, metadata.clone()).into()),
			| TokenTree::Literal(literal) => Self::Literal((literal, metadata.clone()).into()),
		}
	}
}

impl<G, I, P, L> From<(TokenTree, &G, &I, &P, &L)> for TokenTreeWithMetadata<G, I, P, L>
where
	G: Clone,
	I: Clone,
	P: Clone,
	L: Clone,
{
	#[inline]
	fn from((tree, g, i, p, l): (TokenTree, &G, &I, &P, &L)) -> Self {
		match tree {
			| TokenTree::Group(group) => Self::Group((group, g, i, p, l).into()),
			| TokenTree::Ident(ident) => Self::Ident((ident, i.clone()).into()),
			| TokenTree::Punct(punct) => Self::Punct((punct, p.clone()).into()),
			| TokenTree::Literal(literal) => Self::Literal((literal, l.clone()).into()),
		}
	}
}

impl<G, I, P, L> From<Group> for TokenTreeWithMetadata<G, I, P, L>
where
	G: Default,
	I: Default,
	P: Default,
	L: Default,
{
	#[inline(always)]
	fn from(group: Group) -> Self {
		Self::Group(group.into())
	}
}

impl<T> From<(Group, &T)> for TokenTreeWithMetadata<T>
where
	T: Clone,
{
	#[inline(always)]
	fn from(group_and_val: (Group, &T)) -> Self {
		Self::Group(group_and_val.into())
	}
}

impl<G, I, P, L> From<(Group, &G, &I, &P, &L)> for TokenTreeWithMetadata<G, I, P, L>
where
	G: Clone,
	I: Clone,
	P: Clone,
	L: Clone,
{
	#[inline(always)]
	fn from(group_and_vals: (Group, &G, &I, &P, &L)) -> Self {
		Self::Group(group_and_vals.into())
	}
}

impl<G, I, P, L> From<Ident> for TokenTreeWithMetadata<G, I, P, L>
where
	I: Default,
{
	#[inline(always)]
	fn from(ident: Ident) -> Self {
		Self::Ident(ident.into())
	}
}

impl<G, I, P, L> From<(Ident, I)> for TokenTreeWithMetadata<G, I, P, L> {
	#[inline(always)]
	fn from((ident, metadata): (Ident, I)) -> Self {
		Self::Ident((ident, metadata).into())
	}
}

impl<G, I, P, L> From<Punct> for TokenTreeWithMetadata<G, I, P, L>
where
	P: Default,
{
	#[inline(always)]
	fn from(punct: Punct) -> Self {
		Self::Punct(punct.into())
	}
}

impl<G, I, P, L> From<(Punct, P)> for TokenTreeWithMetadata<G, I, P, L> {
	#[inline(always)]
	fn from((punct, metadata): (Punct, P)) -> Self {
		Self::Punct((punct, metadata).into())
	}
}

impl<G, I, P, L> From<Literal> for TokenTreeWithMetadata<G, I, P, L>
where
	L: Default,
{
	#[inline(always)]
	fn from(literal: Literal) -> Self {
		Self::Literal(literal.into())
	}
}

impl<G, I, P, L> From<(Literal, L)> for TokenTreeWithMetadata<G, I, P, L> {
	#[inline(always)]
	fn from((literal, metadata): (Literal, L)) -> Self {
		Self::Literal((literal, metadata).into())
	}
}

/// A type alias for a [`Vec`] of [`TokenTreeWithMetadata`]s.
///
/// Since [`TokenTreeWithMetadata`]s are not obtained from a [`TokenStream`]
/// directly, and are built manually or derived from obtained [`TokenTree`]s, it
/// doesn't make much sense to use a restrictive "token with metadata
/// stream" type. Instead, use vectors of tokens with metadata.
pub type TokenWithMetadataVec<G, I = G, P = G, L = I> = Vec<TokenTreeWithMetadata<G, I, P, L>>;

/// Extension module for [`TokenWithMetadataVec`], or more generally, for
/// iterators of [`TokenTreeWithMetadata`]s.
pub mod token_with_metadata_iter_ext {
	use super::*;

	/// Strips the metadata from an iterator of [`TokenTreeWithMetadata`]s,
	/// returning an iterator of ordinary [`TokenTree`]s.
	pub fn iter_tokens_strip_metadata<G, I, P, L>(iter: impl IntoIterator<Item = TokenTreeWithMetadata<G, I, P, L>>) -> impl Iterator<Item = TokenTree> {
		iter.into_iter().map(|ttwm| ttwm.strip_metadata())
	}

	/// Converts an iterator of ordinary [`TokenTree`]s into an iterator of
	/// [`TokenTreeWithMetadata`]s with default-initialized metadata.
	pub fn iter_tokens_add_default_metadata<G, I, P, L>(iter: impl IntoIterator<Item = TokenTree>) -> impl Iterator<Item = TokenTreeWithMetadata<G, I, P, L>>
	where
		G: Default,
		I: Default,
		P: Default,
		L: Default,
	{
		iter.into_iter().map(|tt| tt.into())
	}

	/// Converts an iterator of ordinary [`TokenTree`]s into an iterator of
	/// [`TokenTreeWithMetadata`]s with the given metadata.
	pub fn iter_tokens_add_metadata<T>(iter: impl IntoIterator<Item = TokenTree>, metadata: &T) -> impl Iterator<Item = TokenTreeWithMetadata<T>>
	where
		T: Clone,
	{
		iter.into_iter().map(move |tt| (tt, metadata).into())
	}

	/// Converts an iterator of ordinary [`TokenTree`]s into an iterator of
	/// [`TokenTreeWithMetadata`]s with the given specific metadata for each
	/// token type.
	pub fn iter_tokens_add_specific_metadata<G, I, P, L>(
		iter: impl IntoIterator<Item = TokenTree>,
		group: &G,
		ident: &I,
		punct: &P,
		literal: &L,
	) -> impl Iterator<Item = TokenTreeWithMetadata<G, I, P, L>>
	where
		G: Clone,
		I: Clone,
		P: Clone,
		L: Clone,
	{
		iter.into_iter().map(move |tt| (tt, group, ident, punct, literal).into())
	}
}