about summary refs log tree commit diff
path: root/src/history.rs
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2021-09-21 01:04:49 +0200
committerMel <einebeere@gmail.com>2021-09-21 01:05:03 +0200
commit9e019b462ff05315641c95442faf7f8fc80f76b1 (patch)
tree5813c85887544d865542fb122882a1fb9d2be718 /src/history.rs
parentc25d22681f9bc6ddba512895182af0ce0252731b (diff)
downloadka-9e019b462ff05315641c95442faf7f8fc80f76b1.tar.zst
ka-9e019b462ff05315641c95442faf7f8fc80f76b1.zip
Slightly more consistent error handling
Diffstat (limited to 'src/history.rs')
-rw-r--r--src/history.rs17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/history.rs b/src/history.rs
index 25a9555..13b2838 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -1,7 +1,12 @@
-use std::{fs::File, io::{self, Read, Seek, Write}};
+use std::{
+    fs::File,
+    io::{Read, Seek, Write},
+};
 
 use serde::{Deserialize, Serialize};
 
+use anyhow::{Context, Result};
+
 use crate::text_diff::TextChange;
 
 #[derive(Serialize, Deserialize, Debug)]
@@ -11,13 +16,13 @@ pub struct FileHistory {
 }
 
 impl FileHistory {
-    pub fn from_file(file: &mut File) -> Result<FileHistory, ()> {
+    pub fn from_file(file: &mut File) -> Result<FileHistory> {
         let mut buffer = Vec::new();
         file.read_to_end(&mut buffer)
-            .expect("Could not read file history,");
+            .context("Failed reading file history.")?;
 
         let file_history = serde_json::from_slice::<FileHistory>(&buffer);
-        Ok(file_history.expect("Corrupted file history."))
+        Ok(file_history.context("Corrupted file history.")?)
     }
 
     pub fn cursor(&self) -> usize {
@@ -48,8 +53,8 @@ impl FileHistory {
         buffer
     }
 
-    pub fn write_to_file(&self, file: &mut File) -> io::Result<()> {
-        let encoded: Vec<u8> = serde_json::to_vec(self).unwrap();
+    pub fn write_to_file(&self, file: &mut File) -> anyhow::Result<()> {
+        let encoded: Vec<u8> = serde_json::to_vec(self)?;
         file.rewind()?;
         file.set_len(0)?;
         file.write_all(encoded.as_ref())?;