use std::{ fs::File, io::{BufRead, BufReader}, path::PathBuf, }; use directories::ProjectDirs; use crate::Session; use super::SessionSource; #[derive(Debug)] pub struct History(PathBuf); impl History { pub fn new(history_file: PathBuf) -> Self { Self(history_file) } pub fn default_path() -> Option { ProjectDirs::from("", "", env!("CARGO_CRATE_NAME"))? .state_dir()? .join("history") .into() } } impl SessionSource for History { type Error = std::io::Error; type Iter = Vec; fn sessions(&self) -> Result { let mut sessions = Vec::new(); let file = File::open(&self.0)?; for res in BufReader::new(file).lines() { let entry = match res { Ok(entry) => entry, Err(err) => { tracing::warn!(?err, "Failed to read line from history file"); continue; } }; match ron::from_str(&entry) { Ok(session) => sessions.push(session), Err(err) => tracing::warn!(?err, "Invalid history entry"), } } Ok(sessions) } }