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
use crate::{
error::RuleSetError,
state::{Key, Rule},
};
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
#[cfg(feature = "serde-with-feature")]
use serde_with::{As, DisplayFromStr};
use safecoin_program::{entrypoint::ProgramResult, pubkey::Pubkey};
use std::collections::HashMap;
pub const RULE_SET_REV_MAP_VERSION: u8 = 1;
pub const RULE_SET_LIB_VERSION: u8 = 1;
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Debug, Clone)]
pub struct RuleSetHeader {
pub key: Key,
pub rev_map_version_location: usize,
}
impl RuleSetHeader {
pub fn new(rev_map_version_location: usize) -> Self {
Self {
key: Key::RuleSet,
rev_map_version_location,
}
}
}
pub const RULE_SET_SERIALIZED_HEADER_LEN: usize = 9;
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Debug, Clone, Default)]
pub struct RuleSetRevisionMapV1 {
pub rule_set_revisions: Vec<usize>,
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct RuleSetV1 {
lib_version: u8,
#[cfg_attr(feature = "serde-with-feature", serde(with = "As::<DisplayFromStr>"))]
owner: Pubkey,
rule_set_name: String,
pub operations: HashMap<String, Rule>,
}
impl RuleSetV1 {
pub fn new(rule_set_name: String, owner: Pubkey) -> Self {
Self {
lib_version: RULE_SET_LIB_VERSION,
rule_set_name,
owner,
operations: HashMap::new(),
}
}
pub fn name(&self) -> &str {
&self.rule_set_name
}
pub fn lib_version(&self) -> u8 {
self.lib_version
}
pub fn owner(&self) -> &Pubkey {
&self.owner
}
pub fn add(&mut self, operation: String, rules: Rule) -> ProgramResult {
if self.operations.get(&operation).is_none() {
self.operations.insert(operation, rules);
Ok(())
} else {
Err(RuleSetError::ValueOccupied.into())
}
}
pub fn get(&self, operation: String) -> Option<&Rule> {
self.operations.get(&operation)
}
}