use std::{ fs::File, io::{BufRead, BufReader, Write}, path::PathBuf, }; use directories::ProjectDirs; use crate::Session; pub use error::Error; mod error; #[derive(Debug)] pub struct History { pub file: File, pub entries: Vec, } impl History { pub fn open(history_file: PathBuf) -> Result { let file = File::options().write(true).open(&history_file)?; let entries = BufReader::new(File::open(history_file)?) .lines() .flatten() .flat_map(|item| ron::from_str(&item)) .collect(); Ok(Self { file, entries }) } pub fn default_path() -> Option { ProjectDirs::from("", "", env!("CARGO_CRATE_NAME"))? .state_dir()? .join("history") .into() } } impl IntoIterator for History { type Item = Session; type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { self.entries.into_iter() } } impl Write for History { fn write(&mut self, buf: &[u8]) -> std::io::Result { self.file.write(buf) } fn flush(&mut self) -> std::io::Result<()> { self.file.flush() } }