Skip to main content

State

Struct State 

Source
pub struct State { /* private fields */ }

Implementations§

Source§

impl State

Source

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");
Source

pub fn with_in_memory(self, client: Arc<dyn InMemoryClient>) -> Self

Source

pub fn with_kvs(self, client: Arc<dyn KVSClient>) -> Self

Source

pub fn with_db(self, client: Arc<dyn DbClient>) -> Self

Source

pub fn with_env(self, client: Arc<dyn EnvClient>) -> Self

Source

pub fn with_http(self, client: Arc<dyn HttpClient>) -> Self

Source

pub fn with_file(self, client: Arc<dyn FileClient>) -> Self

Source

pub fn with_manifest_file(self, client: impl FileClient + 'static) -> Self

Source

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());
Source

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());
Source

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());
Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.