File size: 718 Bytes
224e773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::collections::BTreeMap;
use sha2::{Sha256, Digest};

#[derive(Debug, Clone)]
pub struct WorldState {
    pub facts: BTreeMap<String, String>,
}

impl WorldState {
    pub fn new() -> Self { Self { facts: BTreeMap::new() } }
    pub fn get(&self, key: &str) -> Option<String> { self.facts.get(key).cloned() }
    pub fn set(&mut self, key: String, value: String) { self.facts.insert(key, value); }
    
    pub fn hash(&self) -> hyperkitty_core::Hash {
        let json = format!("{:?}", self.facts);
        let mut h = Sha256::new();
        h.update(json.as_bytes());
        hyperkitty_core::Hash::new(h.finalize().to_vec())
    }
}

impl Default for WorldState {
    fn default() -> Self { Self::new() }
}