mermaid_cli/session/
state.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use std::fs;
4use std::path::PathBuf;
5
6/// Session state that persists between runs
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct SessionState {
9    pub last_used_model: Option<String>,
10    pub last_project_path: Option<String>,
11    pub operation_mode: Option<String>,
12}
13
14impl Default for SessionState {
15    fn default() -> Self {
16        Self {
17            last_used_model: None,
18            last_project_path: None,
19            operation_mode: None,
20        }
21    }
22}
23
24impl SessionState {
25    /// Get the path to the session file
26    fn session_file() -> Result<PathBuf> {
27        let home = std::env::var("HOME")?;
28        let config_dir = PathBuf::from(home).join(".config").join("mermaid");
29        fs::create_dir_all(&config_dir)?;
30        Ok(config_dir.join("session.toml"))
31    }
32
33    /// Load session state from disk
34    pub fn load() -> Result<Self> {
35        let path = Self::session_file()?;
36        if path.exists() {
37            let content = fs::read_to_string(&path)?;
38            Ok(toml::from_str(&content)?)
39        } else {
40            Ok(Self::default())
41        }
42    }
43
44    /// Save session state to disk
45    pub fn save(&self) -> Result<()> {
46        let path = Self::session_file()?;
47        let content = toml::to_string_pretty(&self)?;
48        fs::write(path, content)?;
49        Ok(())
50    }
51
52    /// Update the last used model
53    pub fn set_model(&mut self, model: String) {
54        self.last_used_model = Some(model);
55    }
56
57    /// Get the last used model
58    pub fn get_model(&self) -> Option<&str> {
59        self.last_used_model.as_deref()
60    }
61}