Skip to main content

rusticity_term/session/
core.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct Session {
7    pub id: String,
8    pub timestamp: String,
9    pub profile: String,
10    pub region: String,
11    pub account_id: String,
12    pub role_arn: String,
13    pub tabs: Vec<SessionTab>,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct SessionTab {
18    pub service: String,
19    pub title: String,
20    pub breadcrumb: String,
21    pub filter: Option<String>,
22    pub selected_item: Option<String>,
23}
24
25impl Session {
26    pub fn new(profile: String, region: String, account_id: String, role_arn: String) -> Self {
27        let timestamp = chrono::Utc::now()
28            .format("%Y-%m-%d %H:%M:%S UTC")
29            .to_string();
30        let id = chrono::Utc::now().format("%Y%m%d_%H%M%S").to_string();
31
32        Self {
33            id,
34            timestamp,
35            profile,
36            region,
37            account_id,
38            role_arn,
39            tabs: Vec::new(),
40        }
41    }
42
43    pub fn save(&self) -> Result<()> {
44        let session_dir = Self::session_dir()?;
45        std::fs::create_dir_all(&session_dir)?;
46
47        let file_path = session_dir.join(format!("{}.json", self.id));
48        let json = serde_json::to_string_pretty(self)?;
49        std::fs::write(file_path, json)?;
50
51        Ok(())
52    }
53
54    pub fn delete(&self) -> Result<()> {
55        let session_dir = Self::session_dir()?;
56        let file_path = session_dir.join(format!("{}.json", self.id));
57        if file_path.exists() {
58            std::fs::remove_file(file_path)?;
59        }
60        Ok(())
61    }
62
63    pub fn load(id: &str) -> Result<Self> {
64        let session_dir = Self::session_dir()?;
65        let file_path = session_dir.join(format!("{}.json", id));
66        let json = std::fs::read_to_string(file_path)?;
67        let session = serde_json::from_str(&json)?;
68        Ok(session)
69    }
70
71    pub fn list_all() -> Result<Vec<Session>> {
72        let session_dir = Self::session_dir()?;
73        if !session_dir.exists() {
74            return Ok(Vec::new());
75        }
76
77        let mut sessions = Vec::new();
78        for entry in std::fs::read_dir(session_dir)? {
79            let entry = entry?;
80            let path = entry.path();
81            if path.extension().and_then(|s| s.to_str()) == Some("json") {
82                if let Ok(json) = std::fs::read_to_string(&path) {
83                    if let Ok(session) = serde_json::from_str::<Session>(&json) {
84                        sessions.push(session);
85                    }
86                }
87            }
88        }
89
90        // Sort by timestamp descending (newest first)
91        sessions.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));
92        Ok(sessions)
93    }
94
95    fn session_dir() -> Result<PathBuf> {
96        let home =
97            dirs::home_dir().ok_or_else(|| anyhow::anyhow!("Could not find home directory"))?;
98        Ok(home.join(".rusticity").join("sessions"))
99    }
100}