diff options
| author | Mel <einebeere@gmail.com> | 2021-10-02 19:55:01 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2021-10-02 19:55:01 +0200 |
| commit | 55da5ae8cf731c768d6cc580693068e64234a4af (patch) | |
| tree | 9024bf8c315f1c150a468db6791acbeec2907601 /src/history.rs | |
| parent | c1eccadf76ca7625d8cff9a52480168aab876e62 (diff) | |
| download | ka-55da5ae8cf731c768d6cc580693068e64234a4af.tar.zst ka-55da5ae8cf731c768d6cc580693068e64234a4af.zip | |
Use filesystem abstraction throughout Ka.
Diffstat (limited to 'src/history.rs')
| -rw-r--r-- | src/history.rs | 32 |
1 files changed, 12 insertions, 20 deletions
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<RepositoryHistory> { - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer) + pub fn from_file<FS: Fs>(fs: &mut FS, file: &mut FS::File) -> Result<RepositoryHistory> { + let buffer = fs + .read_from_file(file) .context("Failed reading repository history.")?; let repository_history = serde_json::from_slice::<RepositoryHistory>(&buffer); repository_history.context("Corrupted repository history.") } - pub fn write_to_file(&self, file: &mut File) -> anyhow::Result<()> { + pub fn write_to_file<FS: Fs>(&self, fs: &mut FS, file: &mut FS::File) -> anyhow::Result<()> { let encoded: Vec<u8> = 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<FileHistory> { - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer) + pub fn from_file<FS: Fs>(fs: &mut FS, file: &mut FS::File) -> Result<FileHistory> { + let buffer = fs + .read_from_file(file) .context("Failed reading file history.")?; let file_history = serde_json::from_slice::<FileHistory>(&buffer); file_history.context("Corrupted file history.") } - pub fn write_to_file(&self, file: &mut File) -> anyhow::Result<()> { + pub fn write_to_file<FS: Fs>(&self, fs: &mut FS, file: &mut FS::File) -> Result<()> { let encoded: Vec<u8> = serde_json::to_vec(self)?; - file.rewind()?; - file.set_len(0)?; - file.write_all(encoded.as_ref())?; + fs.write_to_file(file, encoded)?; Ok(()) } |
