cyfs_lib/root_state/
def.rs

1use cyfs_base::*;
2
3use serde::{Serialize, Deserialize};
4use std::str::FromStr;
5
6#[derive(Debug, Eq, PartialEq)]
7pub enum RootStateAction {
8    GetCurrentRoot,
9    CreateOpEnv,
10}
11
12impl ToString for RootStateAction {
13    fn to_string(&self) -> String {
14        (match *self {
15            Self::GetCurrentRoot => "get-current-root",
16            Self::CreateOpEnv => "create-op-env",
17        })
18        .to_owned()
19    }
20}
21
22impl FromStr for RootStateAction {
23    type Err = BuckyError;
24
25    fn from_str(value: &str) -> Result<Self, Self::Err> {
26        let ret = match value {
27            "get-current-root" => Self::GetCurrentRoot,
28            "create-op-env" => Self::CreateOpEnv,
29
30            v @ _ => {
31                let msg = format!("unknown state action: {}", v);
32                error!("{}", msg);
33
34                return Err(BuckyError::new(BuckyErrorCode::InvalidData, msg));
35            }
36        };
37
38        Ok(ret)
39    }
40}
41
42#[derive(Debug, Eq, PartialEq)]
43pub enum OpEnvAction {
44    // map methods
45    GetByKey,
46    InsertWithKey,
47    SetWithKey,
48    RemoveWithKey,
49
50    // set methods
51    Contains,
52    Insert,
53    Remove,
54
55    // single op_env
56    Load,
57    LoadByPath,
58    CreateNew,
59
60    // transaciton
61    Lock,
62    Commit,
63    Abort,
64
65    // metadata
66    Metadata,
67
68    GetCurrentRoot,
69
70    // iterator
71    Next,
72    Reset,
73    List,
74}
75
76impl ToString for OpEnvAction {
77    fn to_string(&self) -> String {
78        (match *self {
79            Self::GetByKey => "get-by-key",
80            Self::InsertWithKey => "insert-with-key",
81            Self::SetWithKey => "set-with-key",
82            Self::RemoveWithKey => "remove-with-key",
83
84            Self::Contains => "contains",
85            Self::Insert => "insert",
86            Self::Remove => "remove",
87
88            Self::Load => "load",
89            Self::LoadByPath => "load-by-path",
90            Self::CreateNew => "create-new",
91
92            Self::Lock => "lock",
93            Self::Commit => "commit",
94            Self::Abort => "abort",
95
96            Self::Metadata => "metadata",
97
98            Self::GetCurrentRoot => "get-current-root",
99
100            Self::Next => "next",
101            Self::Reset => "reset",
102            Self::List => "list",
103        })
104        .to_owned()
105    }
106}
107
108impl FromStr for OpEnvAction {
109    type Err = BuckyError;
110
111    fn from_str(value: &str) -> Result<Self, Self::Err> {
112        let ret = match value {
113            "get-by-key" => Self::GetByKey,
114            "insert-with-key" => Self::InsertWithKey,
115            "set-with-key" => Self::SetWithKey,
116            "remove-with-key" => Self::RemoveWithKey,
117
118            "contains" => Self::Contains,
119            "insert" => Self::Insert,
120            "remove" => Self::Remove,
121
122            "load" => Self::Load,
123            "load-by-path" => Self::LoadByPath,
124            "create-new" => Self::CreateNew,
125
126            "lock" => Self::Lock,
127            "commit" => Self::Commit,
128            "abort" => Self::Abort,
129
130            "metadata" => Self::Metadata,
131
132            "get-current-root" => Self::GetCurrentRoot,
133
134            "next" => Self::Next,
135            "reset" => Self::Reset,
136            "list" => Self::List,
137
138            v @ _ => {
139                let msg = format!("unknown op_env action: {}", v);
140                error!("{}", msg);
141
142                return Err(BuckyError::new(BuckyErrorCode::InvalidData, msg));
143            }
144        };
145
146        Ok(ret)
147    }
148}
149
150#[derive(Debug, Clone, Eq, PartialEq)]
151pub enum GlobalStateAccessorAction {
152    GetObjectByPath,
153    List,
154}
155
156impl ToString for GlobalStateAccessorAction {
157    fn to_string(&self) -> String {
158        (match *self {
159            Self::GetObjectByPath => "get-object-by-path",
160            Self::List => "list",
161        })
162        .to_owned()
163    }
164}
165
166impl FromStr for GlobalStateAccessorAction {
167    type Err = BuckyError;
168
169    fn from_str(value: &str) -> Result<Self, Self::Err> {
170        let ret = match value {
171            "get-object-by-path" | "get" => Self::GetObjectByPath,
172            "list" => Self::List,
173
174            _ => {
175                // as default action in access mode
176                Self::GetObjectByPath
177            }
178        };
179
180        Ok(ret)
181    }
182}
183
184#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
185pub enum GlobalStateCategory {
186    RootState,
187    LocalCache,
188}
189
190impl GlobalStateCategory {
191    pub fn as_str(&self) -> &str {
192        match &self {
193            Self::RootState => "root-state",
194            Self::LocalCache => "local-cache",
195        }
196    }
197}
198
199impl std::fmt::Display for GlobalStateCategory {
200    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
201        std::fmt::Display::fmt(self.as_str(), f)
202    }
203}
204
205impl FromStr for GlobalStateCategory {
206    type Err = BuckyError;
207
208    fn from_str(s: &str) -> BuckyResult<Self> {
209        match s {
210            "root-state" => Ok(Self::RootState),
211            "local-cache" => Ok(Self::LocalCache),
212            _ => {
213                let msg = format!("unknown GlobalStateCategory value: {}", s);
214                error!("{}", msg);
215
216                Err(BuckyError::new(BuckyErrorCode::InvalidData, msg))
217            }
218        }
219    }
220}
221
222#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize)]
223pub enum GlobalStateAccessMode {
224    Read = 0,
225    Write = 1,
226}
227
228impl GlobalStateAccessMode {
229    pub fn is_writable(&self) -> bool {
230        match *self {
231            GlobalStateAccessMode::Read => false,
232            GlobalStateAccessMode::Write => true,
233        }
234    }
235
236    pub fn as_str(&self) -> &str {
237        match self {
238            Self::Read => "read",
239            Self::Write => "write",
240        }
241    }
242}
243
244impl ToString for GlobalStateAccessMode {
245    fn to_string(&self) -> String {
246        self.as_str().to_owned()
247    }
248}
249
250impl FromStr for GlobalStateAccessMode {
251    type Err = BuckyError;
252
253    fn from_str(s: &str) -> BuckyResult<Self> {
254        match s {
255            "read" => Ok(Self::Read),
256            "write" => Ok(Self::Write),
257            _ => {
258                let msg = format!("unknown GlobalStateAccessMode value: {}", s);
259                error!("{}", msg);
260
261                Err(BuckyError::new(BuckyErrorCode::InvalidData, msg))
262            }
263        }
264    }
265}