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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// 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.

//! Home of the parsing code for the Tasks API

use std::collections::HashSet;

#[cfg(test)]
use crate::assert_parse_error_matches;

#[cfg(test)]
use crate::pallet::parse::tests::simulate_manifest_dir;

use derive_syn_parse::Parse;
use frame_support_procedural_tools::generate_access_from_frame_or_crate;
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens};
use syn::{
	parse::ParseStream,
	parse2,
	spanned::Spanned,
	token::{Bracket, Paren, PathSep, Pound},
	Error, Expr, Ident, ImplItem, ImplItemFn, ItemEnum, ItemImpl, LitInt, Path, PathArguments,
	Result, TypePath,
};

pub mod keywords {
	use syn::custom_keyword;

	custom_keyword!(tasks_experimental);
	custom_keyword!(task_enum);
	custom_keyword!(task_list);
	custom_keyword!(task_condition);
	custom_keyword!(task_index);
	custom_keyword!(task_weight);
	custom_keyword!(pallet);
}

/// Represents the `#[pallet::tasks_experimental]` attribute and its attached item. Also includes
/// metadata about the linked [`TaskEnumDef`] if applicable.
#[derive(Clone, Debug)]
pub struct TasksDef {
	pub tasks_attr: Option<PalletTasksAttr>,
	pub tasks: Vec<TaskDef>,
	pub item_impl: ItemImpl,
	/// Path to `frame_support`
	pub scrate: Path,
	pub enum_ident: Ident,
	pub enum_arguments: PathArguments,
}

impl syn::parse::Parse for TasksDef {
	fn parse(input: ParseStream) -> Result<Self> {
		let item_impl: ItemImpl = input.parse()?;
		let (tasks_attrs, normal_attrs) = partition_tasks_attrs(&item_impl);
		let tasks_attr = match tasks_attrs.first() {
			Some(attr) => Some(parse2::<PalletTasksAttr>(attr.to_token_stream())?),
			None => None,
		};
		if let Some(extra_tasks_attr) = tasks_attrs.get(1) {
			return Err(Error::new(
				extra_tasks_attr.span(),
				"unexpected extra `#[pallet::tasks_experimental]` attribute",
			))
		}
		let tasks: Vec<TaskDef> = if tasks_attr.is_some() {
			item_impl
				.items
				.clone()
				.into_iter()
				.filter(|impl_item| matches!(impl_item, ImplItem::Fn(_)))
				.map(|item| parse2::<TaskDef>(item.to_token_stream()))
				.collect::<Result<_>>()?
		} else {
			Vec::new()
		};
		let mut task_indices = HashSet::<LitInt>::new();
		for task in tasks.iter() {
			let task_index = &task.index_attr.meta.index;
			if !task_indices.insert(task_index.clone()) {
				return Err(Error::new(
					task_index.span(),
					format!("duplicate task index `{}`", task_index),
				))
			}
		}
		let mut item_impl = item_impl;
		item_impl.attrs = normal_attrs;

		// we require the path on the impl to be a TypePath
		let enum_path = parse2::<TypePath>(item_impl.self_ty.to_token_stream())?;
		let segments = enum_path.path.segments.iter().collect::<Vec<_>>();
		let (Some(last_seg), None) = (segments.get(0), segments.get(1)) else {
			return Err(Error::new(
				enum_path.span(),
				"if specified manually, the task enum must be defined locally in this \
				pallet and cannot be a re-export",
			))
		};
		let enum_ident = last_seg.ident.clone();
		let enum_arguments = last_seg.arguments.clone();

		// We do this here because it would be improper to do something fallible like this at
		// the expansion phase. Fallible stuff should happen during parsing.
		let scrate = generate_access_from_frame_or_crate("frame-support")?;

		Ok(TasksDef { tasks_attr, item_impl, tasks, scrate, enum_ident, enum_arguments })
	}
}

/// Parsing for a `#[pallet::tasks_experimental]` attr.
pub type PalletTasksAttr = PalletTaskAttr<keywords::tasks_experimental>;

/// Parsing for any of the attributes that can be used within a `#[pallet::tasks_experimental]`
/// [`ItemImpl`].
pub type TaskAttr = PalletTaskAttr<TaskAttrMeta>;

