diff options
author | Toby Vincent <tobyv13@gmail.com> | 2023-04-02 15:22:35 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2023-04-02 15:23:02 -0500 |
commit | c90dc69bd3dccfad43e3a2f26713543b5c876005 (patch) | |
tree | 694676106b7ae94ddf7179a05a061cfa2056d965 /src/history.rs | |
parent | e11e8bbf14be8f84b57013e4ace1e61071853c12 (diff) |
feat: rewrite to use Extend trait and remove tmux control
Diffstat (limited to 'src/history.rs')
-rw-r--r-- | src/history.rs | 91 |
1 files changed, 28 insertions, 63 deletions
diff --git a/src/history.rs b/src/history.rs index d7e7063..863be8e 100644 --- a/src/history.rs +++ b/src/history.rs @@ -1,32 +1,34 @@ use std::{ fs::File, - io::{BufRead, BufReader, BufWriter, Write}, + io::{BufRead, BufReader, Write}, path::PathBuf, }; use directories::ProjectDirs; -use crate::{Session, SessionSource}; +use crate::Session; pub use error::Error; -mod error { - #[derive(Debug, thiserror::Error)] - pub enum Error { - #[error("IO error: {0}")] - IO(#[from] std::io::Error), - - #[error(transparent)] - Format(#[from] ron::Error), - } -} +mod error; #[derive(Debug)] -pub struct History(PathBuf); +pub struct History { + pub file: File, + pub entries: Vec<Session>, +} impl History { - pub fn new(history_file: PathBuf) -> Self { - Self(history_file) + pub fn open(history_file: PathBuf) -> Result<Self, std::io::Error> { + 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<PathBuf> { @@ -35,61 +37,24 @@ impl History { .join("history") .into() } - - pub fn update_session(&self, session: Session) -> Result<(), Error> { - std::fs::create_dir_all(&self.0)?; - - let mut sessions: Vec<Session> = self - .sessions()? - .into_iter() - .filter(|s| s.name == session.name) - .collect(); - - sessions.push(session); - - let mut writer = BufWriter::new(File::create(&self.0)?); - - Ok(sessions - .into_iter() - .try_for_each(|s| match ron::to_string(&s) { - Ok(ser) => writeln!(writer, "{ser}"), - Err(err) => Ok(tracing::warn!(?err, "Failed to re-serialize session")), - })?) - } } -impl SessionSource for History { - type Error = std::io::Error; +impl IntoIterator for History { + type Item = Session; - type Iter = Vec<Session>; + type IntoIter = std::vec::IntoIter<Self::Item>; - fn sessions(&self) -> Result<Self::Iter, Self::Error> { - 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) + fn into_iter(self) -> Self::IntoIter { + self.entries.into_iter() } } -impl std::ops::Deref for History { - type Target = PathBuf; +impl Write for History { + fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { + self.file.write(buf) + } - fn deref(&self) -> &Self::Target { - &self.0 + fn flush(&mut self) -> std::io::Result<()> { + self.file.flush() } } |