areev 0.2.0

Rust SDK for the Areev knowledge database — gRPC and HTTP transports
Documentation
//! `Memories` resource — multi-memory CRUD (`client.memories()`).
//!
//! Memories are the top-level container in Areev. Most callers run
//! against a single memory configured at client construction time, but
//! teams managing multiple memories per org reach for this resource.

use serde_json::{json, Value};

use crate::error::Result;
use crate::http::HttpClient;

/// Memory lifecycle CRUD — list, create, get, delete.
///
/// Access via [`crate::Areev::memories`]. All methods return loosely
/// typed `serde_json::Value` payloads — the server contract is stable,
/// but Rust strong-typing for every endpoint is deferred to a future
/// generated layer.
pub struct Memories<'a> {
    http: &'a HttpClient,
}

impl<'a> Memories<'a> {
    /// Internal constructor — use [`crate::Areev::memories`].
    pub(crate) fn new(http: &'a HttpClient) -> Self {
        Self { http }
    }

    /// List all memories the caller can access.
    ///
    /// Returns the raw `memories` array from the server when the
    /// response is wrapped, or the top-level array otherwise.
    pub async fn list(&self) -> Result<Vec<Value>> {
        let resp = self.http._get("/memories", None).await?;
        match resp {
            Value::Object(mut map) => Ok(map
                .remove("memories")
                .and_then(|v| match v {
                    Value::Array(a) => Some(a),
                    _ => None,
                })
                .unwrap_or_default()),
            Value::Array(a) => Ok(a),
            _ => Ok(Vec::new()),
        }
    }

    /// Create a new memory. Returns the new memory's metadata.
    pub async fn create(&self, name: &str, description: &str) -> Result<Value> {
        let body = json!({ "name": name, "description": description });
        self.http._post("/memories", Some(&body)).await
    }

    /// Get a single memory's metadata by id.
    pub async fn get(&self, memory_id: &str) -> Result<Value> {
        let path = format!("/memories/{memory_id}");
        self.http._get(&path, None).await
    }

    /// Delete a memory and all its data — irreversible, emits an audit
    /// event server-side.
    pub async fn delete(&self, memory_id: &str) -> Result<()> {
        let path = format!("/memories/{memory_id}");
        self.http._delete(&path).await.map(|_| ())
    }

    /// Grain counts, partition + size statistics for a memory.
    pub async fn stats(&self, memory_id: &str) -> Result<Value> {
        let path = format!("/memories/{memory_id}/stats");
        self.http._get(&path, None).await
    }

    /// Time-series of a memory's stats (grain growth over time).
    /// `filters` is added as query parameters.
    pub async fn stats_history(&self, memory_id: &str, filters: Option<&Value>) -> Result<Value> {
        let path = format!("/memories/{memory_id}/stats/history");
        self.http._get(&path, filters).await
    }
}