/// Parsing for a `#[pallet::task_index]` attr.
pub type TaskIndexAttr = PalletTaskAttr<TaskIndexAttrMeta>;

/// Parsing for a `#[pallet::task_condition]` attr.
pub type TaskConditionAttr = PalletTaskAttr<TaskConditionAttrMeta>;

/// Parsing for a `#[pallet::task_list]` attr.
pub type TaskListAttr = PalletTaskAttr<TaskListAttrMeta>;

/// Parsing for a `#[pallet::task_weight]` attr.
pub type TaskWeightAttr = PalletTaskAttr<TaskWeightAttrMeta>;

/// Parsing for a `#[pallet:task_enum]` attr.
pub type PalletTaskEnumAttr = PalletTaskAttr<keywords::task_enum>;

/// Parsing for a manually-specified (or auto-generated) task enum, optionally including the
/// attached `#[pallet::task_enum]` attribute.
#[derive(Clone, Debug)]
pub struct TaskEnumDef {
	pub attr: Option<PalletTaskEnumAttr>,
	pub item_enum: ItemEnum,
	pub scrate: Path,
	pub type_use_generics: TokenStream2,
}

impl syn::parse::Parse for TaskEnumDef {
	fn parse(input: ParseStream) -> Result<Self> {
		let mut item_enum = input.parse::<ItemEnum>()?;
		let attr = extract_pallet_attr(&mut item_enum)?;
		let attr = match attr {
			Some(attr) => Some(parse2(attr)?),
			None => None,
		};

		// We do this here because it would be improper to do something fallible like this at
		// the expansion phase. Fallible stuff should happen during parsing.
		let scrate = generate_access_from_frame_or_crate("frame-support")?;

		let type_use_generics = quote!(T);

		Ok(TaskEnumDef { attr, item_enum, scrate, type_use_generics })
	}
}

/// Represents an individual tasks within a [`TasksDef`].
#[derive(Debug, Clone)]
pub struct TaskDef {
	pub index_attr: TaskIndexAttr,
	pub condition_attr: TaskConditionAttr,
	pub list_attr: TaskListAttr,
	pub weight_attr: TaskWeightAttr,
	pub item: ImplItemFn,
	pub arg_names: Vec<Ident>,
}

