systemprompt_cloud/cli_session/
session_file.rs1use std::fs;
7use std::path::Path;
8
9use super::private_file::{ensure_private_dir, write_private_atomic};
10use super::session::{CURRENT_VERSION, CliSession, MIN_SUPPORTED_VERSION};
11use crate::error::{CloudError, CloudResult};
12
13impl CliSession {
14 pub fn load_from_path(path: &Path) -> CloudResult<Self> {
15 if !path.exists() {
16 return Err(CloudError::NotAuthenticated);
17 }
18
19 let content = fs::read_to_string(path)?;
20
21 let mut session: Self = serde_json::from_str(&content)
22 .map_err(|e| CloudError::CredentialsCorrupted { source: e })?;
23
24 if session.version < MIN_SUPPORTED_VERSION || session.version > CURRENT_VERSION {
25 return Err(CloudError::SessionVersionMismatch {
26 min: MIN_SUPPORTED_VERSION,
27 max: CURRENT_VERSION,
28 actual: session.version,
29 path: path.display().to_string(),
30 });
31 }
32
33 session.version = CURRENT_VERSION;
34 Ok(session)
35 }
36
37 pub fn save_to_path(&self, path: &Path) -> CloudResult<()> {
38 if let Some(dir) = path.parent() {
39 ensure_private_dir(dir)?;
40 }
41 let content = serde_json::to_string_pretty(self)?;
42 write_private_atomic(path, &content)
43 }
44
45 pub fn delete_from_path(path: &Path) -> CloudResult<()> {
46 if path.exists() {
47 fs::remove_file(path)?;
48 }
49 Ok(())
50 }
51}