diff --git a/generator/src/main.rs b/generator/src/main.rs index bf6fffe4..bbacbc4c 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -2390,7 +2390,12 @@ pub enum ClientError {"#); }, /// JWT errors from auth.rs #[error(transparent)] - JsonWebTokenError(#[from] jsonwebtoken::errors::Error),"#); + JsonWebTokenError(#[from] jsonwebtoken::errors::Error), + /// IO Errors + #[cfg(feature = "httpcache")] + #[error(transparent)] + #[cfg(feature = "httpcache")] + IoError(#[from] std::io::Error),"#); } TemplateType::GenericApiKey | TemplateType::GenericClientCredentials => { a(r#"/// utf8 convertion error diff --git a/github/src/http_cache.rs b/github/src/http_cache.rs index 083f6696..4df08ba2 100644 --- a/github/src/http_cache.rs +++ b/github/src/http_cache.rs @@ -9,7 +9,8 @@ use std::{ path::{Path, PathBuf}, }; -use anyhow::{Error, Result}; +use crate::ClientResult; +use crate::ClientError; use http::Uri; /// A type for an HTTP cache. @@ -23,10 +24,10 @@ pub trait HttpCache: HttpCacheClone + Debug { body: &[u8], etag: &[u8], next_link: &Option, - ) -> Result<()>; - fn lookup_etag(&self, uri: &str) -> Result; - fn lookup_body(&self, uri: &str) -> Result; - fn lookup_next_link(&self, uri: &str) -> Result>; + ) -> ClientResult<()>; + fn lookup_etag(&self, uri: &str) -> ClientResult; + fn lookup_body(&self, uri: &str) -> ClientResult; + fn lookup_next_link(&self, uri: &str) -> ClientResult>; } impl dyn HttpCache { @@ -60,16 +61,16 @@ impl Clone for BoxedHttpCache { pub struct NoCache; impl HttpCache for NoCache { - fn cache_response(&self, _: &str, _: &[u8], _: &[u8], _: &Option) -> Result<()> { + fn cache_response(&self, _: &str, _: &[u8], _: &[u8], _: &Option) -> ClientResult<()> { Ok(()) } - fn lookup_etag(&self, _uri: &str) -> Result { + fn lookup_etag(&self, _uri: &str) -> ClientResult { no_read("No etag cached") } - fn lookup_body(&self, _uri: &str) -> Result { + fn lookup_body(&self, _uri: &str) -> ClientResult { no_read("No body cached") } - fn lookup_next_link(&self, _uri: &str) -> Result> { + fn lookup_next_link(&self, _uri: &str) -> ClientResult> { no_read("No next link cached") } } @@ -94,7 +95,7 @@ impl HttpCache for FileBasedCache { body: &[u8], etag: &[u8], next_link: &Option, - ) -> Result<()> { + ) -> ClientResult<()> { let mut path = cache_path(&self.root, uri, "json"); //println!("caching body at path: {}", path.display()); if let Some(parent) = path.parent() { @@ -110,15 +111,15 @@ impl HttpCache for FileBasedCache { Ok(()) } - fn lookup_etag(&self, uri: &str) -> Result { + fn lookup_etag(&self, uri: &str) -> ClientResult { read_to_string(cache_path(&self.root, uri, "etag")) } - fn lookup_body(&self, uri: &str) -> Result { + fn lookup_body(&self, uri: &str) -> ClientResult { read_to_string(cache_path(&self.root, uri, "json")) } - fn lookup_next_link(&self, uri: &str) -> Result> { + fn lookup_next_link(&self, uri: &str) -> ClientResult> { let path = cache_path(&self.root, uri, "next_link"); if path.exists() { Ok(Some(read_to_string(path)?)) @@ -185,13 +186,13 @@ pub fn cache_path>(dir: &Path, uri: &str, extension: S) -> PathB path } -fn read_to_string>(path: P) -> Result { +fn read_to_string>(path: P) -> ClientResult { //println!("reading path: {}", path.as_ref().display()); - fs::read_to_string(path).map_err(Error::from) + Ok(fs::read_to_string(path)?) } -fn no_read>>(error: E) -> Result { - Err(Error::from(io::Error::new(io::ErrorKind::NotFound, error))) +fn no_read>>(error: E) -> ClientResult { + Err(ClientError::IoError(io::Error::new(io::ErrorKind::NotFound, error))) } // Separate to provide a blanket implementation for `T: HttpCache + Clone`