From 3c92e88164db6a94177fb4adeb18c80dffc377e4 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 12 Dec 2019 02:16:27 +0100 Subject: remove quick_error quick_error used the deprecated Error::cause interface, therefore we want to use our own error enums with proper methods. --- src/cache.rs | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'src/cache.rs') diff --git a/src/cache.rs b/src/cache.rs index f64b45a..2b490ec 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,18 +1,31 @@ //! Caching support to prevent hitting the API a lot. -use std::fs::File; -use std::io::prelude::*; -use std::path::Path; +use std::{error::Error, fmt, fs, path::Path}; use xdg::BaseDirectories; use super::APP_NAME; -quick_error! { - #[derive(Debug)] - pub enum CacheError { - Io(err: std::io::Error) { - cause(err) - from() +#[derive(Debug)] +pub enum CacheError { + Io(std::io::Error), +} + +error_froms! { CacheError, + err: std::io::Error => CacheError::Io(err), +} + +impl fmt::Display for CacheError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + CacheError::Io(_) => write!(f, "cache input/output error"), + } + } +} + +impl Error for CacheError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + match *self { + CacheError::Io(ref err) => Some(err), } } } @@ -39,20 +52,14 @@ impl FileCache { impl Cache for FileCache { fn store(&mut self, path: &Path, data: &[u8]) -> Result<(), CacheError> { let cache_path = self.dirs.place_cache_file(path).unwrap(); - let mut f = File::create(cache_path)?; - f.write_all(data)?; + fs::write(cache_path, data)?; Ok(()) } fn get(&mut self, path: &Path) -> Result>, CacheError> { let cache_path = self.dirs.find_cache_file(path); match cache_path { - Some(p) => { - let mut f = File::open(p)?; - let mut buffer = Vec::new(); - f.read_to_end(&mut buffer)?; - Ok(Some(buffer)) - } + Some(path) => Ok(Some(fs::read(path)?)), None => Ok(None), } } -- cgit v1.2.3