systemprompt_cloud/cli_session/
session_file.rs1use std::fs;
7use std::path::Path;
8
9use super::session::{CURRENT_VERSION, CliSession, MIN_SUPPORTED_VERSION};
10use crate::error::{CloudError, CloudResult};
11use crate::private_dir::write_private_json;
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 =
22 serde_json::from_str(&content).map_err(|e| CloudError::CredentialsCorrupted {
23 source: Box::new(e),
24 })?;
25
26 if session.version < MIN_SUPPORTED_VERSION || session.version > CURRENT_VERSION {
27 return Err(CloudError::SessionVersionMismatch {
28 min: MIN_SUPPORTED_VERSION,
29 max: CURRENT_VERSION,
30 actual: session.version,
31 path: path.display().to_string(),
32 });
33 }
34
35 session.version = CURRENT_VERSION;
36 Ok(session)
37 }
38
39 pub fn save_to_path(&self, path: &Path) -> CloudResult<()> {
40 write_private_json(path, self)
41 }
42
43 pub fn delete_from_path(path: &Path) -> CloudResult<()> {
44 if path.exists() {
45 fs::remove_file(path)?;
46 }
47 Ok(())
48 }
49}