about summary refs log tree commit diff
path: root/tvix/castore/src
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2024-03-19T09·41+0200
committerclbot <clbot@tvl.fyi>2024-03-20T11·52+0000
commit7deadd50d50ed10062411879458fc8fc2c4bf8c0 (patch)
tree48640751d6cde36b89b874cb1472d1b172300126 /tvix/castore/src
parent602f4b2cb85aa1c25c533648f50cad962e0034d2 (diff)
refactor(tvix/castore/blob/from_addr): use match guards r/7745
This will allow feature-flagging some of the backends.

Change-Id: Idffbf8b3fd154f5a3d938225c3871feffea8ff8c
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11200
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Diffstat (limited to 'tvix/castore/src')
-rw-r--r--tvix/castore/src/blobservice/from_addr.rs112
1 files changed, 59 insertions, 53 deletions
diff --git a/tvix/castore/src/blobservice/from_addr.rs b/tvix/castore/src/blobservice/from_addr.rs
index 6803b415ec82..6925407552f6 100644
--- a/tvix/castore/src/blobservice/from_addr.rs
+++ b/tvix/castore/src/blobservice/from_addr.rs
@@ -20,66 +20,72 @@ pub async fn from_addr(uri: &str) -> Result<Box<dyn BlobService>, crate::Error>
     let url = Url::parse(uri)
         .map_err(|e| crate::Error::StorageError(format!("unable to parse url: {}", e)))?;
 
-    Ok(if url.scheme() == "memory" {
-        // memory doesn't support host or path in the URL.
-        if url.has_host() || !url.path().is_empty() {
-            return Err(Error::StorageError("invalid url".to_string()));
-        }
-        Box::<MemoryBlobService>::default()
-    } else if url.scheme() == "sled" {
-        // sled doesn't support host, and a path can be provided (otherwise
-        // it'll live in memory only).
-        if url.has_host() {
-            return Err(Error::StorageError("no host allowed".to_string()));
+    let blob_service: Box<dyn BlobService> = match url.scheme() {
+        "memory" => {
+            // memory doesn't support host or path in the URL.
+            if url.has_host() || !url.path().is_empty() {
+                return Err(Error::StorageError("invalid url".to_string()));
+            }
+            Box::<MemoryBlobService>::default()
         }
+        "sled" => {
+            // sled doesn't support host, and a path can be provided (otherwise
+            // it'll live in memory only).
+            if url.has_host() {
+                return Err(Error::StorageError("no host allowed".to_string()));
+            }
 
-        if url.path() == "/" {
-            return Err(Error::StorageError(
-                "cowardly refusing to open / with sled".to_string(),
-            ));
-        }
+            if url.path() == "/" {
+                return Err(Error::StorageError(
+                    "cowardly refusing to open / with sled".to_string(),
+                ));
+            }
+
+            // TODO: expose other parameters as URL parameters?
 
-        // TODO: expose other parameters as URL parameters?
+            Box::new(if url.path().is_empty() {
+                SledBlobService::new_temporary().map_err(|e| Error::StorageError(e.to_string()))?
+            } else {
+                SledBlobService::new(url.path()).map_err(|e| Error::StorageError(e.to_string()))?
+            })
+        }
+        scheme if scheme.starts_with("grpc+") => {
+            // schemes starting with grpc+ go to the GRPCPathInfoService.
+            //   That's normally grpc+unix for unix sockets, and grpc+http(s) for the HTTP counterparts.
+            // - In the case of unix sockets, there must be a path, but may not be a host.
+            // - In the case of non-unix sockets, there must be a host, but no path.
+            // Constructing the channel is handled by tvix_castore::channel::from_url.
+            let client = BlobServiceClient::new(crate::tonic::channel_from_url(&url).await?);
+            Box::new(GRPCBlobService::from_client(client))
+        }
+        "simplefs" => {
+            if url.path().is_empty() {
+                return Err(Error::StorageError("Invalid filesystem path".to_string()));
+            }
 
-        if url.path().is_empty() {
-            return Ok(Box::new(
-                SledBlobService::new_temporary().map_err(|e| Error::StorageError(e.to_string()))?,
-            ));
+            Box::new(SimpleFilesystemBlobService::new(url.path().into()).await?)
+        }
+        scheme if scheme.starts_with("objectstore+") => {
+            // We need to convert the URL to string, strip the prefix there, and then
+            // parse it back as url, as Url::set_scheme() rejects some of the transitions we want to do.
+            let trimmed_url = {
+                let s = url.to_string();
+                Url::parse(s.strip_prefix("objectstore+").unwrap()).unwrap()
+            };
+            Box::new(
+                ObjectStoreBlobService::parse_url(&trimmed_url)
+                    .map_err(|e| Error::StorageError(e.to_string()))?,
+            )
         }
-        return Ok(Box::new(
-            SledBlobService::new(url.path()).map_err(|e| Error::StorageError(e.to_string()))?,
-        ));
-    } else if url.scheme().starts_with("grpc+") {
-        // schemes starting with grpc+ go to the GRPCPathInfoService.
-        //   That's normally grpc+unix for unix sockets, and grpc+http(s) for the HTTP counterparts.
-        // - In the case of unix sockets, there must be a path, but may not be a host.
-        // - In the case of non-unix sockets, there must be a host, but no path.
-        // Constructing the channel is handled by tvix_castore::channel::from_url.
-        let client = BlobServiceClient::new(crate::tonic::channel_from_url(&url).await?);
-        Box::new(GRPCBlobService::from_client(client))
-    } else if url.scheme() == "simplefs" {
-        if url.path().is_empty() {
-            return Err(Error::StorageError("Invalid filesystem path".to_string()));
+        scheme => {
+            return Err(crate::Error::StorageError(format!(
+                "unknown scheme: {}",
+                scheme
+            )))
         }
+    };
 
-        Box::new(SimpleFilesystemBlobService::new(url.path().into()).await?)
-    } else if let Some(_trimmed_scheme) = url.scheme().strip_prefix("objectstore+") {
-        // We need to convert the URL to string, strip the prefix there, and then
-        // parse it back as url, as Url::set_scheme() rejects some of the transitions we want to do.
-        let trimmed_url = {
-            let s = url.to_string();
-            Url::parse(s.strip_prefix("objectstore+").unwrap()).unwrap()
-        };
-        return Ok(Box::new(
-            ObjectStoreBlobService::parse_url(&trimmed_url)
-                .map_err(|e| Error::StorageError(e.to_string()))?,
-        ));
-    } else {
-        Err(crate::Error::StorageError(format!(
-            "unknown scheme: {}",
-            url.scheme()
-        )))?
-    })
+    Ok(blob_service)
 }
 
 #[cfg(test)]