1pub mod engine;
2pub mod sdk;
3pub mod server;
4
5use serde_json;
6use thiserror::Error;
7use async_trait::async_trait;
8use std::collections::HashMap;
9
10#[derive(Error, Debug)]
11pub enum Error {
12 #[error("persona not found")]
13 PersonaNotFound,
14 #[error("app not found")]
15 AppNotFound,
16 #[error("key not found")]
17 KeyNotFound,
18 #[error("internal error: {0}")]
19 Internal(String),
20 #[error("IO error: {0}")]
21 Io(#[from] std::io::Error),
22 #[error("Serialization error: {0}")]
23 Serialization(#[from] serde_json::Error),
24}
25
26pub type Result<T> = std::result::Result<T, Error>;
27
28pub const SYSTEM_PERSONA: &str = "_system";
29
30#[async_trait]
31pub trait KVReader: Send + Sync {
32 async fn get(&self, persona_id: &str, app_id: &str, key: &str) -> Result<serde_json::Value>;
33}
34
35#[async_trait]
36pub trait KVWriter: Send + Sync {
37 async fn set(&self, persona_id: &str, app_id: &str, key: &str, value: serde_json::Value) -> Result<()>;
38 async fn delete(&self, persona_id: &str, app_id: &str, key: &str) -> Result<()>;
39}
40
41#[async_trait]
42pub trait AppEnumeration: Send + Sync {
43 async fn get_personas(&self) -> Result<Vec<String>>;
44 async fn get_apps(&self, persona_id: &str) -> Result<Vec<String>>;
45}
46
47#[async_trait]
48pub trait BatchExporter: Send + Sync {
49 async fn get_app_store(&self, persona_id: &str, app_id: &str) -> Result<HashMap<String, serde_json::Value>>;
50 async fn dump_app(&self, app_id: &str) -> Result<HashMap<String, HashMap<String, serde_json::Value>>>;
51}
52
53#[async_trait]
54pub trait GlobalSearcher: Send + Sync {
55 async fn get_global(&self, app_id: &str, key: &str) -> Result<(serde_json::Value, String)>;
56}
57
58#[async_trait]
59pub trait Orchestrator: Send + Sync {
60 async fn move_key(&self, src_persona: &str, dst_persona: &str, app_id: &str, key: &str) -> Result<()>;
61}
62
63#[async_trait]
64pub trait CelerixStore: KVReader + KVWriter + AppEnumeration + BatchExporter + GlobalSearcher + Orchestrator {
65 fn app(&self, persona_id: &str, app_id: &str) -> Box<dyn AppScope + '_>;
66}
67
68#[async_trait]
69pub trait AppScope: Send + Sync {
70 async fn get(&self, key: &str) -> Result<serde_json::Value>;
71 async fn set(&self, key: &str, value: serde_json::Value) -> Result<()>;
72 async fn delete(&self, key: &str) -> Result<()>;
73 fn vault(&self, master_key: &[u8]) -> Box<dyn VaultScope + '_>;
74}
75
76#[async_trait]
77pub trait VaultScope: Send + Sync {
78 async fn get(&self, key: &str) -> Result<String>;
79 async fn set(&self, key: &str, plaintext: &str) -> Result<()>;
80}