parity-scale-codec-derive 3.7.3

Serialization and deserialization derive macro for Parity SCALE Codec
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright 2018-2020 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Various internal utils.
//!
//! NOTE: attributes finder must be checked using check_attribute first,
//! otherwise the macro can panic.

use std::str::FromStr;

use proc_macro2::TokenStream;
use quote::quote;
use syn::{
	parse::Parse, punctuated::Punctuated, spanned::Spanned, token, Attribute, Data, DataEnum,
	DeriveInput, Expr, ExprLit, Field, Fields, FieldsNamed, FieldsUnnamed, Lit, Meta,
	MetaNameValue, Path, Variant,
};

fn find_meta_item<'a, F, R, I, M>(mut itr: I, mut pred: F) -> Option<R>
where
	F: FnMut(M) -> Option<R> + Clone,
	I: Iterator<Item = &'a Attribute>,
	M: Parse,
{
	itr.find_map(|attr| {
		attr.path().is_ident("codec").then(|| pred(attr.parse_args().ok()?)).flatten()
	})
}

pub fn const_eval_check_variant_indexes(
	recurse_variant_indices: impl Iterator<Item = (syn::Ident, TokenStream)>,
	crate_path: &syn::Path,
) -> TokenStream {
	let mut recurse_indices = vec![];
	for (ident, index) in recurse_variant_indices {
		let ident_str = ident.to_string();
		recurse_indices.push(quote! { (#index, #ident_str) });
	}
	let len = recurse_indices.len();

	if len == 0 {
		return quote! {};
	}

	quote! {
		const _: () = {
			const indices: [(usize, &'static str); #len] = [#( #recurse_indices ,)*];

			// Returns if there is duplicate, and if there is some the duplicate indexes.
			const fn duplicate_info(array: &[(usize, &'static str); #len]) -> (bool, usize, usize) {
				let len = array.len();
				let mut i = 0;
				while i < len {
						let mut j = i + 1;
						while j < len {
								if array[i].0 == array[j].0 {
									return (true, i, j);
								}
								j += 1;
						}
						i += 1;
				}
				(false, 0, 0)
			}

			const DUP_INFO: (bool, usize, usize) = duplicate_info(&indices);

			if DUP_INFO.0 {
				let msg = #crate_path::__private::concatcp!(
					"Found variants that have duplicate indexes. Both `",
					indices[DUP_INFO.1].1,
					"` and `",
					indices[DUP_INFO.2].1,
					"` have the index `",
					indices[DUP_INFO.1].0,
					"`. Use different indexes for each variant."
				);

				::core::panic!("{}", msg);
			}
		};
	}
}

/// Look for a `#[scale(index = $int)]` attribute on a variant. If no attribute
/// is found, fall back to the discriminant or just the variant index.
pub fn variant_index(v: &Variant, i: usize) -> TokenStream {
	// first look for an attribute
	let mut index = find_meta_item(v.attrs.iter(), |meta| {
		if let Meta::NameValue(ref nv) = meta {
			if nv.path.is_ident("index") {
				if let Expr::Lit(ExprLit { lit: Lit::Int(ref v), .. }) = nv.value {
					let byte = v
						.base10_parse::<usize>()
						.expect("Internal error, index attribute must have been checked");
					return Some(byte);
				}
			}
		}

		None
	});

	// if no attribute, we fall back to an explicit discriminant (ie 'enum A { Foo = 1u32 }').
	if index.is_none() {
		if let Some((_, Expr::Lit(ExprLit { lit: Lit::Int(disc_lit), .. }))) = &v.discriminant {
			index = disc_lit.base10_parse::<usize>().ok()
		}
	}

	// fall back to the variant index if no attribute or explicit discriminant is found.
	let index = index.unwrap_or(i);

	// output the index with no suffix (ie 1 rather than 1usize) so that these can be quoted into
	// an array of usizes and will work regardless of what type the discriminant values actually
	// are.
	let s = proc_macro2::Literal::usize_unsuffixed(index);
	quote! { #s }
}

/// Look for a `#[codec(encoded_as = "SomeType")]` outer attribute on the given
/// `Field`.
pub fn get_encoded_as_type(field: &Field) -> Option<TokenStream> {
	find_meta_item(field.attrs.iter(), |meta| {
		if let Meta::NameValue(ref nv) = meta {
			if nv.path.is_ident("encoded_as") {
				if let Expr::Lit(ExprLit { lit: Lit::Str(ref s), .. }) = nv.value {
					return Some(
						TokenStream::from_str(&s.value())
							.expect("Internal error, encoded_as attribute must have been checked"),
					);
				}
			}
		}

		None
	})
}

/// Look for a `#[codec(compact)]` outer attribute on the given `Field`. If the attribute is found,
/// return the compact type associated with the field type.
pub fn get_compact_type(field: &Field, crate_path: &syn::Path) -> Option<TokenStream> {
	find_meta_item(field.attrs.iter(), |meta| {
		if let Meta::Path(ref path) = meta {
			if path.is_ident("compact") {
				let field_type = &field.ty;
				return Some(quote! {<#field_type as #crate_path::HasCompact>::Type});
			}
		}

		None
	})
}

/// Look for a `#[codec(compact)]` outer attribute on the given `Field`.
pub fn is_compact(field: &Field) -> bool {
	get_compact_type(field, &parse_quote!(::crate)).is_some()
}

/// Look for a `#[codec(skip)]` in the given attributes.
pub fn should_skip(attrs: &[Attribute]) -> bool {
	find_meta_item(attrs.iter(), |meta| {
		if let Meta::Path(ref path) = meta {
			if path.is_ident("skip") {
				return Some(path.span());
			}
		}

		None
	})
	.is_some()
}

/// Look for a `#[codec(dumb_trait_bound)]`in the given attributes.
pub fn has_dumb_trait_bound(attrs: &[Attribute]) -> bool {
	find_meta_item(attrs.iter(), |meta| {
		if let Meta::Path(ref path) = meta {
			if path.is_ident("dumb_trait_bound") {
				return Some(());
			}
		}

		None
	})
	.is_some()
}

/// Generate the crate access for the crate using 2018 syntax.
fn crate_access() -> syn::Result<proc_macro2::Ident> {
	use proc_macro2::{Ident, Span};
	use proc_macro_crate::{crate_name, FoundCrate};
	const DEF_CRATE: &str = "parity-scale-codec";
	match crate_name(DEF_CRATE) {
		Ok(FoundCrate::Itself) => {
			let name = DEF_CRATE.to_string().replace('-', "_");
			Ok(syn::Ident::new(&name, Span::call_site()))
		},
		Ok(FoundCrate::Name(name)) => Ok(Ident::new(&name, Span::call_site())),
		Err(e) => Err(syn::Error::new(Span::call_site(), e)),
	}
}

/// This struct matches `crate = ...` where the ellipsis is a `Path`.
struct CratePath {
	_crate_token: Token![crate],
	_eq_token: Token![=],
	path: Path,
}

impl Parse for CratePath {
	fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
		Ok(CratePath {
			_crate_token: input.parse()?,
			_eq_token: input.parse()?,
			path: input.parse()?,
		})
	}
}

impl From<CratePath> for Path {
	fn from(CratePath { path, .. }: CratePath) -> Self {
		path
	}
}

/// Match `#[codec(crate = ...)]` and return the `...` if it is a `Path`.
fn codec_crate_path_inner(attr: &Attribute) -> Option<Path> {
	// match `#[codec ...]`
	attr.path()
		.is_ident("codec")
		.then(|| {
			// match `#[codec(crate = ...)]` and return the `...`
			attr.parse_args::<CratePath>().map(Into::into).ok()
		})
		.flatten()
}

/// Match `#[codec(crate = ...)]` and return the ellipsis as a `Path`.
///
/// If not found, returns the default crate access pattern.
///
/// If multiple items match the pattern, all but the first are ignored.
pub fn codec_crate_path(attrs: &[Attribute]) -> syn::Result<Path> {
	match attrs.iter().find_map(codec_crate_path_inner) {
		Some(path) => Ok(path),
		None => crate_access().map(|ident| parse_quote!(::#ident)),
	}
}

/// Parse `name(T: Bound, N: Bound)` or `name(skip_type_params(T, N))` as a custom trait bound.
pub enum CustomTraitBound<N> {
	SpecifiedBounds {
		_name: N,
		_paren_token: token::Paren,
		bounds: Punctuated<syn::WherePredicate, Token![,]>,
	},
	SkipTypeParams {
		_name: N,
		_paren_token_1: token::Paren,
		_skip_type_params: skip_type_params,
		_paren_token_2: token::Paren,
		type_names: Punctuated<syn::Ident, Token![,]>,
	},
}

impl<N: Parse> Parse for CustomTraitBound<N> {
	fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
		let mut content;
		let _name: N = input.parse()?;
		let _paren_token = syn::parenthesized!(content in input);
		if content.peek(skip_type_params) {
			Ok(Self::SkipTypeParams {
				_name,
				_paren_token_1: _paren_token,
				_skip_type_params: content.parse::<skip_type_params>()?,
				_paren_token_2: syn::parenthesized!(content in content),
				type_names: content.parse_terminated(syn::Ident::parse, Token![,])?,
			})
		} else {
			Ok(Self::SpecifiedBounds {
				_name,
				_paren_token,
				bounds: content.parse_terminated(syn::WherePredicate::parse, Token![,])?,
			})
		}
	}
}

syn::custom_keyword!(encode_bound);
syn::custom_keyword!(decode_bound);
syn::custom_keyword!(decode_with_mem_tracking_bound);
syn::custom_keyword!(mel_bound);
syn::custom_keyword!(skip_type_params);

/// Look for a `#[codec(decode_bound(T: Decode))]` in the given attributes.
///
/// If found, it should be used as trait bounds when deriving the `Decode` trait.
pub fn custom_decode_trait_bound(attrs: &[Attribute]) -> Option<CustomTraitBound<decode_bound>> {
	find_meta_item(attrs.iter(), Some)
}

/// Look for a `#[codec(decode_with_mem_tracking_bound(T: Decode))]` in the given attributes.
///
/// If found, it should be used as trait bounds when deriving the `Decode` trait.
pub fn custom_decode_with_mem_tracking_trait_bound(
	attrs: &[Attribute],
) -> Option<CustomTraitBound<decode_with_mem_tracking_bound>> {
	find_meta_item(attrs.iter(), Some)
}

/// Look for a `#[codec(encode_bound(T: Encode))]` in the given attributes.
///
/// If found, it should be used as trait bounds when deriving the `Encode` trait.
pub fn custom_encode_trait_bound(attrs: &[Attribute]) -> Option<CustomTraitBound<encode_bound>> {
	find_meta_item(attrs.iter(), Some)
}

/// Look for a `#[codec(mel_bound(T: MaxEncodedLen))]` in the given attributes.
///
/// If found, it should be used as the trait bounds when deriving the `MaxEncodedLen` trait.
#[cfg(feature = "max-encoded-len")]
pub fn custom_mel_trait_bound(attrs: &[Attribute]) -> Option<CustomTraitBound<mel_bound>> {
	find_meta_item(attrs.iter(), Some)
}

/// Given a set of named fields, return an iterator of `Field` where all fields
/// marked `#[codec(skip)]` are filtered out.
pub fn filter_skip_named(fields: &syn::FieldsNamed) -> impl Iterator<Item = &Field> {
	fields.named.iter().filter(|f| !should_skip(&f.attrs))
}

/// Given a set of unnamed fields, return an iterator of `(index, Field)` where all fields
/// marked `#[codec(skip)]` are filtered out.
pub fn filter_skip_unnamed(fields: &syn::FieldsUnnamed) -> impl Iterator<Item = (usize, &Field)> {
	fields.unnamed.iter().enumerate().filter(|(_, f)| !should_skip(&f.attrs))
}

/// Ensure attributes are correctly applied. This *must* be called before using
/// any of the attribute finder methods or the macro may panic if it encounters
/// misapplied attributes.
///
/// The top level can have the following attributes:
///
/// * `#[codec(dumb_trait_bound)]`
/// * `#[codec(encode_bound(T: Encode))]`
/// * `#[codec(decode_bound(T: Decode))]`
/// * `#[codec(mel_bound(T: MaxEncodedLen))]`
/// * `#[codec(crate = path::to::crate)]
///
/// Fields can have the following attributes:
///
/// * `#[codec(skip)]`
/// * `#[codec(compact)]`
/// * `#[codec(encoded_as = "$EncodeAs")]` with $EncodedAs a valid TokenStream
///
/// Variants can have the following attributes:
///
/// * `#[codec(skip)]`
/// * `#[codec(index = $int)]`
pub fn check_attributes(input: &DeriveInput) -> syn::Result<()> {
	for attr in &input.attrs {
		check_top_attribute(attr)?;
	}

	match input.data {
		Data::Struct(ref data) => match &data.fields {
			| Fields::Named(FieldsNamed { named: fields, .. }) |
			Fields::Unnamed(FieldsUnnamed { unnamed: fields, .. }) =>
				for field in fields {
					for attr in &field.attrs {
						check_field_attribute(attr)?;
					}
				},
			Fields::Unit => (),
		},
		Data::Enum(ref data) =>
			for variant in data.variants.iter() {
				for attr in &variant.attrs {
					check_variant_attribute(attr)?;
				}
				for field in &variant.fields {
					for attr in &field.attrs {
						check_field_attribute(attr)?;
					}
				}
				// While we're checking things, also ensure that
				// any explicit discriminants are within 0..=255
				let discriminant = variant.discriminant.as_ref().map(|(_, d)| d);
				if let Some(expr) = discriminant {
					check_variant_discriminant(expr)?;
				}
			},
		Data::Union(_) => (),
	}
	Ok(())
}

// Check if the attribute is `#[allow(..)]`, `#[deny(..)]`, `#[forbid(..)]` or `#[warn(..)]`.
pub fn is_lint_attribute(attr: &Attribute) -> bool {
	attr.path().is_ident("allow") ||
		attr.path().is_ident("deny") ||
		attr.path().is_ident("forbid") ||
		attr.path().is_ident("warn")
}

// Ensure a field is decorated only with the following attributes:
// * `#[codec(skip)]`
// * `#[codec(compact)]`
// * `#[codec(encoded_as = "$EncodeAs")]` with $EncodedAs a valid TokenStream
fn check_field_attribute(attr: &Attribute) -> syn::Result<()> {
	let field_error = "Invalid attribute on field, only `#[codec(skip)]`, `#[codec(compact)]` and \
		`#[codec(encoded_as = \"$EncodeAs\")]` are accepted.";

	if attr.path().is_ident("codec") {
		let nested = attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
		if nested.len() != 1 {
			return Err(syn::Error::new(attr.meta.span(), field_error));
		}
		match nested.first().expect("Just checked that there is one item; qed") {
			Meta::Path(path) if path.get_ident().map_or(false, |i| i == "skip") => Ok(()),

			Meta::Path(path) if path.get_ident().map_or(false, |i| i == "compact") => Ok(()),

			Meta::NameValue(MetaNameValue {
				path,
				value: Expr::Lit(ExprLit { lit: Lit::Str(lit_str), .. }),
				..
			}) if path.get_ident().map_or(false, |i| i == "encoded_as") =>
				TokenStream::from_str(&lit_str.value())
					.map(|_| ())
					.map_err(|_e| syn::Error::new(lit_str.span(), "Invalid token stream")),

			elt => Err(syn::Error::new(elt.span(), field_error)),
		}
	} else {
		Ok(())
	}
}

// Ensure a field is decorated only with the following attributes:
// * `#[codec(skip)]`
// * `#[codec(index = $int)]`
fn check_variant_attribute(attr: &Attribute) -> syn::Result<()> {
	let variant_error = "Invalid attribute on variant, only `#[codec(skip)]` and \
		`#[codec(index = $u8)]` are accepted.";

	if attr.path().is_ident("codec") {
		let nested = attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
		if nested.len() != 1 {
			return Err(syn::Error::new(attr.meta.span(), variant_error));
		}
		match nested.first().expect("Just checked that there is one item; qed") {
			Meta::Path(path) if path.get_ident().map_or(false, |i| i == "skip") => Ok(()),

			Meta::NameValue(MetaNameValue {
				path,
				value: Expr::Lit(ExprLit { lit: Lit::Int(lit_int), .. }),
				..
			}) if path.get_ident().map_or(false, |i| i == "index") => lit_int
				.base10_parse::<u8>()
				.map(|_| ())
				.map_err(|_| syn::Error::new(lit_int.span(), "Index must be in 0..255")),

			elt => Err(syn::Error::new(elt.span(), variant_error)),
		}
	} else {
		Ok(())
	}
}

// Ensure a variant discriminant, if provided, can be parsed into
// something in the range 0..255.
fn check_variant_discriminant(discriminant: &Expr) -> syn::Result<()> {
	if let Expr::Lit(ExprLit { lit: Lit::Int(lit_int), .. }) = discriminant {
		lit_int.base10_parse::<u8>().map(|_| ()).map_err(|_| {
			syn::Error::new(lit_int.span(), "Discriminant index must be in the range 0..255")
		})
	} else {
		Err(syn::Error::new(
			discriminant.span(),
			"Discriminant must be an integer literal in the range 0..255",
		))
	}
}

// Only `#[codec(dumb_trait_bound)]` is accepted as top attribute
fn check_top_attribute(attr: &Attribute) -> syn::Result<()> {
	let top_error = "Invalid attribute: only `#[codec(dumb_trait_bound)]`, \
		`#[codec(crate = path::to::crate)]`, `#[codec(encode_bound(T: Encode))]`, \
		`#[codec(decode_bound(T: Decode))]`, \
		`#[codec(decode_bound_with_mem_tracking_bound(T: DecodeWithMemTracking))]` or \
		`#[codec(mel_bound(T: MaxEncodedLen))]` are accepted as top attribute";
	if attr.path().is_ident("codec") &&
		attr.parse_args::<CustomTraitBound<encode_bound>>().is_err() &&
		attr.parse_args::<CustomTraitBound<decode_bound>>().is_err() &&
		attr.parse_args::<CustomTraitBound<decode_with_mem_tracking_bound>>().is_err() &&
		attr.parse_args::<CustomTraitBound<mel_bound>>().is_err() &&
		codec_crate_path_inner(attr).is_none()
	{
		let nested = attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
		if nested.len() != 1 {
			return Err(syn::Error::new(attr.meta.span(), top_error));
		}
		match nested.first().expect("Just checked that there is one item; qed") {
			Meta::Path(path) if path.get_ident().map_or(false, |i| i == "dumb_trait_bound") =>
				Ok(()),

			elt => Err(syn::Error::new(elt.span(), top_error)),
		}
	} else {
		Ok(())
	}
}

/// Checks whether the given attributes contain a `#[repr(transparent)]`.
pub fn is_transparent(attrs: &[syn::Attribute]) -> bool {
	attrs.iter().any(|attr| {
		if !attr.path().is_ident("repr") {
			return false;
		}
		let Ok(nested) = attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)
		else {
			return false;
		};
		nested.iter().any(|n| matches!(n, Meta::Path(p) if p.is_ident("transparent")))
	})
}

pub fn try_get_variants(data: &DataEnum) -> Result<Vec<&Variant>, syn::Error> {
	let data_variants: Vec<_> =
		data.variants.iter().filter(|variant| !should_skip(&variant.attrs)).collect();

	if data_variants.len() > 256 {
		return Err(syn::Error::new(
			data.variants.span(),
			"Currently only enums with at most 256 variants are encodable/decodable.",
		));
	}

	Ok(data_variants)
}