shank_macro_impl/instruction/
strategy_attrs.rs

1use std::collections::HashSet;
2
3use syn::Attribute;
4
5const LEGACY_OPTIONAL_ACCOUNTS_STRATEGY: &str =
6    "legacy_optional_accounts_strategy";
7
8#[derive(Debug, PartialEq, Eq, Hash)]
9pub enum InstructionStrategy {
10    LegacyOptionalAccounts,
11}
12
13#[derive(Debug, PartialEq, Eq)]
14pub struct InstructionStrategies(pub HashSet<InstructionStrategy>);
15
16impl InstructionStrategy {
17    pub fn from_account_attr(attr: &Attribute) -> Option<InstructionStrategy> {
18        match attr.path.get_ident().map(|x| {
19            x.to_string().as_str() == LEGACY_OPTIONAL_ACCOUNTS_STRATEGY
20        }) {
21            Some(true) => Some(InstructionStrategy::LegacyOptionalAccounts),
22            _ => None,
23        }
24    }
25}
26
27impl From<&[Attribute]> for InstructionStrategies {
28    fn from(attrs: &[Attribute]) -> Self {
29        let strategies = attrs
30            .iter()
31            .filter_map(InstructionStrategy::from_account_attr)
32            .collect::<HashSet<InstructionStrategy>>();
33
34        InstructionStrategies(strategies)
35    }
36}