diff options
Diffstat (limited to 'src/cache.rs')
-rw-r--r-- | src/cache.rs | 41 |
1 files changed, 24 insertions, 17 deletions
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<Option<Vec<u8>>, 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), } } |