use serde::{Deserialize, Serialize};
use std::io;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionRecord {
pub id: String,
pub agent: String,
pub cwd: PathBuf,
pub name: Option<String>,
pub created_at: u64,
pub closed: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub acp_session_id: Option<String>,
}
impl SessionRecord {
pub fn save(&self, path: &Path) -> io::Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let json = serde_json::to_string_pretty(self)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
std::fs::write(path, json)
}
pub fn update_acp_session_id(&mut self, new_id: String, path: &Path) -> io::Result<()> {
self.acp_session_id = Some(new_id);
self.save(path)
}
pub fn load(path: &Path) -> io::Result<Option<Self>> {
match std::fs::read_to_string(path) {
Ok(contents) => {
let record: Self = serde_json::from_str(&contents)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
Ok(Some(record))
}
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e),
}
}
}