Skip to main content

state_engine/ports/
provided.rs

1use serde_json::Value;
2
3#[derive(Debug, PartialEq)]
4pub enum ManifestError {
5    FileNotFound(String),
6    AmbiguousFile(String),
7    ParseError(String),
8}
9
10impl std::fmt::Display for ManifestError {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        match self {
13            ManifestError::FileNotFound(msg)  => write!(f, "FileNotFound: {}", msg),
14            ManifestError::AmbiguousFile(msg) => write!(f, "AmbiguousFile: {}", msg),
15            ManifestError::ParseError(msg)    => write!(f, "ParseError: {}", msg),
16        }
17    }
18}
19
20#[derive(Debug, PartialEq)]
21pub enum LoadError {
22    /// Required client (Env/KVS/DB/HTTP/File) is not configured.
23    ClientNotConfigured,
24    /// A required config key (key/url/table/map/connection) is missing.
25    ConfigMissing(String),
26    /// The client call succeeded but returned no data.
27    NotFound(String),
28    /// JSON parse error from client response.
29    ParseError(String),
30}
31
32impl std::fmt::Display for LoadError {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match self {
35            LoadError::ClientNotConfigured      => write!(f, "ClientNotConfigured"),
36            LoadError::ConfigMissing(msg)       => write!(f, "ConfigMissing: {}", msg),
37            LoadError::NotFound(msg)            => write!(f, "NotFound: {}", msg),
38            LoadError::ParseError(msg)          => write!(f, "ParseError: {}", msg),
39        }
40    }
41}
42
43#[derive(Debug, PartialEq)]
44pub enum StoreError {
45    /// Required client (KVS/InMemory/HTTP/File) is not configured.
46    ClientNotConfigured,
47    /// A required config key (key/url/client) is missing.
48    ConfigMissing(String),
49    /// JSON serialize error.
50    SerializeError(String),
51    /// Unsupported client id in config.
52    UnsupportedClient(u64),
53}
54
55impl std::fmt::Display for StoreError {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        match self {
58            StoreError::ClientNotConfigured    => write!(f, "ClientNotConfigured"),
59            StoreError::ConfigMissing(msg)     => write!(f, "ConfigMissing: {}", msg),
60            StoreError::SerializeError(msg)    => write!(f, "SerializeError: {}", msg),
61            StoreError::UnsupportedClient(id)  => write!(f, "UnsupportedClient: {}", id),
62        }
63    }
64}
65
66#[derive(Debug, PartialEq)]
67pub enum StateError {
68    ManifestLoadFailed(String),
69    KeyNotFound(String),
70    RecursionLimitExceeded,
71    StoreFailed(StoreError),
72    LoadFailed(LoadError),
73}
74
75impl std::fmt::Display for StateError {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        match self {
78            StateError::ManifestLoadFailed(msg)  => write!(f, "ManifestLoadFailed: {}", msg),
79            StateError::KeyNotFound(msg)          => write!(f, "KeyNotFound: {}", msg),
80            StateError::RecursionLimitExceeded    => write!(f, "RecursionLimitExceeded"),
81            StateError::StoreFailed(e)            => write!(f, "StoreFailed: {}", e),
82            StateError::LoadFailed(e)             => write!(f, "LoadFailed: {}", e),
83        }
84    }
85}
86
87/// The primary interface for state-engine. Manages state per manifest definition.
88pub trait State {
89    /// Returns value from _store, or triggers _load on miss.
90    fn get(&mut self, key: &str) -> Result<Option<Value>, StateError>;
91
92    /// Writes value to _store. Returns Ok(false) if no _store is configured.
93    /// `ttl` overrides manifest definition (KVS only).
94    fn set(&mut self, key: &str, value: Value, ttl: Option<u64>) -> Result<bool, StateError>;
95
96    /// Removes value from _store.
97    fn delete(&mut self, key: &str) -> Result<bool, StateError>;
98
99    /// Checks existence in cache or _store. Does not trigger _load.
100    fn exists(&mut self, key: &str) -> Result<bool, StateError>;
101}