Skip to main content

ass_editor/sessions/
session.rs

1//! Individual editing session state.
2//!
3//! Defines [`EditorSession`], pairing an [`EditorDocument`] with its
4//! identifier, access bookkeeping, memory accounting, and per-session
5//! metadata.
6
7use crate::core::EditorDocument;
8
9#[cfg(feature = "std")]
10use std::collections::HashMap;
11
12#[cfg(not(feature = "std"))]
13use alloc::collections::BTreeMap as HashMap;
14
15#[cfg(not(feature = "std"))]
16use alloc::string::String;
17
18/// A single editing session containing a document and associated state
19#[derive(Debug)]
20pub struct EditorSession {
21    /// The document being edited
22    pub document: EditorDocument,
23
24    /// Session identifier
25    pub id: String,
26
27    /// Last access timestamp for cleanup purposes
28    #[cfg(feature = "std")]
29    pub last_accessed: std::time::Instant,
30
31    /// Memory usage of this session
32    pub memory_usage: usize,
33
34    /// Number of operations performed in this session
35    pub operation_count: usize,
36
37    /// Session-specific metadata
38    pub metadata: HashMap<String, String>,
39}
40
41impl EditorSession {
42    /// Create a new session with a document
43    pub fn new(id: String, document: EditorDocument) -> Self {
44        Self {
45            id,
46            document,
47            #[cfg(feature = "std")]
48            last_accessed: std::time::Instant::now(),
49            memory_usage: 0,
50            operation_count: 0,
51            metadata: HashMap::new(),
52        }
53    }
54
55    /// Update last accessed time
56    #[cfg(feature = "std")]
57    pub fn touch(&mut self) {
58        self.last_accessed = std::time::Instant::now();
59    }
60
61    /// Check if session is stale (for cleanup)
62    #[cfg(feature = "std")]
63    pub fn is_stale(&self, max_age: std::time::Duration) -> bool {
64        self.last_accessed.elapsed() > max_age
65    }
66
67    /// Get session metadata
68    #[must_use]
69    pub fn get_metadata(&self, key: &str) -> Option<&str> {
70        self.metadata.get(key).map(|s| s.as_str())
71    }
72
73    /// Set session metadata
74    pub fn set_metadata(&mut self, key: String, value: String) {
75        self.metadata.insert(key, value);
76    }
77
78    /// Increment operation counter
79    pub fn increment_operations(&mut self) {
80        self.operation_count += 1;
81    }
82}