Skip to main content

state_engine/ports/
provided.rs

1use serde_json::Value;
2use std::collections::HashMap;
3
4#[derive(Debug, PartialEq)]
5pub enum ManifestError {
6    FileNotFound(String),
7    AmbiguousFile(String),
8    ReadError(String),
9    ParseError(String),
10}
11
12impl std::fmt::Display for ManifestError {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        match self {
15            ManifestError::FileNotFound(msg)  => write!(f, "FileNotFound: {}", msg),
16            ManifestError::AmbiguousFile(msg) => write!(f, "AmbiguousFile: {}", msg),
17            ManifestError::ReadError(msg)     => write!(f, "ReadError: {}", msg),
18            ManifestError::ParseError(msg)    => write!(f, "ParseError: {}", msg),
19        }
20    }
21}
22
23#[derive(Debug, PartialEq)]
24pub enum StateError {
25    ManifestLoadFailed(String),
26    KeyNotFound(String),
27    RecursionLimitExceeded,
28    StoreFailed(String),
29    LoadFailed(String),
30}
31
32impl std::fmt::Display for StateError {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match self {
35            StateError::ManifestLoadFailed(msg)  => write!(f, "ManifestLoadFailed: {}", msg),
36            StateError::KeyNotFound(msg)          => write!(f, "KeyNotFound: {}", msg),
37            StateError::RecursionLimitExceeded    => write!(f, "RecursionLimitExceeded"),
38            StateError::StoreFailed(msg)          => write!(f, "StoreFailed: {}", msg),
39            StateError::LoadFailed(msg)           => write!(f, "LoadFailed: {}", msg),
40        }
41    }
42}
43
44pub trait Manifest {
45    fn get_value(&mut self, key: &str, default: Option<Value>) -> Value;
46    fn get_meta(&mut self, key: &str) -> HashMap<String, Value>;
47    fn load_file(&mut self, file: &str) -> Result<(), ManifestError>;
48}
49
50/// The primary interface for state-engine. Manages state per manifest definition.
51pub trait State {
52    /// Returns value from _store, or triggers _load on miss.
53    fn get(&mut self, key: &str) -> Result<Option<Value>, StateError>;
54
55    /// Writes value to _store. Returns Ok(false) if no _store is configured.
56    /// `ttl` overrides manifest definition (KVS only).
57    fn set(&mut self, key: &str, value: Value, ttl: Option<u64>) -> Result<bool, StateError>;
58
59    /// Removes value from _store.
60    fn delete(&mut self, key: &str) -> Result<bool, StateError>;
61
62    /// Checks existence in cache or _store. Does not trigger _load.
63    fn exists(&mut self, key: &str) -> Result<bool, StateError>;
64}