about summary refs log tree commit diff
path: root/tvix/castore/src
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2024-03-19T10·11+0200
committerclbot <clbot@tvl.fyi>2024-03-20T11·53+0000
commitc0566985b03f5880799455f78252b002e00f3db6 (patch)
tree2fdc8cd6b0d6fba9b6b4ca54abf3f38970dbca1e /tvix/castore/src
parent7deadd50d50ed10062411879458fc8fc2c4bf8c0 (diff)
refactor(tvix/castore/directory/from_addr): use match guards r/7746
This will allow feature-flagging some of the backends.

Change-Id: Iddbdb89d3cf9c966a2c25b06b03e6917b284cae5
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11201
Tested-by: BuildkiteCI
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Autosubmit: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/castore/src')
-rw-r--r--tvix/castore/src/directoryservice/from_addr.rs81
1 files changed, 42 insertions, 39 deletions
diff --git a/tvix/castore/src/directoryservice/from_addr.rs b/tvix/castore/src/directoryservice/from_addr.rs
index 14251ee4bbc8..afb88c63cacf 100644
--- a/tvix/castore/src/directoryservice/from_addr.rs
+++ b/tvix/castore/src/directoryservice/from_addr.rs
@@ -22,51 +22,54 @@ pub async fn from_addr(uri: &str) -> Result<Box<dyn DirectoryService>, crate::Er
     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::<MemoryDirectoryService>::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 directory_service: Box<dyn DirectoryService> = 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::<MemoryDirectoryService>::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 compression and other parameters as URL parameters?
+            // TODO: expose compression and other parameters as URL parameters?
 
-        if url.path().is_empty() {
-            return Ok(Box::new(
+            Box::new(if url.path().is_empty() {
                 SledDirectoryService::new_temporary()
-                    .map_err(|e| Error::StorageError(e.to_string()))?,
-            ));
+                    .map_err(|e| Error::StorageError(e.to_string()))?
+            } else {
+                SledDirectoryService::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 = DirectoryServiceClient::new(crate::tonic::channel_from_url(&url).await?);
+            Box::new(GRPCDirectoryService::from_client(client))
+        }
+        _ => {
+            return Err(crate::Error::StorageError(format!(
+                "unknown scheme: {}",
+                url.scheme()
+            )))
         }
-        return Ok(Box::new(
-            SledDirectoryService::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 = DirectoryServiceClient::new(crate::tonic::channel_from_url(&url).await?);
-        Box::new(GRPCDirectoryService::from_client(client))
-    } else {
-        Err(crate::Error::StorageError(format!(
-            "unknown scheme: {}",
-            url.scheme()
-        )))?
-    })
+    };
+    Ok(directory_service)
 }
 
 #[cfg(test)]