pub struct SessionStore { /* private fields */ }Expand description
Session storage with NDJSON format.
Stores sessions as NDJSON (Newline Delimited JSON) files where each line is a valid JSON object. This format allows:
- Streaming writes (append commands during recording)
- Human readability (one JSON object per line)
- Crash recovery (partial files are readable up to last complete line)
File structure:
{"type": "header", "version": 2, "id": "...", ...}
{"type": "command", "index": 0, "command": "...", ...}
{"type": "command", "index": 1, "command": "...", ...}
{"type": "footer", "ended_at": ..., "command_count": ..., "status": "..."}Implementations§
Source§impl SessionStore
impl SessionStore
Sourcepub fn save(&self, session: &Session) -> Result<()>
pub fn save(&self, session: &Session) -> Result<()>
Save a complete session to NDJSON file atomically.
Uses a write-to-temp-then-rename pattern for crash safety:
- Write to a temporary file (
{uuid}.ndjson.tmp) - Flush and sync to disk
- Atomically rename to final location (POSIX rename is atomic)
This ensures the session file is never left in a partial state.
If a crash occurs during write, only the .tmp file is affected.
§Errors
Returns an error if:
- Directory creation fails
- File creation fails
- JSON serialization fails
- Atomic rename fails
Sourcepub fn load(&self, id: &str) -> Result<Session>
pub fn load(&self, id: &str) -> Result<Session>
Load a session from NDJSON file.
Reads the NDJSON file line by line and reconstructs the Session struct from the header, commands, and optional footer.
§Errors
Returns an error if:
- Session file doesn’t exist
- File read fails
- JSON parsing fails
- File is missing the header
Sourcepub fn list(&self) -> Result<Vec<String>>
pub fn list(&self) -> Result<Vec<String>>
List all session IDs in the data directory.
Returns the file stems (IDs) of all .ndjson files in the
sessions directory.
§Errors
Returns an error if reading the directory fails.
Sourcepub fn delete(&self, id: &str) -> Result<()>
pub fn delete(&self, id: &str) -> Result<()>
Delete a session by ID.
Removes the session file from disk.
§Errors
Returns an error if the session doesn’t exist or deletion fails.
Load only the header and footer from a session file.
Much more efficient than load() for listing because it skips
command deserialization. Reads the file line by line:
- Parses the first non-empty line as the header
- Tracks the last non-empty line and tries parsing it as a footer
§Errors
Returns an error if:
- Session file doesn’t exist
- File read fails
- Header is missing or invalid
Sourcepub fn rename(&self, id: &str, new_name: &str) -> Result<()>
pub fn rename(&self, id: &str, new_name: &str) -> Result<()>
Rename a session by updating the name in its NDJSON file.
Creates a backup of the original file before modifying. Loads the full session, updates the name, and saves it back.
§Errors
Returns an error if:
- Session doesn’t exist
- Backup creation fails
- File write fails
Add tags to a session, skipping duplicates.
Creates a backup of the original file before modifying. Returns the final list of all tags on the session.
§Errors
Returns an error if:
- Session doesn’t exist
- Backup creation fails
- File write fails
Sourcepub fn session_file_path(&self, id: &str) -> PathBuf
pub fn session_file_path(&self, id: &str) -> PathBuf
Get the file path for a session by ID.