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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::*;
use syn::ext::IdentExt;
use syn::parse::{Error as ParseError, Parse, ParseStream, Result as ParseResult};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::Comma;
use syn::{bracketed, Expr, Ident, LitStr, Token};
pub fn parse(
f: &syn::Field,
f_ty: Option<&Ty>,
has_instruction_api: bool,
) -> ParseResult<(ConstraintGroup, ConstraintGroup)> {
let mut constraints = ConstraintGroupBuilder::new(f_ty);
for attr in f.attrs.iter().filter(is_account) {
for c in attr.parse_args_with(Punctuated::<ConstraintToken, Comma>::parse_terminated)? {
constraints.add(c)?;
}
}
let account_constraints = constraints.build()?;
let mut constraints = ConstraintGroupBuilder::new(f_ty);
for attr in f.attrs.iter().filter(is_instruction) {
if !has_instruction_api {
return Err(ParseError::new(
attr.span(),
"an instruction api must be declared",
));
}
for c in attr.parse_args_with(Punctuated::<ConstraintToken, Comma>::parse_terminated)? {
constraints.add(c)?;
}
}
let instruction_constraints = constraints.build()?;
Ok((account_constraints, instruction_constraints))
}
pub fn is_account(attr: &&syn::Attribute) -> bool {
attr.path
.get_ident()
.map_or(false, |ident| ident == "account")
}
pub fn is_instruction(attr: &&syn::Attribute) -> bool {
attr.path
.get_ident()
.map_or(false, |ident| ident == "instruction")
}
pub fn parse_token(stream: ParseStream) -> ParseResult<ConstraintToken> {
let is_lit = stream.peek(LitStr);
if is_lit {
let lit: LitStr = stream.parse()?;
let c = ConstraintToken::Literal(Context::new(lit.span(), ConstraintLiteral { lit }));
return Ok(c);
}
let ident = stream.call(Ident::parse_any)?;
let kw = ident.to_string();
let c = match kw.as_str() {
"init" => ConstraintToken::Init(Context::new(
ident.span(),
ConstraintInit { if_needed: false },
)),
"init_if_needed" => ConstraintToken::Init(Context::new(
ident.span(),
ConstraintInit { if_needed: true },
)),
"zero" => ConstraintToken::Zeroed(Context::new(ident.span(), ConstraintZeroed {})),
"mut" => ConstraintToken::Mut(Context::new(
ident.span(),
ConstraintMut {
error: parse_optional_custom_error(&stream)?,
},
)),
"signer" => ConstraintToken::Signer(Context::new(
ident.span(),
ConstraintSigner {
error: parse_optional_custom_error(&stream)?,
},
)),
"executable" => {
ConstraintToken::Executable(Context::new(ident.span(), ConstraintExecutable {}))
}
"mint" => {
stream.parse::<Token![:]>()?;
stream.parse::<Token![:]>()?;
let kw = stream.call(Ident::parse_any)?.to_string();
stream.parse::<Token![=]>()?;
let span = ident
.span()
.join(stream.span())
.unwrap_or_else(|| ident.span());
match kw.as_str() {
"authority" => ConstraintToken::MintAuthority(Context::new(
span,
ConstraintMintAuthority {
mint_auth: stream.parse()?,
},
)),
"freeze_authority" => ConstraintToken::MintFreezeAuthority(Context::new(
span,
ConstraintMintFreezeAuthority {
mint_freeze_auth: stream.parse()?,
},
)),
"decimals" => ConstraintToken::MintDecimals(Context::new(
span,
ConstraintMintDecimals {
decimals: stream.parse()?,
},
)),
_ => return Err(ParseError::new(ident.span(), "Invalid attribute")),
}
}
"token" => {
stream.parse::<Token![:]>()?;
stream.parse::<Token![:]>()?;
let kw = stream.call(Ident::parse_any)?.to_string();
stream.parse::<Token![=]>()?;
let span = ident
.span()
.join(stream.span())
.unwrap_or_else(|| ident.span());
match kw.as_str() {
"mint" => ConstraintToken::TokenMint(Context::new(
span,
ConstraintTokenMint {
mint: stream.parse()?,
},
)),
"authority" => ConstraintToken::TokenAuthority(Context::new(
span,
ConstraintTokenAuthority {
auth: stream.parse()?,
},
)),
_ => return Err(ParseError::new(ident.span(), "Invalid attribute")),
}
}
"associated_token" => {
stream.parse::<Token![:]>()?;
stream.parse::<Token![:]>()?;
let kw = stream.call(Ident::parse_any)?.to_string();
stream.parse::<Token![=]>()?;
let span = ident
.span()
.join(stream.span())
.unwrap_or_else(|| ident.span());
match kw.as_str() {
"mint" => ConstraintToken::AssociatedTokenMint(Context::new(
span,
ConstraintTokenMint {
mint: stream.parse()?,
},
)),
"authority" => ConstraintToken::AssociatedTokenAuthority(Context::new(
span,
ConstraintTokenAuthority {
auth: stream.parse()?,
},
)),
_ => return Err(ParseError::new(ident.span(), "Invalid attribute")),
}
}
"bump" => {
let bump = {
if stream.peek(Token![=]) {
stream.parse::<Token![=]>()?;
Some(stream.parse()?)
} else {
None
}
};
ConstraintToken::Bump(Context::new(ident.span(), ConstraintTokenBump { bump }))
}
"seeds" => {
if stream.peek(Token![:]) {
stream.parse::<Token![:]>()?;
stream.parse::<Token![:]>()?;
let kw = stream.call(Ident::parse_any)?.to_string();
stream.parse::<Token![=]>()?;
let span = ident
.span()
.join(stream.span())
.unwrap_or_else(|| ident.span());
match kw.as_str() {
"program" => ConstraintToken::ProgramSeed(Context::new(
span,
ConstraintProgramSeed {
program_seed: stream.parse()?,
},
)),
_ => return Err(ParseError::new(ident.span(), "Invalid attribute")),
}
} else {
stream.parse::<Token![=]>()?;
let span = ident
.span()
.join(stream.span())
.unwrap_or_else(|| ident.span());
let seeds;
let bracket = bracketed!(seeds in stream);
ConstraintToken::Seeds(Context::new(
span.join(bracket.span).unwrap_or(span),
ConstraintSeeds {
seeds: seeds.parse_terminated(Expr::parse)?,
},
))
}
}
_ => {
stream.parse::<Token![=]>()?;
let span = ident
.span()
.join(stream.span())
.unwrap_or_else(|| ident.span());
match kw.as_str() {
"has_one" => ConstraintToken::HasOne(Context::new(
span,
ConstraintHasOne {
join_target: stream.parse()?,
error: parse_optional_custom_error(&stream)?,
},
)),
"owner" => ConstraintToken::Owner(Context::new(
span,
ConstraintOwner {
owner_address: stream.parse()?,
error: parse_optional_custom_error(&stream)?,
},
)),
"rent_exempt" => ConstraintToken::RentExempt(Context::new(
span,
match stream.parse::<Ident>()?.to_string().as_str() {
"skip" => ConstraintRentExempt::Skip,
"enforce" => ConstraintRentExempt::Enforce,
_ => {
return Err(ParseError::new(
span,
"rent_exempt must be either skip or enforce",
))
}
},
)),
"state" => ConstraintToken::State(Context::new(
span,
ConstraintState {
program_target: stream.parse()?,
},
)),
"payer" => ConstraintToken::Payer(Context::new(
span,
ConstraintPayer {
target: stream.parse()?,
},
)),
"space" => ConstraintToken::Space(Context::new(
span,
ConstraintSpace {
space: stream.parse()?,
},
)),
"constraint" => ConstraintToken::Raw(Context::new(
span,
ConstraintRaw {
raw: stream.parse()?,
error: parse_optional_custom_error(&stream)?,
},
)),
"close" => ConstraintToken::Close(Context::new(
span,
ConstraintClose {
sol_dest: stream.parse()?,
},
)),
"address" => ConstraintToken::Address(Context::new(
span,
ConstraintAddress {
address: stream.parse()?,
error: parse_optional_custom_error(&stream)?,
},
)),
_ => return Err(ParseError::new(ident.span(), "Invalid attribute")),
}
}
};
Ok(c)
}
fn parse_optional_custom_error(stream: &ParseStream) -> ParseResult<Option<Expr>> {
if stream.peek(Token![@]) {
stream.parse::<Token![@]>()?;
stream.parse().map(Some)
} else {
Ok(None)
}
}
#[derive(Default)]
pub struct ConstraintGroupBuilder<'ty> {
pub f_ty: Option<&'ty Ty>,
pub init: Option<Context<ConstraintInit>>,
pub zeroed: Option<Context<ConstraintZeroed>>,
pub mutable: Option<Context<ConstraintMut>>,
pub signer: Option<Context<ConstraintSigner>>,
pub has_one: Vec<Context<ConstraintHasOne>>,
pub literal: Vec<Context<ConstraintLiteral>>,
pub raw: Vec<Context<ConstraintRaw>>,
pub owner: Option<Context<ConstraintOwner>>,
pub rent_exempt: Option<Context<ConstraintRentExempt>>,
pub seeds: Option<Context<ConstraintSeeds>>,
pub executable: Option<Context<ConstraintExecutable>>,
pub state: Option<Context<ConstraintState>>,
pub payer: Option<Context<ConstraintPayer>>,
pub space: Option<Context<ConstraintSpace>>,
pub close: Option<Context<ConstraintClose>>,
pub address: Option<Context<ConstraintAddress>>,
pub token_mint: Option<Context<ConstraintTokenMint>>,
pub token_authority: Option<Context<ConstraintTokenAuthority>>,
pub associated_token_mint: Option<Context<ConstraintTokenMint>>,
pub associated_token_authority: Option<Context<ConstraintTokenAuthority>>,
pub mint_authority: Option<Context<ConstraintMintAuthority>>,
pub mint_freeze_authority: Option<Context<ConstraintMintFreezeAuthority>>,
pub mint_decimals: Option<Context<ConstraintMintDecimals>>,
pub bump: Option<Context<ConstraintTokenBump>>,
pub program_seed: Option<Context<ConstraintProgramSeed>>,
}
impl<'ty> ConstraintGroupBuilder<'ty> {
pub fn new(f_ty: Option<&'ty Ty>) -> Self {
Self {
f_ty,
init: None,
zeroed: None,
mutable: None,
signer: None,
has_one: Vec::new(),
literal: Vec::new(),
raw: Vec::new(),
owner: None,
rent_exempt: None,
seeds: None,
executable: None,
state: None,
payer: None,
space: None,
close: None,
address: None,
token_mint: None,
token_authority: None,
associated_token_mint: None,
associated_token_authority: None,
mint_authority: None,
mint_freeze_authority: None,
mint_decimals: None,
bump: None,
program_seed: None,
}
}
pub fn build(mut self) -> ParseResult<ConstraintGroup> {
if let Some(i) = &self.init {
if cfg!(not(feature = "init-if-needed")) && i.if_needed {
return Err(ParseError::new(
i.span(),
"init_if_needed requires that anchor-lang be imported \
with the init-if-needed cargo feature enabled. \
Carefully read the init_if_needed docs before using this feature \
to make sure you know how to protect yourself against \
re-initialization attacks.",
));
}
match self.mutable {
Some(m) => {
return Err(ParseError::new(
m.span(),
"mut cannot be provided with init",
))
}
None => self
.mutable
.replace(Context::new(i.span(), ConstraintMut { error: None })),
};
if self.rent_exempt.is_none() {
self.rent_exempt
.replace(Context::new(i.span(), ConstraintRentExempt::Enforce));
}
if self.payer.is_none() {
return Err(ParseError::new(
i.span(),
"payer must be provided when initializing an account",
));
}
if self.signer.is_none() && self.seeds.is_none() && self.associated_token_mint.is_none()
{
self.signer
.replace(Context::new(i.span(), ConstraintSigner { error: None }));
}
if let Some(b) = &self.bump {
if b.bump.is_some() {
return Err(ParseError::new(
b.span(),
"bump targets should not be provided with init. Please use bump without a target."
));
}
}
}
if let Some(z) = &self.zeroed {
match self.mutable {
Some(m) => {
return Err(ParseError::new(
m.span(),
"mut cannot be provided with zeroed",
))
}
None => self
.mutable
.replace(Context::new(z.span(), ConstraintMut { error: None })),
};
if self.rent_exempt.is_none() {
self.rent_exempt
.replace(Context::new(z.span(), ConstraintRentExempt::Enforce));
}
}
if let Some(i) = &self.seeds {
if self.init.is_some() && self.payer.is_none() {
return Err(ParseError::new(
i.span(),
"payer must be provided when creating a program derived address",
));
}
if self.bump.is_none() {
return Err(ParseError::new(
i.span(),
"bump must be provided with seeds",
));
}
}
if let Some(token_mint) = &self.token_mint {
if self.token_authority.is_none() {
return Err(ParseError::new(
token_mint.span(),
"token authority must be provided if token mint is",
));
}
if self.init.is_none() {
return Err(ParseError::new(
token_mint.span(),
"init is required for a pda token",
));
}
}
if let Some(token_authority) = &self.token_authority {
if self.token_mint.is_none() {
return Err(ParseError::new(
token_authority.span(),
"token mint must be provided if token authority is",
));
}
}
if let Some(mint_decimals) = &self.mint_decimals {
if self.mint_authority.is_none() {
return Err(ParseError::new(
mint_decimals.span(),
"mint authority must be provided if mint decimals is",
));
}
}
if let Some(mint_authority) = &self.mint_authority {
if self.mint_decimals.is_none() {
return Err(ParseError::new(
mint_authority.span(),
"mint decimals must be provided if mint authority is",
));
}
}
if self.init.is_some()
&& self.seeds.is_some()
&& self.token_mint.is_some()
&& (self.mint_authority.is_some() || self.token_authority.is_some())
&& self.space.is_some()
{
return Err(ParseError::new(
self.space.as_ref().unwrap().span(),
"space is not required for initializing an spl account",
));
}
let ConstraintGroupBuilder {
f_ty: _,
init,
zeroed,
mutable,
signer,
has_one,
literal,
raw,
owner,
rent_exempt,
seeds,
executable,
state,
payer,
space,
close,
address,
token_mint,
token_authority,
associated_token_mint,
associated_token_authority,
mint_authority,
mint_freeze_authority,
mint_decimals,
bump,
program_seed,
} = self;
macro_rules! into_inner {
($opt:ident) => {
$opt.map(|c| c.into_inner())
};
($opt:expr) => {
$opt.map(|c| c.into_inner())
};
}
macro_rules! into_inner_vec {
($opt:ident) => {
$opt.into_iter().map(|c| c.into_inner()).collect()
};
}
let is_init = init.is_some();
let seeds = seeds.map(|c| ConstraintSeedsGroup {
is_init,
seeds: c.seeds.clone(),
bump: into_inner!(bump)
.map(|b| b.bump)
.expect("bump must be provided with seeds"),
program_seed: into_inner!(program_seed).map(|id| id.program_seed),
});
let associated_token = match (associated_token_mint, associated_token_authority) {
(Some(mint), Some(auth)) => Some(ConstraintAssociatedToken {
wallet: auth.into_inner().auth,
mint: mint.into_inner().mint,
}),
(Some(mint), None) => return Err(ParseError::new(
mint.span(),
"authority must be provided to specify an associated token program derived address",
)),
(None, Some(auth)) => {
return Err(ParseError::new(
auth.span(),
"mint must be provided to specify an associated token program derived address",
))
}
_ => None,
};
Ok(ConstraintGroup {
init: init.as_ref().map(|i| Ok(ConstraintInitGroup {
if_needed: i.if_needed,
seeds: seeds.clone(),
payer: into_inner!(payer.clone()).map(|a| a.target),
space: space.clone().map(|s| s.space.clone()),
kind: if let Some(tm) = &token_mint {
InitKind::Token {
mint: tm.clone().into_inner().mint,
owner: match &token_authority {
Some(a) => a.clone().into_inner().auth,
None => return Err(ParseError::new(
tm.span(),
"authority must be provided to initialize a token program derived address"
)),
},
}
} else if let Some(at) = &associated_token {
InitKind::AssociatedToken {
mint: at.mint.clone(),
owner: at.wallet.clone()
}
} else if let Some(d) = &mint_decimals {
InitKind::Mint {
decimals: d.clone().into_inner().decimals,
owner: match &mint_authority {
Some(a) => a.clone().into_inner().mint_auth,
None => return Err(ParseError::new(
d.span(),
"authority must be provided to initialize a mint program derived address"
))
},
freeze_authority: mint_freeze_authority.map(|fa| fa.into_inner().mint_freeze_auth)
}
} else {
InitKind::Program {
owner: owner.as_ref().map(|o| o.owner_address.clone()),
}
},
})).transpose()?,
zeroed: into_inner!(zeroed),
mutable: into_inner!(mutable),
signer: into_inner!(signer),
has_one: into_inner_vec!(has_one),
literal: into_inner_vec!(literal),
raw: into_inner_vec!(raw),
owner: into_inner!(owner),
rent_exempt: into_inner!(rent_exempt),
executable: into_inner!(executable),
state: into_inner!(state),
close: into_inner!(close),
address: into_inner!(address),
associated_token: if !is_init { associated_token } else { None },
seeds,
})
}
pub fn add(&mut self, c: ConstraintToken) -> ParseResult<()> {
match c {
ConstraintToken::Init(c) => self.add_init(c),
ConstraintToken::Zeroed(c) => self.add_zeroed(c),
ConstraintToken::Mut(c) => self.add_mut(c),
ConstraintToken::Signer(c) => self.add_signer(c),
ConstraintToken::HasOne(c) => self.add_has_one(c),
ConstraintToken::Literal(c) => self.add_literal(c),
ConstraintToken::Raw(c) => self.add_raw(c),
ConstraintToken::Owner(c) => self.add_owner(c),
ConstraintToken::RentExempt(c) => self.add_rent_exempt(c),
ConstraintToken::Seeds(c) => self.add_seeds(c),
ConstraintToken::Executable(c) => self.add_executable(c),
ConstraintToken::State(c) => self.add_state(c),
ConstraintToken::Payer(c) => self.add_payer(c),
ConstraintToken::Space(c) => self.add_space(c),
ConstraintToken::Close(c) => self.add_close(c),
ConstraintToken::Address(c) => self.add_address(c),
ConstraintToken::TokenAuthority(c) => self.add_token_authority(c),
ConstraintToken::TokenMint(c) => self.add_token_mint(c),
ConstraintToken::AssociatedTokenAuthority(c) => self.add_associated_token_authority(c),
ConstraintToken::AssociatedTokenMint(c) => self.add_associated_token_mint(c),
ConstraintToken::MintAuthority(c) => self.add_mint_authority(c),
ConstraintToken::MintFreezeAuthority(c) => self.add_mint_freeze_authority(c),
ConstraintToken::MintDecimals(c) => self.add_mint_decimals(c),
ConstraintToken::Bump(c) => self.add_bump(c),
ConstraintToken::ProgramSeed(c) => self.add_program_seed(c),
}
}
fn add_init(&mut self, c: Context<ConstraintInit>) -> ParseResult<()> {
if self.init.is_some() {
return Err(ParseError::new(c.span(), "init already provided"));
}
if self.zeroed.is_some() {
return Err(ParseError::new(c.span(), "zeroed already provided"));
}
self.init.replace(c);
Ok(())
}
fn add_zeroed(&mut self, c: Context<ConstraintZeroed>) -> ParseResult<()> {
if self.zeroed.is_some() {
return Err(ParseError::new(c.span(), "zeroed already provided"));
}
if self.init.is_some() {
return Err(ParseError::new(c.span(), "init already provided"));
}
self.zeroed.replace(c);
Ok(())
}
fn add_close(&mut self, c: Context<ConstraintClose>) -> ParseResult<()> {
if !matches!(self.f_ty, Some(Ty::ProgramAccount(_)))
&& !matches!(self.f_ty, Some(Ty::Account(_)))
&& !matches!(self.f_ty, Some(Ty::Loader(_)))
&& !matches!(self.f_ty, Some(Ty::AccountLoader(_)))
{
return Err(ParseError::new(
c.span(),
"close must be on an Account, ProgramAccount, or Loader",
));
}
if self.mutable.is_none() {
return Err(ParseError::new(
c.span(),
"mut must be provided before close",
));
}
if self.close.is_some() {
return Err(ParseError::new(c.span(), "close already provided"));
}
self.close.replace(c);
Ok(())
}
fn add_address(&mut self, c: Context<ConstraintAddress>) -> ParseResult<()> {
if self.address.is_some() {
return Err(ParseError::new(c.span(), "address already provided"));
}
self.address.replace(c);
Ok(())
}
fn add_token_mint(&mut self, c: Context<ConstraintTokenMint>) -> ParseResult<()> {
if self.token_mint.is_some() {
return Err(ParseError::new(c.span(), "token mint already provided"));
}
if self.associated_token_mint.is_some() {
return Err(ParseError::new(
c.span(),
"associated token mint already provided",
));
}
if self.init.is_none() {
return Err(ParseError::new(
c.span(),
"init must be provided before token",
));
}
self.token_mint.replace(c);
Ok(())
}
fn add_associated_token_mint(&mut self, c: Context<ConstraintTokenMint>) -> ParseResult<()> {
if self.associated_token_mint.is_some() {
return Err(ParseError::new(
c.span(),
"associated token mint already provided",
));
}
if self.token_mint.is_some() {
return Err(ParseError::new(c.span(), "token mint already provided"));
}
self.associated_token_mint.replace(c);
Ok(())
}
fn add_bump(&mut self, c: Context<ConstraintTokenBump>) -> ParseResult<()> {
if self.bump.is_some() {
return Err(ParseError::new(c.span(), "bump already provided"));
}
if self.seeds.is_none() {
return Err(ParseError::new(
c.span(),
"seeds must be provided before bump",
));
}
self.bump.replace(c);
Ok(())
}
fn add_program_seed(&mut self, c: Context<ConstraintProgramSeed>) -> ParseResult<()> {
if self.program_seed.is_some() {
return Err(ParseError::new(c.span(), "seeds::program already provided"));
}
if self.seeds.is_none() {
return Err(ParseError::new(
c.span(),
"seeds must be provided before seeds::program",
));
}
if let Some(ref init) = self.init {
if init.if_needed {
return Err(ParseError::new(
c.span(),
"seeds::program cannot be used with init_if_needed",
));
} else {
return Err(ParseError::new(
c.span(),
"seeds::program cannot be used with init",
));
}
}
self.program_seed.replace(c);
Ok(())
}
fn add_token_authority(&mut self, c: Context<ConstraintTokenAuthority>) -> ParseResult<()> {
if self.token_authority.is_some() {
return Err(ParseError::new(
c.span(),
"token authority already provided",
));
}
if self.init.is_none() {
return Err(ParseError::new(
c.span(),
"init must be provided before token authority",
));
}
self.token_authority.replace(c);
Ok(())
}
fn add_associated_token_authority(
&mut self,
c: Context<ConstraintTokenAuthority>,
) -> ParseResult<()> {
if self.associated_token_authority.is_some() {
return Err(ParseError::new(
c.span(),
"associated token authority already provided",
));
}
if self.token_authority.is_some() {
return Err(ParseError::new(
c.span(),
"token authority already provided",
));
}
self.associated_token_authority.replace(c);
Ok(())
}
fn add_mint_authority(&mut self, c: Context<ConstraintMintAuthority>) -> ParseResult<()> {
if self.mint_authority.is_some() {
return Err(ParseError::new(c.span(), "mint authority already provided"));
}
if self.init.is_none() {
return Err(ParseError::new(
c.span(),
"init must be provided before mint authority",
));
}
self.mint_authority.replace(c);
Ok(())
}
fn add_mint_freeze_authority(
&mut self,
c: Context<ConstraintMintFreezeAuthority>,
) -> ParseResult<()> {
if self.mint_freeze_authority.is_some() {
return Err(ParseError::new(
c.span(),
"mint freeze_authority already provided",
));
}
if self.init.is_none() {
return Err(ParseError::new(
c.span(),
"init must be provided before mint freeze_authority",
));
}
self.mint_freeze_authority.replace(c);
Ok(())
}
fn add_mint_decimals(&mut self, c: Context<ConstraintMintDecimals>) -> ParseResult<()> {
if self.mint_decimals.is_some() {
return Err(ParseError::new(c.span(), "mint decimals already provided"));
}
if self.init.is_none() {
return Err(ParseError::new(
c.span(),
"init must be provided before mint decimals",
));
}
self.mint_decimals.replace(c);
Ok(())
}
fn add_mut(&mut self, c: Context<ConstraintMut>) -> ParseResult<()> {
if self.mutable.is_some() {
return Err(ParseError::new(c.span(), "mut already provided"));
}
self.mutable.replace(c);
Ok(())
}
fn add_signer(&mut self, c: Context<ConstraintSigner>) -> ParseResult<()> {
if self.signer.is_some() {
return Err(ParseError::new(c.span(), "signer already provided"));
}
self.signer.replace(c);
Ok(())
}
fn add_has_one(&mut self, c: Context<ConstraintHasOne>) -> ParseResult<()> {
if self
.has_one
.iter()
.filter(|item| item.join_target == c.join_target)
.count()
> 0
{
return Err(ParseError::new(c.span(), "has_one target already provided"));
}
self.has_one.push(c);
Ok(())
}
fn add_literal(&mut self, c: Context<ConstraintLiteral>) -> ParseResult<()> {
self.literal.push(c);
Ok(())
}
fn add_raw(&mut self, c: Context<ConstraintRaw>) -> ParseResult<()> {
self.raw.push(c);
Ok(())
}
fn add_owner(&mut self, c: Context<ConstraintOwner>) -> ParseResult<()> {
if self.owner.is_some() {
return Err(ParseError::new(c.span(), "owner already provided"));
}
self.owner.replace(c);
Ok(())
}
fn add_rent_exempt(&mut self, c: Context<ConstraintRentExempt>) -> ParseResult<()> {
if self.rent_exempt.is_some() {
return Err(ParseError::new(c.span(), "rent already provided"));
}
self.rent_exempt.replace(c);
Ok(())
}
fn add_seeds(&mut self, c: Context<ConstraintSeeds>) -> ParseResult<()> {
if self.seeds.is_some() {
return Err(ParseError::new(c.span(), "seeds already provided"));
}
self.seeds.replace(c);
Ok(())
}
fn add_executable(&mut self, c: Context<ConstraintExecutable>) -> ParseResult<()> {
if self.executable.is_some() {
return Err(ParseError::new(c.span(), "executable already provided"));
}
self.executable.replace(c);
Ok(())
}
fn add_state(&mut self, c: Context<ConstraintState>) -> ParseResult<()> {
if self.state.is_some() {
return Err(ParseError::new(c.span(), "state already provided"));
}
self.state.replace(c);
Ok(())
}
fn add_payer(&mut self, c: Context<ConstraintPayer>) -> ParseResult<()> {
if self.init.is_none() {
return Err(ParseError::new(
c.span(),
"init must be provided before payer",
));
}
if self.payer.is_some() {
return Err(ParseError::new(c.span(), "payer already provided"));
}
self.payer.replace(c);
Ok(())
}
fn add_space(&mut self, c: Context<ConstraintSpace>) -> ParseResult<()> {
if self.init.is_none() {
return Err(ParseError::new(
c.span(),
"init must be provided before space",
));
}
if self.space.is_some() {
return Err(ParseError::new(c.span(), "space already provided"));
}
self.space.replace(c);
Ok(())
}
}