Skip to main content

ass_editor/sessions/
lifecycle.rs

1//! Session lifecycle operations for [`EditorSessionManager`].
2//!
3//! Implements creation, switching, removal, and enumeration of sessions,
4//! tracking the active session and enforcing configured session limits.
5
6use super::config::SessionConfig;
7use super::manager::EditorSessionManager;
8use super::session::EditorSession;
9use crate::core::{EditorDocument, EditorError, Result};
10
11#[cfg(not(feature = "std"))]
12use alloc::{
13    string::{String, ToString},
14    vec::Vec,
15};
16
17impl EditorSessionManager {
18    /// Create a new session manager
19    pub fn new() -> Self {
20        Self::with_config(SessionConfig::default())
21    }
22
23    /// Create a new session with an empty document
24    pub fn create_session(&mut self, session_id: String) -> Result<()> {
25        self.create_session_with_document(session_id, EditorDocument::new())
26    }
27
28    /// Create a new session with a specific document
29    pub fn create_session_with_document(
30        &mut self,
31        session_id: String,
32        document: EditorDocument,
33    ) -> Result<()> {
34        self.with_inner_mut(|inner| {
35            // Check session limits
36            if inner.sessions.len() >= inner.config.max_sessions {
37                return Err(EditorError::SessionLimitExceeded {
38                    current: inner.sessions.len(),
39                    limit: inner.config.max_sessions,
40                });
41            }
42
43            // Create new session
44            let session = EditorSession::new(session_id.clone(), document);
45
46            // Add to sessions map
47            inner.sessions.insert(session_id.clone(), session);
48
49            // Update stats
50            inner.stats.active_sessions += 1;
51
52            // Set as active if it's the first session
53            if inner.active_session_id.is_none() {
54                inner.active_session_id = Some(session_id);
55            }
56
57            Ok(())
58        })
59    }
60
61    /// Switch to a different session
62    pub fn switch_session(&mut self, session_id: &str) -> Result<()> {
63        self.with_inner_mut(|inner| {
64            // Check if session exists
65            if !inner.sessions.contains_key(session_id) {
66                return Err(EditorError::DocumentNotFound {
67                    id: session_id.to_string(),
68                });
69            }
70
71            // Switch active session
72            inner.active_session_id = Some(session_id.to_string());
73
74            // Touch the session to update access time
75            #[cfg(feature = "std")]
76            if let Some(session) = inner.sessions.get_mut(session_id) {
77                session.touch();
78            }
79
80            Ok(())
81        })
82    }
83
84    /// Get the currently active session
85    pub fn active_session(&self) -> Result<Option<String>> {
86        Ok(self.with_inner(|inner| inner.active_session_id.clone()))
87    }
88
89    /// Remove a session
90    pub fn remove_session(&mut self, session_id: &str) -> Result<EditorSession> {
91        self.with_inner_mut(|inner| {
92            let session =
93                inner
94                    .sessions
95                    .remove(session_id)
96                    .ok_or_else(|| EditorError::DocumentNotFound {
97                        id: session_id.to_string(),
98                    })?;
99
100            // Update stats
101            inner.stats.active_sessions -= 1;
102            inner.stats.total_memory_usage -= session.memory_usage;
103
104            // Clear active session if it was removed
105            if inner.active_session_id.as_ref() == Some(&session_id.to_string()) {
106                inner.active_session_id = None;
107            }
108
109            Ok(session)
110        })
111    }
112
113    /// List all session IDs
114    pub fn list_sessions(&self) -> Result<Vec<String>> {
115        Ok(self.with_inner(|inner| inner.sessions.keys().cloned().collect()))
116    }
117}
118
119impl Default for EditorSessionManager {
120    fn default() -> Self {
121        Self::new()
122    }
123}