about summary refs log tree commit diff
path: root/src/history.rs
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2021-10-05 19:06:01 +0200
committerMel <einebeere@gmail.com>2021-10-05 19:06:01 +0200
commitfad317349557bee1055d81d7444f99619247815c (patch)
tree50d9e081fcdab32b02daed8d23fa41e8eacd1d83 /src/history.rs
parentad153a44d383962ac8021c02023a92b4c987fd54 (diff)
downloadka-fad317349557bee1055d81d7444f99619247815c.tar.zst
ka-fad317349557bee1055d81d7444f99619247815c.zip
Fs asserts and Create action tests.
Diffstat (limited to 'src/history.rs')
-rw-r--r--src/history.rs32
1 files changed, 23 insertions, 9 deletions
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>(fs: &FS, file: &mut FS::File) -> Result<RepositoryHistory> {
+    pub fn encode(&self) -> Result<Vec<u8>> {
+        serde_json::to_vec(self).context("Failed encoding repository history.")
+    }
+
+    pub fn decode(buffer: &[u8]) -> Result<Self> {
+        serde_json::from_slice::<Self>(buffer).context("Failed decoding repository history.")
+    }
+
+    pub fn from_file<FS: Fs>(fs: &FS, file: &mut FS::File) -> Result<Self> {
         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.")
+        Self::decode(&buffer)
     }
 
-    pub fn write_to_file<FS: Fs>(&self, fs: &FS, file: &mut FS::File) -> anyhow::Result<()> {
-        let encoded: Vec<u8> = serde_json::to_vec(self)?;
+    pub fn write_to_file<FS: Fs>(&self, fs: &FS, file: &mut FS::File) -> Result<()> {
+        let encoded: Vec<u8> = self.encode()?;
         fs.write_to_file(file, encoded)?;
         Ok(())
     }
@@ -58,17 +65,24 @@ pub struct FileHistory {
 }
 
 impl FileHistory {
-    pub fn from_file<FS: Fs>(fs: &FS, file: &mut FS::File) -> Result<FileHistory> {
+    pub fn encode(&self) -> Result<Vec<u8>> {
+        serde_json::to_vec(self).context("Failed encoding file history.")
+    }
+
+    pub fn decode(buffer: &[u8]) -> Result<Self> {
+        serde_json::from_slice::<Self>(buffer).context("Failed decoding file history.")
+    }
+
+    pub fn from_file<FS: Fs>(fs: &FS, file: &mut FS::File) -> Result<Self> {
         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.")
+        Self::decode(&buffer)
     }
 
     pub fn write_to_file<FS: Fs>(&self, fs: &FS, file: &mut FS::File) -> Result<()> {
-        let encoded: Vec<u8> = serde_json::to_vec(self)?;
+        let encoded: Vec<u8> = self.encode()?;
         fs.write_to_file(file, encoded)?;
         Ok(())
     }