Skip to main content

starweaver_context/
state.rs

1//! Serializable state domains for agent context.
2
3use std::collections::BTreeMap;
4
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8/// In-memory state store for context domains.
9#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
10pub struct StateStore {
11    domains: BTreeMap<String, Value>,
12}
13
14impl StateStore {
15    /// Create an empty state store.
16    #[must_use]
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Set a domain value.
22    pub fn set(&mut self, key: impl Into<String>, value: Value) {
23        self.domains.insert(key.into(), value);
24    }
25
26    /// Get a domain value.
27    #[must_use]
28    pub fn get(&self, key: &str) -> Option<&Value> {
29        self.domains.get(key)
30    }
31
32    /// Remove a domain value.
33    pub fn remove(&mut self, key: &str) -> Option<Value> {
34        self.domains.remove(key)
35    }
36
37    /// Return whether the store has no domains.
38    #[must_use]
39    pub fn is_empty(&self) -> bool {
40        self.domains.is_empty()
41    }
42
43    /// Return all domains.
44    #[must_use]
45    #[allow(clippy::missing_const_for_fn)]
46    pub fn domains(&self) -> &BTreeMap<String, Value> {
47        &self.domains
48    }
49}