pub struct State { /* private fields */ }Implementations§
Source§impl State
impl State
Sourcepub fn new(manifest_dir: &str) -> Self
pub fn new(manifest_dir: &str) -> Self
Creates a new State with the given manifest directory.
§Examples
use state_engine::State;
let state = State::new("./examples/manifest");pub fn with_in_memory(self, client: Arc<dyn InMemoryClient>) -> Self
pub fn with_kvs(self, client: Arc<dyn KVSClient>) -> Self
pub fn with_db(self, client: Arc<dyn DbClient>) -> Self
pub fn with_env(self, client: Arc<dyn EnvClient>) -> Self
pub fn with_http(self, client: Arc<dyn HttpClient>) -> Self
pub fn with_file(self, client: Arc<dyn FileClient>) -> Self
pub fn with_manifest_file(self, client: impl FileClient + 'static) -> Self
Sourcepub fn get(&mut self, key: &str) -> Result<Option<Value>, StateError>
pub fn get(&mut self, key: &str) -> Result<Option<Value>, StateError>
Returns the value for key, checking state cache → _store → _load in order.
§Examples
use state_engine::State;
use state_engine::InMemoryClient;
use serde_json::{json, Value};
struct MockInMemory { data: std::sync::Mutex<std::collections::HashMap<String, Value>> }
impl MockInMemory { fn new() -> Self { Self { data: Default::default() } } }
impl InMemoryClient for MockInMemory {
fn get(&self, key: &str) -> Option<Value> { self.data.lock().unwrap().get(key).cloned() }
fn set(&self, key: &str, value: Value) -> bool { self.data.lock().unwrap().insert(key.to_string(), value); true }
fn delete(&self, key: &str) -> bool { self.data.lock().unwrap().remove(key).is_some() }
}
let client = MockInMemory::new();
let mut state = State::new("./examples/manifest")
.with_in_memory(std::sync::Arc::new(client));
// set then get
state.set("connection.common", json!({"host": "localhost"}), None).unwrap();
assert!(state.get("connection.common").unwrap().is_some());Sourcepub fn set(
&mut self,
key: &str,
value: Value,
ttl: Option<u64>,
) -> Result<bool, StateError>
pub fn set( &mut self, key: &str, value: Value, ttl: Option<u64>, ) -> Result<bool, StateError>
Writes value to the _store backend for key.
§Examples
let client = MockInMemory::new();
let mut state = State::new("./examples/manifest")
.with_in_memory(std::sync::Arc::new(client));
assert!(state.set("connection.common", json!({"host": "localhost"}), None).unwrap());Sourcepub fn delete(&mut self, key: &str) -> Result<bool, StateError>
pub fn delete(&mut self, key: &str) -> Result<bool, StateError>
Removes the value for key from the _store backend.
§Examples
let client = MockInMemory::new();
let mut state = State::new("./examples/manifest")
.with_in_memory(std::sync::Arc::new(client));
state.set("connection.common", json!({"host": "localhost"}), None).unwrap();
assert!(state.delete("connection.common").unwrap());
// after delete, store has no data; _load is attempted but EnvClient is not configured here
assert!(state.get("connection.common").is_err() || state.get("connection.common").unwrap().is_none());Sourcepub fn exists(&mut self, key: &str) -> Result<bool, StateError>
pub fn exists(&mut self, key: &str) -> Result<bool, StateError>
Returns true if a value exists for key in state cache or _store.
Does not trigger _load.
§Examples
let client = MockInMemory::new();
let mut state = State::new("./examples/manifest")
.with_in_memory(std::sync::Arc::new(client));
assert!(!state.exists("connection.common").unwrap());
state.set("connection.common", json!({"host": "localhost"}), None).unwrap();
assert!(state.exists("connection.common").unwrap());Auto Trait Implementations§
impl Freeze for State
impl !RefUnwindSafe for State
impl Send for State
impl Sync for State
impl Unpin for State
impl UnsafeUnpin for State
impl !UnwindSafe for State
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more