use std::collections::HashMap;
use std::path::Path;
use serde::{Deserialize, Serialize};
pub const SESSION_FILE: &str = ".session.json";
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct SessionState {
#[serde(default)]
pub tree: TreeSession,
#[serde(default)]
pub editor: Option<EditorSession>,
#[serde(default)]
pub focus: String,
#[serde(default)]
pub paragraph_cursors: HashMap<String, ParagraphCursor>,
}
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
pub struct ParagraphCursor {
#[serde(default)]
pub cursor_row: usize,
#[serde(default)]
pub cursor_col: usize,
#[serde(default)]
pub scroll_row: usize,
#[serde(default)]
pub scroll_col: usize,
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct TreeSession {
#[serde(default)]
pub cursor_id: Option<String>,
#[serde(default)]
pub collapsed_nodes: Vec<String>,
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct EditorSession {
pub opened_id: String,
#[serde(default)]
pub cursor_row: usize,
#[serde(default)]
pub cursor_col: usize,
}
impl SessionState {
pub fn load(project_root: &Path) -> Option<Self> {
let path = project_root.join(SESSION_FILE);
let raw = std::fs::read_to_string(&path).ok()?;
serde_json::from_str(&raw).ok()
}
pub fn save(&self, project_root: &Path) -> std::io::Result<()> {
let path = project_root.join(SESSION_FILE);
let json = serde_json::to_string_pretty(self)?;
std::fs::write(&path, json)
}
}