mpl_token_auth_rules/state/v2/constraint/
not.rs

1use solana_program::msg;
2
3use crate::{
4    error::RuleSetError,
5    state::v2::{Constraint, ConstraintType, RuleV2, HEADER_SECTION},
6    state::{Header, RuleResult},
7};
8
9/// Constraint representing a negation, where the contained rule must fail.
10pub struct Not<'a> {
11    /// The Rule contained under Not.
12    pub rule: RuleV2<'a>,
13}
14
15impl<'a> Not<'a> {
16    /// Deserialize a constraint from a byte array.
17    pub fn from_bytes(bytes: &'a [u8]) -> Result<Self, RuleSetError> {
18        let rule = RuleV2::from_bytes(bytes)?;
19        Ok(Self { rule })
20    }
21
22    /// Serialize a constraint into a byte array.
23    pub fn serialize(rule: &[u8]) -> Result<Vec<u8>, RuleSetError> {
24        let mut data = Vec::with_capacity(HEADER_SECTION + rule.len());
25
26        // Header
27        Header::serialize(ConstraintType::Not, rule.len() as u32, &mut data);
28
29        // Constraint
30        // - rule
31        data.extend(rule);
32
33        Ok(data)
34    }
35}
36
37impl<'a> Constraint<'a> for Not<'a> {
38    fn constraint_type(&self) -> ConstraintType {
39        ConstraintType::Not
40    }
41
42    fn validate(
43        &self,
44        accounts: &std::collections::HashMap<
45            solana_program::pubkey::Pubkey,
46            &solana_program::account_info::AccountInfo,
47        >,
48        payload: &crate::payload::Payload,
49        update_rule_state: bool,
50        rule_set_state_pda: &Option<&solana_program::account_info::AccountInfo>,
51        rule_authority: &Option<&solana_program::account_info::AccountInfo>,
52    ) -> RuleResult {
53        msg!("Validating Not");
54
55        let result = self.rule.validate(
56            accounts,
57            payload,
58            update_rule_state,
59            rule_set_state_pda,
60            rule_authority,
61        );
62
63        // Negate the result.
64        match result {
65            RuleResult::Success(err) => RuleResult::Failure(err),
66            RuleResult::Failure(err) => RuleResult::Success(err),
67            RuleResult::Error(err) => RuleResult::Error(err),
68        }
69    }
70}