From 1415c996060e8fa7949c8cf04c47889037db6ce2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= <biwanczuk@gmail.com>
Date: Tue, 14 Jan 2025 15:15:13 +0100
Subject: [PATCH] lints now

---
 ext/cache/lib.rs       | 12 ++++++++++++
 ext/cache/lsc_shard.rs | 29 ++++++++++++++---------------
 ext/cache/lscache.rs   | 13 ++-----------
 3 files changed, 28 insertions(+), 26 deletions(-)

diff --git a/ext/cache/lib.rs b/ext/cache/lib.rs
index f2a879e709..7c6e8b16ff 100644
--- a/ext/cache/lib.rs
+++ b/ext/cache/lib.rs
@@ -40,6 +40,9 @@ pub enum CacheError {
   #[class(type)]
   #[error("Cache deletion is not supported")]
   DeletionNotSupported,
+  #[class(type)]
+  #[error("Content-Encoding is not allowed in response headers")]
+  ContentEncodingNotAllowed,
   #[class(generic)]
   #[error(transparent)]
   Sqlite(#[from] rusqlite::Error),
@@ -68,6 +71,15 @@ pub enum CacheError {
     #[source]
     source: std::io::Error,
   },
+  #[class(generic)]
+  #[error("cache {method} request failed: {status}")]
+  RequestFailed {
+    method: &'static str,
+    status: hyper::StatusCode,
+  },
+  #[class(generic)]
+  #[error("{0}")]
+  Reqwest(#[from] reqwest::Error),
 }
 
 #[derive(Clone)]
diff --git a/ext/cache/lsc_shard.rs b/ext/cache/lsc_shard.rs
index ac63131c74..7a3df607a0 100644
--- a/ext/cache/lsc_shard.rs
+++ b/ext/cache/lsc_shard.rs
@@ -1,10 +1,11 @@
 // Copyright 2018-2025 the Deno authors. MIT license.
 
-use anyhow::Context;
 use hyper::header::AUTHORIZATION;
 use hyper::HeaderMap;
 use hyper::StatusCode;
 
+use crate::CacheError;
+
 pub struct CacheShard {
   client: reqwest::Client,
   endpoint: String,
@@ -26,7 +27,7 @@ impl CacheShard {
   pub async fn get_object(
     &self,
     object_key: &str,
-  ) -> anyhow::Result<Option<reqwest::Response>> {
+  ) -> Result<Option<reqwest::Response>, CacheError> {
     let res = self
       .client
       .get(format!("{}/objects/{}", self.endpoint, object_key))
@@ -34,18 +35,17 @@ impl CacheShard {
       .header("x-ryw", "1")
       .send()
       .await
-      .map_err(|e| e.without_url())
-      .with_context(|| "failed to start cache GET request")?;
+      .map_err(|e| e.without_url())?;
 
     if res.status().is_success() {
       Ok(Some(res))
     } else if res.status() == StatusCode::NOT_FOUND {
       Ok(None)
     } else {
-      Err(anyhow::anyhow!(
-        "cache GET request failed: {}",
-        res.status()
-      ))
+      Err(CacheError::RequestFailed {
+        method: "GET",
+        status: res.status(),
+      })
     }
   }
 
@@ -54,7 +54,7 @@ impl CacheShard {
     object_key: &str,
     headers: HeaderMap,
     body: reqwest::Body,
-  ) -> anyhow::Result<()> {
+  ) -> Result<(), CacheError> {
     let res = self
       .client
       .put(format!("{}/objects/{}", self.endpoint, object_key))
@@ -63,16 +63,15 @@ impl CacheShard {
       .body(body)
       .send()
       .await
-      .map_err(|e| e.without_url())
-      .with_context(|| "failed to start cache PUT request")?;
+      .map_err(|e| e.without_url())?;
 
     if res.status().is_success() {
       Ok(())
     } else {
-      Err(anyhow::anyhow!(
-        "cache PUT request failed: {}",
-        res.status()
-      ))
+      Err(CacheError::RequestFailed {
+        method: "GET",
+        status: res.status(),
+      })
     }
   }
 }
diff --git a/ext/cache/lscache.rs b/ext/cache/lscache.rs
index d27ddcf16e..8ceb8f0fa9 100644
--- a/ext/cache/lscache.rs
+++ b/ext/cache/lscache.rs
@@ -39,20 +39,13 @@ use crate::CachePutRequest;
 
 const REQHDR_PREFIX: &str = "x-lsc-meta-reqhdr-";
 
-#[derive(Clone)]
+#[derive(Clone, Default)]
 pub struct LscBackend {
   shard: Rc<RefCell<Option<Rc<CacheShard>>>>,
   id2name: Rc<RefCell<Slab<String>>>,
 }
 
 impl LscBackend {
-  pub fn new() -> Self {
-    Self {
-      shard: Rc::new(RefCell::new(None)),
-      id2name: Rc::new(RefCell::new(Slab::new())),
-    }
-  }
-
   pub fn set_shard(&self, shard: Rc<CacheShard>) {
     *self.shard.borrow_mut() = Some(shard);
   }
@@ -121,9 +114,7 @@ impl Cache for LscBackend {
         continue;
       }
       if hdr.0[..] == b"content-encoding"[..] {
-        return Err(type_error(
-          "Content-Encoding is not allowed in response headers",
-        ));
+        return Err(CacheError::ContentEncodingNotAllowed);
       }
       headers.insert(
         HeaderName::from_bytes(&hdr.0[..])?,