Skip to main content

edgechain_memory/
store.rs

1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use chrono::{DateTime, Utc};
4use crate::error::MemoryError;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct MemoryEntry {
8    pub id: String,
9    pub key: String,
10    pub value: serde_json::Value,
11    pub tags: Vec<String>,
12    pub created_at: DateTime<Utc>,
13    pub updated_at: DateTime<Utc>,
14}
15
16impl MemoryEntry {
17    pub fn new(key: impl Into<String>, value: serde_json::Value) -> Self {
18        let now = Utc::now();
19        Self {
20            id: uuid::Uuid::new_v4().to_string(),
21            key: key.into(),
22            value,
23            tags: vec![],
24            created_at: now,
25            updated_at: now,
26        }
27    }
28
29    pub fn with_tags(mut self, tags: Vec<impl Into<String>>) -> Self {
30        self.tags = tags.into_iter().map(|t| t.into()).collect();
31        self
32    }
33}
34
35#[async_trait]
36pub trait MemoryStore: Send + Sync {
37    async fn set(&self, key: &str, value: serde_json::Value) -> Result<(), MemoryError>;
38
39    async fn get(&self, key: &str) -> Result<Option<MemoryEntry>, MemoryError>;
40
41    async fn delete(&self, key: &str) -> Result<(), MemoryError>;
42
43    async fn list_keys(&self, prefix: Option<&str>) -> Result<Vec<String>, MemoryError>;
44
45    async fn search_by_tag(&self, tag: &str) -> Result<Vec<MemoryEntry>, MemoryError>;
46
47    async fn set_tagged(
48        &self,
49        key: &str,
50        value: serde_json::Value,
51        tags: Vec<String>,
52    ) -> Result<(), MemoryError>;
53}