impl syn::parse::Parse for TaskDef {
	fn parse(input: ParseStream) -> Result<Self> {
		let item = input.parse::<ImplItemFn>()?;
		// we only want to activate TaskAttrType parsing errors for tasks-related attributes,
		// so we filter them here
		let task_attrs = partition_task_attrs(&item).0;

		let task_attrs: Vec<TaskAttr> = task_attrs
			.into_iter()
			.map(|attr| parse2(attr.to_token_stream()))
			.collect::<Result<_>>()?;

		let Some(index_attr) = task_attrs
			.iter()
			.find(|attr| matches!(attr.meta, TaskAttrMeta::TaskIndex(_)))
			.cloned()
		else {
			return Err(Error::new(
				item.sig.ident.span(),
				"missing `#[pallet::task_index(..)]` attribute",
			))
		};

		let Some(condition_attr) = task_attrs
			.iter()
			.find(|attr| matches!(attr.meta, TaskAttrMeta::TaskCondition(_)))
			.cloned()
		else {
			return Err(Error::new(
				item.sig.ident.span(),
				"missing `#[pallet::task_condition(..)]` attribute",
			))
		};

		let Some(list_attr) = task_attrs
			.iter()
			.find(|attr| matches!(attr.meta, TaskAttrMeta::TaskList(_)))
			.cloned()
		else {
			return Err(Error::new(
				item.sig.ident.span(),
				"missing `#[pallet::task_list(..)]` attribute",
			))
		};

		let Some(weight_attr) = task_attrs
			.iter()
			.find(|attr| matches!(attr.meta, TaskAttrMeta::TaskWeight(_)))
			.cloned()
		else {
			return Err(Error::new(
				item.sig.ident.span(),
				"missing `#[pallet::task_weight(..)]` attribute",
			))
		};

		if let Some(duplicate) = task_attrs
			.iter()
			.filter(|attr| matches!(attr.meta, TaskAttrMeta::TaskCondition(_)))
			.collect::<Vec<_>>()
			.get(1)
		{
			return Err(Error::new(
				duplicate.span(),
				"unexpected extra `#[pallet::task_condition(..)]` attribute",
			))
		}

		if let Some(duplicate) = task_attrs
			.iter()
			.filter(|attr| matches!(attr.meta, TaskAttrMeta::TaskList(_)))
			.collect::<Vec<_>>()
			.get(1)
		{
			return Err(Error::new(
				duplicate.span(),
				"unexpected extra `#[pallet::task_list(..)]` attribute",
			))
		}

		if let Some(duplicate) = task_attrs
			.iter()
			.filter(|attr| matches!(attr.meta, TaskAttrMeta::TaskIndex(_)))
			.collect::<Vec<_>>()
			.get(1)
		{
			return Err(Error::new(
				duplicate.span(),
				"unexpected extra `#[pallet::task_index(..)]` attribute",
			))
		}

		let mut arg_names = vec![];
		for input in item.sig.inputs.iter() {
			match input {
				syn::FnArg::Typed(pat_type) => match &*pat_type.pat {
					syn::Pat::Ident(ident) => arg_names.push(ident.ident.clone()),
					_ => return Err(Error::new(input.span(), "unexpected pattern type")),
				},
				_ => return Err(Error::new(input.span(), "unexpected function argument type")),
			}
		}

		let index_attr = index_attr.try_into().expect("we check the type above; QED");
		let condition_attr = condition_attr.try_into().expect("we check the type above; QED");
		let list_attr = list_attr.try_into().expect("we check the type above; QED");
		let weight_attr = weight_attr.try_into().expect("we check the type above; QED");

		Ok(TaskDef { index_attr, condition_attr, list_attr, weight_attr, item, arg_names })
	}
}

/// The contents of a [`TasksDef`]-related attribute.
#[derive(Parse, Debug, Clone)]
pub enum TaskAttrMeta {
	#[peek(keywords::task_list, name = "#[pallet::task_list(..)]")]
	TaskList(TaskListAttrMeta),
	#[peek(keywords::task_index, name = "#[pallet::task_index(..)")]
	TaskIndex(TaskIndexAttrMeta),
	#[peek(keywords::task_condition, name = "#[pallet::task_condition(..)")]
	TaskCondition(TaskConditionAttrMeta),
	#[peek(keywords::task_weight, name = "#[pallet::task_weight(..)")]
	TaskWeight(TaskWeightAttrMeta),
}

/// The contents of a `#[pallet::task_list]` attribute.
#[derive(Parse, Debug, Clone)]
pub struct TaskListAttrMeta {
	pub task_list: keywords::task_list,
	#[paren]
	_paren: Paren,
	#[inside(_paren)]
	pub expr: Expr,
}

/// The contents of a `#[pallet::task_index]` attribute.
#[derive(Parse, Debug, Clone)]
pub struct TaskIndexAttrMeta {
	pub task_index: keywords::task_index,
	#[paren]
	_paren: Paren,
	#[inside(_paren)]
	pub index: LitInt,
}

/// The contents of a `#[pallet::task_condition]` attribute.
#[derive(Parse, Debug, Clone)]
pub struct TaskConditionAttrMeta {
	pub task_condition: keywords::task_condition,
	#[paren]
	_paren: Paren,
	#[inside(_paren)]
	pub expr: Expr,
}

/// The contents of a `#[pallet::task_weight]` attribute.
#[derive(Parse, Debug, Clone)]
pub struct TaskWeightAttrMeta {
	pub task_weight: keywords::task_weight,
	#[paren]
	_paren: Paren,
	#[inside(_paren)]
	pub expr: Expr,
}

/// The contents of a `#[pallet::task]` attribute.
#[derive(Parse, Debug, Clone)]
pub struct PalletTaskAttr<T: syn::parse::Parse + core::fmt::Debug + ToTokens> {
	pub pound: Pound,
	#[bracket]
	_bracket: Bracket,
	#[inside(_bracket)]
	pub pallet: keywords::pallet,
	#[inside(_bracket)]
	pub colons: PathSep,
	#[inside(_bracket)]
	pub meta: T,
}

