From fad317349557bee1055d81d7444f99619247815c Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 5 Oct 2021 19:06:01 +0200 Subject: Fs asserts and Create action tests. --- src/history.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'src/history.rs') diff --git a/src/history.rs b/src/history.rs index d023330..d31ecb9 100644 --- a/src/history.rs +++ b/src/history.rs @@ -13,17 +13,24 @@ pub struct RepositoryHistory { } impl RepositoryHistory { - pub fn from_file(fs: &FS, file: &mut FS::File) -> Result { + pub fn encode(&self) -> Result> { + serde_json::to_vec(self).context("Failed encoding repository history.") + } + + pub fn decode(buffer: &[u8]) -> Result { + serde_json::from_slice::(buffer).context("Failed decoding repository history.") + } + + pub fn from_file(fs: &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.") + Self::decode(&buffer) } - pub fn write_to_file(&self, fs: &FS, file: &mut FS::File) -> anyhow::Result<()> { - let encoded: Vec = serde_json::to_vec(self)?; + pub fn write_to_file(&self, fs: &FS, file: &mut FS::File) -> Result<()> { + let encoded: Vec = self.encode()?; fs.write_to_file(file, encoded)?; Ok(()) } @@ -58,17 +65,24 @@ pub struct FileHistory { } impl FileHistory { - pub fn from_file(fs: &FS, file: &mut FS::File) -> Result { + pub fn encode(&self) -> Result> { + serde_json::to_vec(self).context("Failed encoding file history.") + } + + pub fn decode(buffer: &[u8]) -> Result { + serde_json::from_slice::(buffer).context("Failed decoding file history.") + } + + pub fn from_file(fs: &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.") + Self::decode(&buffer) } pub fn write_to_file(&self, fs: &FS, file: &mut FS::File) -> Result<()> { - let encoded: Vec = serde_json::to_vec(self)?; + let encoded: Vec = self.encode()?; fs.write_to_file(file, encoded)?; Ok(()) } -- cgit 1.4.1