|
@@ -133,6 +133,8 @@ pub enum Error {
|
|
|
HttpErrorStatus(u16),
|
|
|
}
|
|
|
|
|
|
+// This allows ? to return cache::Error from std::io::Error (see expire)
|
|
|
+
|
|
|
impl From<std::io::Error> for Error {
|
|
|
fn from(e: std::io::Error) -> Self {
|
|
|
Self::IOError(e)
|
|
@@ -152,11 +154,12 @@ impl fmt::Display for Error {
|
|
|
Error::IOError(e) => write!(f, "IOError: {:?}", e),
|
|
|
Error::Unacceptable(ct) => write!(f, "Content-Type {} not allowed", ct),
|
|
|
Error::TooBig(size) => write!(f, "Content-Size {} too big", size),
|
|
|
- Error::HttpErrorStatus(status) => write!(f, "Status Code: {}", status)
|
|
|
+ Error::HttpErrorStatus(status) => write!(f, "Status Code: {}", status),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// This made anyhow happy with my cache::Error.
|
|
|
impl Errorr for Error {}
|
|
|
|
|
|
/*
|
|
@@ -185,13 +188,13 @@ impl Cache {
|
|
|
if path.exists() {
|
|
|
if !path.is_dir() {
|
|
|
// It exists, but it isn't a directory! What?!
|
|
|
- std::io::Error::new(
|
|
|
+ return Err(Error::IOError(std::io::Error::new(
|
|
|
std::io::ErrorKind::Other,
|
|
|
format!(
|
|
|
"Can't create Cache dir {}, it already exists.",
|
|
|
dir.display()
|
|
|
),
|
|
|
- );
|
|
|
+ )));
|
|
|
}
|
|
|
} else {
|
|
|
match create_dir_all(path) {
|
|
@@ -222,6 +225,7 @@ impl Cache {
|
|
|
})
|
|
|
}
|
|
|
Err(e) => {
|
|
|
+ // Client::builder error
|
|
|
return Err(Error::ReqwestError(e));
|
|
|
}
|
|
|
}
|
|
@@ -232,11 +236,21 @@ impl Cache {
|
|
|
self.accept.push(content_type);
|
|
|
}
|
|
|
|
|
|
+ #[allow(dead_code)]
|
|
|
+ pub fn clear_content_type(&mut self) {
|
|
|
+ self.accept.clear();
|
|
|
+ }
|
|
|
+
|
|
|
#[allow(dead_code)]
|
|
|
pub fn set_max_size(&mut self, size: u64) {
|
|
|
self.max_size = Some(size);
|
|
|
}
|
|
|
|
|
|
+ #[allow(dead_code)]
|
|
|
+ pub fn clear_max_size(&mut self) {
|
|
|
+ self.max_size = None;
|
|
|
+ }
|
|
|
+
|
|
|
/// Create safe filename from url for header/content files.
|
|
|
pub fn url_to_basename(url: &str) -> String {
|
|
|
let filename = if url.ends_with("/") {
|
|
@@ -341,9 +355,7 @@ impl Cache {
|
|
|
/// The filename might not exist. It is only the filename
|
|
|
/// that would be used for the given url.
|
|
|
pub fn filename_for_url(&self, url: &str) -> PathBuf {
|
|
|
- self.directory
|
|
|
- .as_path()
|
|
|
- .join(Self::url_to_basename(url))
|
|
|
+ self.directory.as_path().join(Self::url_to_basename(url))
|
|
|
}
|
|
|
|
|
|
/// Given a url, return an open file
|
|
@@ -520,7 +532,7 @@ impl Cache {
|
|
|
Err(e) => {
|
|
|
return Err(Error::ReqwestError(e));
|
|
|
}
|
|
|
- }
|
|
|
+ },
|
|
|
Err(e) => {
|
|
|
return Err(Error::IOError(e));
|
|
|
}
|
|
@@ -623,7 +635,7 @@ mod tests {
|
|
|
for (url, base) in url_base {
|
|
|
// Verify url_to_basename.
|
|
|
let basename = Cache::url_to_basename(url);
|
|
|
- assert_eq!(base, basename, "{} -> {}", url, base);
|
|
|
+ assert_eq!(base, basename, "{} -> {}", url, base);
|
|
|
// Verify filename_for_url.
|
|
|
let path = cache.filename_for_url(url);
|
|
|
let mut newpath = temp.clone();
|
|
@@ -685,7 +697,6 @@ mod tests {
|
|
|
} else {
|
|
|
panic!("cache.fetch: {:?}", r);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -706,8 +717,6 @@ mod tests {
|
|
|
#[test]
|
|
|
#[cfg(feature = "local-httpbin")]
|
|
|
fn call_local() {
|
|
|
- use reqwest::Error;
|
|
|
-
|
|
|
let mut dir = testdir!();
|
|
|
dir.push("cache");
|
|
|
|
|
@@ -718,8 +727,8 @@ mod tests {
|
|
|
let teapot_url = "http://127.0.0.1/status/418";
|
|
|
|
|
|
let r = cache.fetch(&teapot_url);
|
|
|
- if let Ok(r) = r {
|
|
|
- if let Status::HttpErrorStatus(code) = r {
|
|
|
+ if let Err(e) = r {
|
|
|
+ if let Error::HttpErrorStatus(code) = e {
|
|
|
assert_eq!(code, 418);
|
|
|
} else {
|
|
|
panic!("Not an ErrorStatus");
|