refactor(tvix/castore/directory/from_addr): use match guards

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>
This commit is contained in:
Florian Klink 2024-03-19 12:11:19 +02:00 committed by clbot
parent 7deadd50d5
commit c0566985b0

View file

@ -22,13 +22,15 @@ pub async fn from_addr(uri: &str) -> Result<Box<dyn DirectoryService>, crate::Er
let url = Url::parse(uri) let url = Url::parse(uri)
.map_err(|e| crate::Error::StorageError(format!("unable to parse url: {}", e)))?; .map_err(|e| crate::Error::StorageError(format!("unable to parse url: {}", e)))?;
Ok(if url.scheme() == "memory" { let directory_service: Box<dyn DirectoryService> = match url.scheme() {
"memory" => {
// memory doesn't support host or path in the URL. // memory doesn't support host or path in the URL.
if url.has_host() || !url.path().is_empty() { if url.has_host() || !url.path().is_empty() {
return Err(Error::StorageError("invalid url".to_string())); return Err(Error::StorageError("invalid url".to_string()));
} }
Box::<MemoryDirectoryService>::default() Box::<MemoryDirectoryService>::default()
} else if url.scheme() == "sled" { }
"sled" => {
// sled doesn't support host, and a path can be provided (otherwise // sled doesn't support host, and a path can be provided (otherwise
// it'll live in memory only). // it'll live in memory only).
if url.has_host() { if url.has_host() {
@ -43,17 +45,15 @@ pub async fn from_addr(uri: &str) -> Result<Box<dyn DirectoryService>, crate::Er
// TODO: expose compression and other parameters as URL parameters? // TODO: expose compression and other parameters as URL parameters?
if url.path().is_empty() { Box::new(if url.path().is_empty() {
return Ok(Box::new(
SledDirectoryService::new_temporary() SledDirectoryService::new_temporary()
.map_err(|e| Error::StorageError(e.to_string()))?, .map_err(|e| Error::StorageError(e.to_string()))?
)); } else {
}
return Ok(Box::new(
SledDirectoryService::new(url.path()) SledDirectoryService::new(url.path())
.map_err(|e| Error::StorageError(e.to_string()))?, .map_err(|e| Error::StorageError(e.to_string()))?
)); })
} else if url.scheme().starts_with("grpc+") { }
scheme if scheme.starts_with("grpc+") => {
// schemes starting with grpc+ go to the GRPCPathInfoService. // schemes starting with grpc+ go to the GRPCPathInfoService.
// That's normally grpc+unix for unix sockets, and grpc+http(s) for the HTTP counterparts. // 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 unix sockets, there must be a path, but may not be a host.
@ -61,12 +61,15 @@ pub async fn from_addr(uri: &str) -> Result<Box<dyn DirectoryService>, crate::Er
// Constructing the channel is handled by tvix_castore::channel::from_url. // Constructing the channel is handled by tvix_castore::channel::from_url.
let client = DirectoryServiceClient::new(crate::tonic::channel_from_url(&url).await?); let client = DirectoryServiceClient::new(crate::tonic::channel_from_url(&url).await?);
Box::new(GRPCDirectoryService::from_client(client)) Box::new(GRPCDirectoryService::from_client(client))
} else { }
Err(crate::Error::StorageError(format!( _ => {
return Err(crate::Error::StorageError(format!(
"unknown scheme: {}", "unknown scheme: {}",
url.scheme() url.scheme()
)))? )))
}) }
};
Ok(directory_service)
} }
#[cfg(test)] #[cfg(test)]