From 9e019b462ff05315641c95442faf7f8fc80f76b1 Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 21 Sep 2021 01:04:49 +0200 Subject: Slightly more consistent error handling --- src/history.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/history.rs') 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 { + pub fn from_file(file: &mut File) -> Result { 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::(&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 = serde_json::to_vec(self).unwrap(); + pub fn write_to_file(&self, file: &mut File) -> anyhow::Result<()> { + let encoded: Vec = serde_json::to_vec(self)?; file.rewind()?; file.set_len(0)?; file.write_all(encoded.as_ref())?; -- cgit 1.4.1