impl ToTokens for TaskListAttrMeta {
	fn to_tokens(&self, tokens: &mut TokenStream2) {
		let task_list = self.task_list;
		let expr = &self.expr;
		tokens.extend(quote!(#task_list(#expr)));
	}
}

impl ToTokens for TaskConditionAttrMeta {
	fn to_tokens(&self, tokens: &mut TokenStream2) {
		let task_condition = self.task_condition;
		let expr = &self.expr;
		tokens.extend(quote!(#task_condition(#expr)));
	}
}

impl ToTokens for TaskWeightAttrMeta {
	fn to_tokens(&self, tokens: &mut TokenStream2) {
		let task_weight = self.task_weight;
		let expr = &self.expr;
		tokens.extend(quote!(#task_weight(#expr)));
	}
}

impl ToTokens for TaskIndexAttrMeta {
	fn to_tokens(&self, tokens: &mut TokenStream2) {
		let task_index = self.task_index;
		let index = &self.index;
		tokens.extend(quote!(#task_index(#index)))
	}
}

impl ToTokens for TaskAttrMeta {
	fn to_tokens(&self, tokens: &mut TokenStream2) {
		match self {
			TaskAttrMeta::TaskList(list) => tokens.extend(list.to_token_stream()),
			TaskAttrMeta::TaskIndex(index) => tokens.extend(index.to_token_stream()),
			TaskAttrMeta::TaskCondition(condition) => tokens.extend(condition.to_token_stream()),
			TaskAttrMeta::TaskWeight(weight) => tokens.extend(weight.to_token_stream()),
		}
	}
}

impl<T: syn::parse::Parse + core::fmt::Debug + ToTokens> ToTokens for PalletTaskAttr<T> {
	fn to_tokens(&self, tokens: &mut TokenStream2) {
		let pound = self.pound;
		let pallet = self.pallet;
		let colons = self.colons;
		let meta = &self.meta;
		tokens.extend(quote!(#pound[#pallet #colons #meta]));
	}
}

impl TryFrom<PalletTaskAttr<TaskAttrMeta>> for TaskIndexAttr {
	type Error = syn::Error;

	fn try_from(value: PalletTaskAttr<TaskAttrMeta>) -> Result<Self> {
		let pound = value.pound;
		let pallet = value.pallet;
		let colons = value.colons;
		match value.meta {
			TaskAttrMeta::TaskIndex(meta) => parse2(quote!(#pound[#pallet #colons #meta])),
			_ =>
				return Err(Error::new(
					value.span(),
					format!("`{:?}` cannot be converted to a `TaskIndexAttr`", value.meta),
				)),
		}
	}
}

impl TryFrom<PalletTaskAttr<TaskAttrMeta>> for TaskConditionAttr {
	type Error = syn::Error;

	fn try_from(value: PalletTaskAttr<TaskAttrMeta>) -> Result<Self> {
		let pound = value.pound;
		let pallet = value.pallet;
		let colons = value.colons;
		match value.meta {
			TaskAttrMeta::TaskCondition(meta) => parse2(quote!(#pound[#pallet #colons #meta])),
			_ =>
				return Err(Error::new(
					value.span(),
					format!("`{:?}` cannot be converted to a `TaskConditionAttr`", value.meta),
				)),
		}
	}
}

impl TryFrom<PalletTaskAttr<TaskAttrMeta>> for TaskWeightAttr {
	type Error = syn::Error;

	fn try_from(value: PalletTaskAttr<TaskAttrMeta>) -> Result<Self> {
		let pound = value.pound;
		let pallet = value.pallet;
		let colons = value.colons;
		match value.meta {
			TaskAttrMeta::TaskWeight(meta) => parse2(quote!(#pound[#pallet #colons #meta])),
			_ =>
				return Err(Error::new(
					value.span(),
					format!("`{:?}` cannot be converted to a `TaskWeightAttr`", value.meta),
				)),
		}
	}
}

impl TryFrom<PalletTaskAttr<TaskAttrMeta>> for TaskListAttr {
	type Error = syn::Error;

	fn try_from(value: PalletTaskAttr<TaskAttrMeta>) -> Result<Self> {
		let pound = value.pound;
		let pallet = value.pallet;
		let colons = value.colons;
		match value.meta {
			TaskAttrMeta::TaskList(meta) => parse2(quote!(#pound[#pallet #colons #meta])),
			_ =>
				return Err(Error::new(
					value.span(),
					format!("`{:?}` cannot be converted to a `TaskListAttr`", value.meta),
				)),
		}
	}
}

fn extract_pallet_attr(item_enum: &mut ItemEnum) -> Result<Option<TokenStream2>> {
	let mut duplicate = None;
	let mut attr = None;
	item_enum.attrs = item_enum
		.attrs
		.iter()
		.filter(|found_attr| {
			let segs = found_attr
				.path()
				.segments
				.iter()
				.map(|seg| seg.ident.clone())
				.collect::<Vec<_>>();
			let (Some(seg1), Some(_), None) = (segs.get(0), segs.get(1), segs.get(2)) else {
				return true
			};
			if seg1 != "pallet" {
				return true
			}
			if attr.is_some() {
				duplicate = Some(found_attr.span());
			}
			attr = Some(found_attr.to_token_stream());
			false
		})
		.cloned()
		.collect();
	if let Some(span) = duplicate {
		return Err(Error::new(span, "only one `#[pallet::_]` attribute is supported on this item"))
	}
	Ok(attr)
}

fn partition_tasks_attrs(item_impl: &ItemImpl) -> (Vec<syn::Attribute>, Vec<syn::Attribute>) {
	item_impl.attrs.clone().into_iter().partition(|attr| {
		let mut path_segs = attr.path().segments.iter();
		let (Some(prefix), Some(suffix), None) =
			(path_segs.next(), path_segs.next(), path_segs.next())
		else {
			return false
		};
		prefix.ident == "pallet" && suffix.ident == "tasks_experimental"
	})
}

fn partition_task_attrs(item: &ImplItemFn) -> (Vec<syn::Attribute>, Vec<syn::Attribute>) {
	item.attrs.clone().into_iter().partition(|attr| {
		let mut path_segs = attr.path().segments.iter();
		let (Some(prefix), Some(suffix)) = (path_segs.next(), path_segs.next()) else {
			return false
		};
		// N.B: the `PartialEq` impl between `Ident` and `&str` is more efficient than
		// parsing and makes no stack or heap allocations
		prefix.ident == "pallet" &&
			(suffix.ident == "tasks_experimental" ||
				suffix.ident == "task_list" ||
				suffix.ident == "task_condition" ||
				suffix.ident == "task_weight" ||
				suffix.ident == "task_index")
	})
}

#[test]
fn test_parse_task_list_() {
	parse2::<TaskAttr>(quote!(#[pallet::task_list(Something::iter())])).unwrap();
	parse2::<TaskAttr>(quote!(#[pallet::task_list(Numbers::<T, I>::iter_keys())])).unwrap();
	parse2::<TaskAttr>(quote!(#[pallet::task_list(iter())])).unwrap();
	assert_parse_error_matches!(
		parse2::<TaskAttr>(quote!(#[pallet::task_list()])),
		"expected an expression"
	);
	assert_parse_error_matches!(
		parse2::<TaskAttr>(quote!(#[pallet::task_list])),
		"expected parentheses"
	);
}

#[test]
fn test_parse_task_index() {
	parse2::<TaskAttr>(quote!(#[pallet::task_index(3)])).unwrap();
	parse2::<TaskAttr>(quote!(#[pallet::task_index(0)])).unwrap();
	parse2::<TaskAttr>(quote!(#[pallet::task_index(17)])).unwrap();
	assert_parse_error_matches!(
		parse2::<TaskAttr>(quote!(#[pallet::task_index])),
		"expected parentheses"
	);
	assert_parse_error_matches!(
		parse2::<TaskAttr>(quote!(#[pallet::task_index("hey")])),
		"expected integer literal"
	);
	assert_parse_error_matches!(
		parse2::<TaskAttr>(quote!(#[pallet::task_index(0.3)])),
		"expected integer literal"
	);
}

#[test]
fn test_parse_task_condition() {
	parse2::<TaskAttr>(quote!(#[pallet::task_condition(|x| x.is_some())])).unwrap();
	parse2::<TaskAttr>(quote!(#[pallet::task_condition(|_x| some_expr())])).unwrap();
	parse2::<TaskAttr>(quote!(#[pallet::task_condition(|| some_expr())])).unwrap();
	parse2::<TaskAttr>(quote!(#[pallet::task_condition(some_expr())])).unwrap();
}

#[test]
fn test_parse_tasks_attr() {
	parse2::<PalletTasksAttr>(quote!(#[pallet::tasks_experimental])).unwrap();
	assert_parse_error_matches!(
		parse2::<PalletTasksAttr>(quote!(#[pallet::taskss])),
		"expected `tasks_experimental`"
	);
	assert_parse_error_matches!(
		parse2::<PalletTasksAttr>(quote!(#[pallet::tasks_])),
		"expected `tasks_experimental`"
	);
	assert_parse_error_matches!(
		parse2::<PalletTasksAttr>(quote!(#[pal::tasks])),
		"expected `pallet`"
	);
	assert_parse_error_matches!(
		parse2::<PalletTasksAttr>(quote!(#[pallet::tasks_experimental()])),
		"unexpected token"
	);
}

#[test]
fn test_parse_tasks_def_basic() {
	simulate_manifest_dir("../../examples/basic", || {
		let parsed = parse2::<TasksDef>(quote! {
			#[pallet::tasks_experimental]
			impl<T: Config<I>, I: 'static> Pallet<T, I> {
				/// Add a pair of numbers into the totals and remove them.
				#[pallet::task_list(Numbers::<T, I>::iter_keys())]
				#[pallet::task_condition(|i| Numbers::<T, I>::contains_key(i))]
				#[pallet::task_index(0)]
				#[pallet::task_weight(0)]
				pub fn add_number_into_total(i: u32) -> DispatchResult {
					let v = Numbers::<T, I>::take(i).ok_or(Error::<T, I>::NotFound)?;
					Total::<T, I>::mutate(|(total_keys, total_values)| {
						*total_keys += i;
						*total_values += v;
					});
					Ok(())
				}
			}
		})
		.unwrap();
		assert_eq!(parsed.tasks.len(), 1);
	});
}

#[test]
fn test_parse_tasks_def_basic_increment_decrement() {
	simulate_manifest_dir("../../examples/basic", || {
		let parsed = parse2::<TasksDef>(quote! {
			#[pallet::tasks_experimental]
			impl<T: Config<I>, I: 'static> Pallet<T, I> {
				/// Get the value and check if it can be incremented
				#[pallet::task_index(0)]
				#[pallet::task_condition(|| {
					let value = Value::<T>::get().unwrap();
					value < 255
				})]
				#[pallet::task_list(Vec::<Task<T>>::new())]
				#[pallet::task_weight(0)]
				fn increment() -> DispatchResult {
					let value = Value::<T>::get().unwrap_or_default();
					if value >= 255 {
						Err(Error::<T>::ValueOverflow.into())
					} else {
						let new_val = value.checked_add(1).ok_or(Error::<T>::ValueOverflow)?;
						Value::<T>::put(new_val);
						Pallet::<T>::deposit_event(Event::Incremented { new_val });
						Ok(())
					}
				}

				// Get the value and check if it can be decremented
				#[pallet::task_index(1)]
				#[pallet::task_condition(|| {
					let value = Value::<T>::get().unwrap();
					value > 0
				})]
				#[pallet::task_list(Vec::<Task<T>>::new())]
				#[pallet::task_weight(0)]
				fn decrement() -> DispatchResult {
					let value = Value::<T>::get().unwrap_or_default();
					if value == 0 {
						Err(Error::<T>::ValueUnderflow.into())
					} else {
						let new_val = value.checked_sub(1).ok_or(Error::<T>::ValueUnderflow)?;
						Value::<T>::put(new_val);
						Pallet::<T>::deposit_event(Event::Decremented { new_val });
						Ok(())
					}
				}
			}
		})
		.unwrap();
		assert_eq!(parsed.tasks.len(), 2);
	});
}

#[test]
fn test_parse_tasks_def_duplicate_index() {
	simulate_manifest_dir("../../examples/basic", || {
		assert_parse_error_matches!(
			parse2::<TasksDef>(quote! {
				#[pallet::tasks_experimental]
				impl<T: Config<I>, I: 'static> Pallet<T, I> {
					#[pallet::task_list(Something::iter())]
					#[pallet::task_condition(|i| i % 2 == 0)]
					#[pallet::task_index(0)]
					#[pallet::task_weight(0)]
					pub fn foo(i: u32) -> DispatchResult {
						Ok(())
					}

					#[pallet::task_list(Numbers::<T, I>::iter_keys())]
					#[pallet::task_condition(|i| Numbers::<T, I>::contains_key(i))]
					#[pallet::task_index(0)]
					#[pallet::task_weight(0)]
					pub fn bar(i: u32) -> DispatchResult {
						Ok(())
					}
				}
			}),
			"duplicate task index `0`"
		);
	});
}

#[test]
fn test_parse_tasks_def_missing_task_list() {
	simulate_manifest_dir("../../examples/basic", || {
		assert_parse_error_matches!(
			parse2::<TasksDef>(quote! {
				#[pallet::tasks_experimental]
				impl<T: Config<I>, I: 'static> Pallet<T, I> {
					#[pallet::task_condition(|i| i % 2 == 0)]
					#[pallet::task_index(0)]
					pub fn foo(i: u32) -> DispatchResult {
						Ok(())
					}
				}
			}),
			r"missing `#\[pallet::task_list\(\.\.\)\]`"
		);
	});
}

#[test]
fn test_parse_tasks_def_missing_task_condition() {
	simulate_manifest_dir("../../examples/basic", || {
		assert_parse_error_matches!(
			parse2::<TasksDef>(quote! {
				#[pallet::tasks_experimental]
				impl<T: Config<I>, I: 'static> Pallet<T, I> {
					#[pallet::task_list(Something::iter())]
					#[pallet::task_index(0)]
					pub fn foo(i: u32) -> DispatchResult {
						Ok(())
					}
				}
			}),
			r"missing `#\[pallet::task_condition\(\.\.\)\]`"
		);
	});
}

#[test]
fn test_parse_tasks_def_missing_task_index() {
	simulate_manifest_dir("../../examples/basic", || {
		assert_parse_error_matches!(
			parse2::<TasksDef>(quote! {
				#[pallet::tasks_experimental]
				impl<T: Config<I>, I: 'static> Pallet<T, I> {
					#[pallet::task_condition(|i| i % 2 == 0)]
					#[pallet::task_list(Something::iter())]
					pub fn foo(i: u32) -> DispatchResult {
						Ok(())
					}
				}
			}),
			r"missing `#\[pallet::task_index\(\.\.\)\]`"
		);
	});
}

#[test]
fn test_parse_tasks_def_missing_task_weight() {
	simulate_manifest_dir("../../examples/basic", || {
		assert_parse_error_matches!(
			parse2::<TasksDef>(quote! {
				#[pallet::tasks_experimental]
				impl<T: Config<I>, I: 'static> Pallet<T, I> {
					#[pallet::task_condition(|i| i % 2 == 0)]
					#[pallet::task_list(Something::iter())]
					#[pallet::task_index(0)]
					pub fn foo(i: u32) -> DispatchResult {
						Ok(())
					}
				}
			}),
			r"missing `#\[pallet::task_weight\(\.\.\)\]`"
		);
	});
}

#[test]
fn test_parse_tasks_def_unexpected_extra_task_list_attr() {
	simulate_manifest_dir("../../examples/basic", || {
		assert_parse_error_matches!(
			parse2::<TasksDef>(quote! {
				#[pallet::tasks_experimental]
				impl<T: Config<I>, I: 'static> Pallet<T, I> {
					#[pallet::task_condition(|i| i % 2 == 0)]
					#[pallet::task_index(0)]
					#[pallet::task_weight(0)]
					#[pallet::task_list(Something::iter())]
					#[pallet::task_list(SomethingElse::iter())]
					pub fn foo(i: u32) -> DispatchResult {
						Ok(())
					}
				}
			}),
			r"unexpected extra `#\[pallet::task_list\(\.\.\)\]`"
		);
	});
}

#[test]
fn test_parse_tasks_def_unexpected_extra_task_condition_attr() {
	simulate_manifest_dir("../../examples/basic", || {
		assert_parse_error_matches!(
			parse2::<TasksDef>(quote! {
				#[pallet::tasks_experimental]
				impl<T: Config<I>, I: 'static> Pallet<T, I> {
					#[pallet::task_condition(|i| i % 2 == 0)]
					#[pallet::task_condition(|i| i % 4 == 0)]
					#[pallet::task_index(0)]
					#[pallet::task_list(Something::iter())]
					#[pallet::task_weight(0)]
					pub fn foo(i: u32) -> DispatchResult {
						Ok(())
					}
				}
			}),
			r"unexpected extra `#\[pallet::task_condition\(\.\.\)\]`"
		);
	});
}

#[test]
fn test_parse_tasks_def_unexpected_extra_task_index_attr() {
	simulate_manifest_dir("../../examples/basic", || {
		assert_parse_error_matches!(
			parse2::<TasksDef>(quote! {
				#[pallet::tasks_experimental]
				impl<T: Config<I>, I: 'static> Pallet<T, I> {
					#[pallet::task_condition(|i| i % 2 == 0)]
					#[pallet::task_index(0)]
					#[pallet::task_index(0)]
					#[pallet::task_list(Something::iter())]
					#[pallet::task_weight(0)]
					pub fn foo(i: u32) -> DispatchResult {
						Ok(())
					}
				}
			}),
			r"unexpected extra `#\[pallet::task_index\(\.\.\)\]`"
		);
	});
}

#[test]
fn test_parse_tasks_def_extra_tasks_attribute() {
	simulate_manifest_dir("../../examples/basic", || {
		assert_parse_error_matches!(
			parse2::<TasksDef>(quote! {
				#[pallet::tasks_experimental]
				#[pallet::tasks_experimental]
				impl<T: Config<I>, I: 'static> Pallet<T, I> {}
			}),
			r"unexpected extra `#\[pallet::tasks_experimental\]` attribute"
		);
	});
}

#[test]
fn test_parse_task_enum_def_basic() {
	simulate_manifest_dir("../../examples/basic", || {
		parse2::<TaskEnumDef>(quote! {
			#[pallet::task_enum]
			pub enum Task<T: Config> {
				Increment,
				Decrement,
			}
		})
		.unwrap();
	});
}

#[test]
fn test_parse_task_enum_def_non_task_name() {
	simulate_manifest_dir("../../examples/basic", || {
		parse2::<TaskEnumDef>(quote! {
			#[pallet::task_enum]
			pub enum Something {
				Foo
			}
		})
		.unwrap();
	});
}

#[test]
fn test_parse_task_enum_def_missing_attr_allowed() {
	simulate_manifest_dir("../../examples/basic", || {
		parse2::<TaskEnumDef>(quote! {
			pub enum Task<T: Config> {
				Increment,
				Decrement,
			}
		})
		.unwrap();
	});
}

#[test]
fn test_parse_task_enum_def_missing_attr_alternate_name_allowed() {
	simulate_manifest_dir("../../examples/basic", || {
		parse2::<TaskEnumDef>(quote! {
			pub enum Foo {
				Red,
			}
		})
		.unwrap();
	});
}

#[test]
fn test_parse_task_enum_def_wrong_attr() {
	simulate_manifest_dir("../../examples/basic", || {
		assert_parse_error_matches!(
			parse2::<TaskEnumDef>(quote! {
				#[pallet::something]
				pub enum Task<T: Config> {
					Increment,
					Decrement,
				}
			}),
			"expected `task_enum`"
		);
	});
}

#[test]
fn test_parse_task_enum_def_wrong_item() {
	simulate_manifest_dir("../../examples/basic", || {
		assert_parse_error_matches!(
			parse2::<TaskEnumDef>(quote! {
				#[pallet::task_enum]
				pub struct Something;
			}),
			"expected `enum`"
		);
	});
}