gmsol_store/states/
feature.rs1use anchor_lang::prelude::*;
2use gmsol_utils::config::ConfigError;
3
4use crate::CoreError;
5
6pub use gmsol_utils::config::{display_feature, ActionDisabledFlag, DomainDisabledFlag};
7
8type DisabledKey = (DomainDisabledFlag, ActionDisabledFlag);
9
10const MAX_DISABLED_FEATURES: usize = 64;
11const DISABLED: u8 = u8::MAX;
12
13#[zero_copy]
15#[cfg_attr(feature = "debug", derive(Debug))]
16pub struct DisabledFeatures {
17 map: DisabledMap,
18}
19
20impl DisabledFeatures {
21 pub(crate) fn get_disabled(
22 &self,
23 domain: DomainDisabledFlag,
24 action: ActionDisabledFlag,
25 ) -> Option<bool> {
26 self.map
27 .get(&(domain, action))
28 .map(|value| *value == DISABLED)
29 }
30
31 pub(crate) fn set_disabled(
32 &mut self,
33 domain: DomainDisabledFlag,
34 action: ActionDisabledFlag,
35 disabled: bool,
36 ) {
37 let value = if disabled { DISABLED } else { 0 };
38 self.map.insert(&(domain, action), value);
39 }
40}
41
42fn to_key(key: &DisabledKey) -> [u8; 2] {
43 [key.0 as u8, key.1 as u8]
44}
45
46gmsol_utils::fixed_map!(
47 DisabledMap,
48 2,
49 DisabledKey,
50 to_key,
51 u8,
52 MAX_DISABLED_FEATURES,
53 0
54);
55
56impl From<ConfigError> for CoreError {
57 fn from(err: ConfigError) -> Self {
58 msg!("Config error: {}", err);
59 match err {
60 ConfigError::UnsupportedDomain => Self::Unimplemented,
61 }
62 }
63}