atrium_cli/
store.rs

1use async_trait::async_trait;
2use atrium_api::agent::{store::SessionStore, Session};
3use std::path::{Path, PathBuf};
4use tokio::fs::{remove_file, File};
5use tokio::io::{AsyncReadExt, AsyncWriteExt};
6
7pub struct SimpleJsonFileSessionStore<T = PathBuf>
8where
9    T: AsRef<Path>,
10{
11    path: T,
12}
13
14impl<T> SimpleJsonFileSessionStore<T>
15where
16    T: AsRef<Path>,
17{
18    pub fn new(path: T) -> Self {
19        Self { path }
20    }
21}
22
23#[async_trait]
24impl<T> SessionStore for SimpleJsonFileSessionStore<T>
25where
26    T: AsRef<Path> + Send + Sync + 'static,
27{
28    async fn get_session(&self) -> Option<Session> {
29        let mut file = File::open(self.path.as_ref()).await.ok()?;
30        let mut buffer = Vec::new();
31        file.read_to_end(&mut buffer).await.ok()?;
32        serde_json::from_slice(&buffer).ok()
33    }
34    async fn set_session(&self, session: Session) {
35        let mut file = File::create(self.path.as_ref()).await.unwrap();
36        let buffer = serde_json::to_vec_pretty(&session).ok().unwrap();
37        file.write_all(&buffer).await.ok();
38    }
39    async fn clear_session(&self) {
40        remove_file(self.path.as_ref()).await.ok();
41    }
42}