diff options
author | Daniel Schadt <kingdread@gmx.de> | 2019-12-12 02:16:27 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2019-12-12 02:16:27 +0100 |
commit | 3c92e88164db6a94177fb4adeb18c80dffc377e4 (patch) | |
tree | c2fdefbe04901df614a9753093d146f424d42a9c /src/cache.rs | |
parent | a6afa81d1d9f2dd7d10fe7c0555ae8a8a6d84867 (diff) | |
download | kondou-3c92e88164db6a94177fb4adeb18c80dffc377e4.tar.gz kondou-3c92e88164db6a94177fb4adeb18c80dffc377e4.tar.bz2 kondou-3c92e88164db6a94177fb4adeb18c80dffc377e4.zip |
remove quick_error
quick_error used the deprecated Error::cause interface, therefore we
want to use our own error enums with proper methods.
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), } } |