about summary refs log tree commit diff
path: root/tvix/store/src/pathinfoservice/from_addr.rs
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2024-03-19T10·23+0200
committerclbot <clbot@tvl.fyi>2024-03-20T11·53+0000
commit65b8359ff3ce1bed882f2d773fe137b673531801 (patch)
tree4ce31d664c729b09fd7a8f7b3c6e0957311b7239 /tvix/store/src/pathinfoservice/from_addr.rs
parentc0566985b03f5880799455f78252b002e00f3db6 (diff)
refactor(tvix/store/pathinfo/from_addr): use match guards r/7747
This will allow feature-flagging some of the backends.

Change-Id: Ie92914c3e2ad870eee87e73b3b5abe605fb56fe7
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11202
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Diffstat (limited to 'tvix/store/src/pathinfoservice/from_addr.rs')
-rw-r--r--tvix/store/src/pathinfoservice/from_addr.rs121
1 files changed, 62 insertions, 59 deletions
diff --git a/tvix/store/src/pathinfoservice/from_addr.rs b/tvix/store/src/pathinfoservice/from_addr.rs
index f5a287cd787f..142428768d60 100644
--- a/tvix/store/src/pathinfoservice/from_addr.rs
+++ b/tvix/store/src/pathinfoservice/from_addr.rs
@@ -40,78 +40,81 @@ pub async fn from_addr(
     let url =
         Url::parse(uri).map_err(|e| 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::new(MemoryPathInfoService::new(blob_service, directory_service))
-    } 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 path_info_service: Box<dyn PathInfoService> = 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::new(MemoryPathInfoService::new(blob_service, directory_service))
         }
+        "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?
 
-        if url.path().is_empty() {
-            return Ok(Box::new(
+            Box::new(if url.path().is_empty() {
                 SledPathInfoService::new_temporary(blob_service, directory_service)
-                    .map_err(|e| Error::StorageError(e.to_string()))?,
-            ));
+                    .map_err(|e| Error::StorageError(e.to_string()))?
+            } else {
+                SledPathInfoService::new(url.path(), blob_service, directory_service)
+                    .map_err(|e| Error::StorageError(e.to_string()))?
+            })
         }
-        return Ok(Box::new(
-            SledPathInfoService::new(url.path(), blob_service, directory_service)
-                .map_err(|e| Error::StorageError(e.to_string()))?,
-        ));
-    } else if url.scheme() == "nix+http" || url.scheme() == "nix+https" {
-        // Stringify the URL and remove the nix+ prefix.
-        // We can't use `url.set_scheme(rest)`, as it disallows
-        // setting something http(s) that previously wasn't.
-        let new_url = Url::parse(url.to_string().strip_prefix("nix+").unwrap()).unwrap();
-
-        let mut nix_http_path_info_service =
-            NixHTTPPathInfoService::new(new_url, blob_service, directory_service);
-
-        let pairs = &url.query_pairs();
-        for (k, v) in pairs.into_iter() {
-            if k == "trusted-public-keys" {
-                let pubkey_strs: Vec<_> = v.split_ascii_whitespace().collect();
-
-                let mut pubkeys: Vec<narinfo::PubKey> = Vec::with_capacity(pubkey_strs.len());
-                for pubkey_str in pubkey_strs {
-                    pubkeys
-                        .push(narinfo::PubKey::parse(pubkey_str).map_err(|e| {
+        "nix+http" | "nix+https" => {
+            // Stringify the URL and remove the nix+ prefix.
+            // We can't use `url.set_scheme(rest)`, as it disallows
+            // setting something http(s) that previously wasn't.
+            let new_url = Url::parse(url.to_string().strip_prefix("nix+").unwrap()).unwrap();
+
+            let mut nix_http_path_info_service =
+                NixHTTPPathInfoService::new(new_url, blob_service, directory_service);
+
+            let pairs = &url.query_pairs();
+            for (k, v) in pairs.into_iter() {
+                if k == "trusted-public-keys" {
+                    let pubkey_strs: Vec<_> = v.split_ascii_whitespace().collect();
+
+                    let mut pubkeys: Vec<narinfo::PubKey> = Vec::with_capacity(pubkey_strs.len());
+                    for pubkey_str in pubkey_strs {
+                        pubkeys.push(narinfo::PubKey::parse(pubkey_str).map_err(|e| {
                             Error::StorageError(format!("invalid public key: {e}"))
                         })?);
-                }
+                    }
 
-                nix_http_path_info_service.set_public_keys(pubkeys);
+                    nix_http_path_info_service.set_public_keys(pubkeys);
+                }
             }
-        }
 
-        Box::new(nix_http_path_info_service)
-    } 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 = PathInfoServiceClient::new(tvix_castore::tonic::channel_from_url(&url).await?);
-        Box::new(GRPCPathInfoService::from_client(client))
-    } else {
-        Err(Error::StorageError(format!(
+            Box::new(nix_http_path_info_service)
+        }
+        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 =
+                PathInfoServiceClient::new(tvix_castore::tonic::channel_from_url(&url).await?);
+            Box::new(GRPCPathInfoService::from_client(client))
+        }
+        _ => Err(Error::StorageError(format!(
             "unknown scheme: {}",
             url.scheme()
-        )))?
-    })
+        )))?,
+    };
+
+    Ok(path_info_service)
 }
 
 #[cfg(test)]