50 lines
1.5 KiB
Rust
50 lines
1.5 KiB
Rust
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||
|
|
// src/document/store.rs
|
||
|
|
//
|
||
|
|
// RON-based persistence for session data: load and save session lists.
|
||
|
|
//
|
||
|
|
// For the session concept and the meaning of each field see `session.rs`.
|
||
|
|
//
|
||
|
|
// On disk a session file contains a RON list of SessionData records:
|
||
|
|
//
|
||
|
|
// [
|
||
|
|
// (
|
||
|
|
// name: "Pictures",
|
||
|
|
// kind: DirectoryBrowser,
|
||
|
|
// path: Some("/home/user/Pictures"),
|
||
|
|
// items: [],
|
||
|
|
// current_index: None,
|
||
|
|
// ),
|
||
|
|
// (
|
||
|
|
// name: "Project Docs",
|
||
|
|
// kind: DocumentCollection,
|
||
|
|
// path: None,
|
||
|
|
// items: [
|
||
|
|
// (path: "/home/user/docs/spec.pdf", page_index: 0),
|
||
|
|
// (path: "/home/user/docs/notes.pdf", page_index: 2),
|
||
|
|
// ],
|
||
|
|
// current_index: Some(0),
|
||
|
|
// ),
|
||
|
|
// ]
|
||
|
|
|
||
|
|
use crate::document::session::SessionData;
|
||
|
|
use crate::error::{Error, Result};
|
||
|
|
use std::path::Path;
|
||
|
|
|
||
|
|
/// Loads all sessions from a RON file.
|
||
|
|
pub fn load_sessions(path: &Path) -> Result<Vec<SessionData>> {
|
||
|
|
let content = std::fs::read_to_string(path).map_err(Error::Io)?;
|
||
|
|
|
||
|
|
ron::from_str(&content)
|
||
|
|
.map_err(|e| Error::Session(format!("Failed to parse session file {:?}: {}", path, e)))
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Saves all sessions to a RON file.
|
||
|
|
#[allow(dead_code)]
|
||
|
|
pub fn save_sessions(path: &Path, sessions: &[SessionData]) -> Result<()> {
|
||
|
|
let content = ron::ser::to_string_pretty(sessions, ron::ser::PrettyConfig::default())
|
||
|
|
.map_err(|e| Error::Session(format!("Failed to serialize sessions: {}", e)))?;
|
||
|
|
|
||
|
|
std::fs::write(path, content).map_err(Error::Io)
|
||
|
|
}
|