From 55da5ae8cf731c768d6cc580693068e64234a4af Mon Sep 17 00:00:00 2001 From: Mel Date: Sat, 2 Oct 2021 19:55:01 +0200 Subject: Use filesystem abstraction throughout Ka. --- src/history.rs | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'src/history.rs') diff --git a/src/history.rs b/src/history.rs index 72640b6..4f23790 100644 --- a/src/history.rs +++ b/src/history.rs @@ -1,14 +1,10 @@ -use std::{ - fs::File, - io::{Read, Seek, Write}, - path::PathBuf, -}; +use std::path::PathBuf; use serde::{Deserialize, Serialize}; use anyhow::{Context, Result}; -use crate::diff::ContentChange; +use crate::{diff::ContentChange, filesystem::Fs}; #[derive(Serialize, Deserialize, Debug)] pub struct RepositoryHistory { @@ -17,20 +13,18 @@ pub struct RepositoryHistory { } impl RepositoryHistory { - pub fn from_file(file: &mut File) -> Result { - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer) + pub fn from_file(fs: &mut FS, file: &mut FS::File) -> Result { + let buffer = fs + .read_from_file(file) .context("Failed reading repository history.")?; let repository_history = serde_json::from_slice::(&buffer); repository_history.context("Corrupted repository history.") } - pub fn write_to_file(&self, file: &mut File) -> anyhow::Result<()> { + pub fn write_to_file(&self, fs: &mut FS, file: &mut FS::File) -> anyhow::Result<()> { let encoded: Vec = serde_json::to_vec(self)?; - file.rewind()?; - file.set_len(0)?; - file.write_all(encoded.as_ref())?; + fs.write_to_file(file, encoded)?; Ok(()) } @@ -64,20 +58,18 @@ pub struct FileHistory { } impl FileHistory { - pub fn from_file(file: &mut File) -> Result { - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer) + pub fn from_file(fs: &mut FS, file: &mut FS::File) -> Result { + let buffer = fs + .read_from_file(file) .context("Failed reading file history.")?; let file_history = serde_json::from_slice::(&buffer); file_history.context("Corrupted file history.") } - pub fn write_to_file(&self, file: &mut File) -> anyhow::Result<()> { + pub fn write_to_file(&self, fs: &mut FS, file: &mut FS::File) -> Result<()> { let encoded: Vec = serde_json::to_vec(self)?; - file.rewind()?; - file.set_len(0)?; - file.write_all(encoded.as_ref())?; + fs.write_to_file(file, encoded)?; Ok(()) } -- cgit 1.4.1