elvis_core/
state.rs

1//! State machine
2use crate::Node;
3use std::collections::HashMap;
4
5/// State store map
6pub type StateKV = HashMap<Vec<u8>, Vec<u8>>;
7
8/// state for tree
9pub struct State<W> {
10    /// Elvis Node
11    pub child: W,
12    /// State Machine
13    pub state: StateKV,
14}
15
16impl<W> State<W>
17where
18    W: Into<Node>,
19{
20    /// Get state
21    pub fn get(&self, k: &[u8]) -> Vec<u8> {
22        self.state.get(k).unwrap_or(&vec![]).to_vec()
23    }
24
25    /// Set state
26    pub fn set(&mut self, k: &[u8], v: &[u8]) {
27        self.state.insert(k.to_vec(), v.to_vec());
28    }
29}
30
31impl<W> Into<Node> for State<W>
32where
33    W: Into<Node>,
34{
35    fn into(self) -> Node {
36        let mut n = self.child.into();
37        n.state = Some(self.state);
38        n
39    }
40}