refactor(tvix/store/pathinfo/from_addr): use match guards
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>
This commit is contained in:
		
							parent
							
								
									c0566985b0
								
							
						
					
					
						commit
						65b8359ff3
					
				
					 1 changed files with 59 additions and 56 deletions
				
			
		| 
						 | 
					@ -40,13 +40,15 @@ pub async fn from_addr(
 | 
				
			||||||
    let url =
 | 
					    let url =
 | 
				
			||||||
        Url::parse(uri).map_err(|e| Error::StorageError(format!("unable to parse url: {}", e)))?;
 | 
					        Url::parse(uri).map_err(|e| Error::StorageError(format!("unable to parse url: {}", e)))?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Ok(if url.scheme() == "memory" {
 | 
					    let path_info_service: Box<dyn PathInfoService> = 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::new(MemoryPathInfoService::new(blob_service, directory_service))
 | 
					            Box::new(MemoryPathInfoService::new(blob_service, directory_service))
 | 
				
			||||||
    } 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() {
 | 
				
			||||||
| 
						 | 
					@ -61,17 +63,15 @@ pub async fn from_addr(
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // TODO: expose other parameters as URL parameters?
 | 
					            // TODO: expose other parameters as URL parameters?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if url.path().is_empty() {
 | 
					            Box::new(if url.path().is_empty() {
 | 
				
			||||||
            return Ok(Box::new(
 | 
					 | 
				
			||||||
                SledPathInfoService::new_temporary(blob_service, directory_service)
 | 
					                SledPathInfoService::new_temporary(blob_service, directory_service)
 | 
				
			||||||
                    .map_err(|e| Error::StorageError(e.to_string()))?,
 | 
					                    .map_err(|e| Error::StorageError(e.to_string()))?
 | 
				
			||||||
            ));
 | 
					            } else {
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        return Ok(Box::new(
 | 
					 | 
				
			||||||
                SledPathInfoService::new(url.path(), blob_service, directory_service)
 | 
					                SledPathInfoService::new(url.path(), blob_service, directory_service)
 | 
				
			||||||
                .map_err(|e| Error::StorageError(e.to_string()))?,
 | 
					                    .map_err(|e| Error::StorageError(e.to_string()))?
 | 
				
			||||||
        ));
 | 
					            })
 | 
				
			||||||
    } else if url.scheme() == "nix+http" || url.scheme() == "nix+https" {
 | 
					        }
 | 
				
			||||||
 | 
					        "nix+http" | "nix+https" => {
 | 
				
			||||||
            // Stringify the URL and remove the nix+ prefix.
 | 
					            // Stringify the URL and remove the nix+ prefix.
 | 
				
			||||||
            // We can't use `url.set_scheme(rest)`, as it disallows
 | 
					            // We can't use `url.set_scheme(rest)`, as it disallows
 | 
				
			||||||
            // setting something http(s) that previously wasn't.
 | 
					            // setting something http(s) that previously wasn't.
 | 
				
			||||||
| 
						 | 
					@ -87,8 +87,7 @@ pub async fn from_addr(
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    let mut pubkeys: Vec<narinfo::PubKey> = Vec::with_capacity(pubkey_strs.len());
 | 
					                    let mut pubkeys: Vec<narinfo::PubKey> = Vec::with_capacity(pubkey_strs.len());
 | 
				
			||||||
                    for pubkey_str in pubkey_strs {
 | 
					                    for pubkey_str in pubkey_strs {
 | 
				
			||||||
                    pubkeys
 | 
					                        pubkeys.push(narinfo::PubKey::parse(pubkey_str).map_err(|e| {
 | 
				
			||||||
                        .push(narinfo::PubKey::parse(pubkey_str).map_err(|e| {
 | 
					 | 
				
			||||||
                            Error::StorageError(format!("invalid public key: {e}"))
 | 
					                            Error::StorageError(format!("invalid public key: {e}"))
 | 
				
			||||||
                        })?);
 | 
					                        })?);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
| 
						 | 
					@ -98,20 +97,24 @@ pub async fn from_addr(
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Box::new(nix_http_path_info_service)
 | 
					            Box::new(nix_http_path_info_service)
 | 
				
			||||||
    } 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.
 | 
				
			||||||
            // - In the case of non-unix sockets, there must be a host, but no path.
 | 
					            // - 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.
 | 
					            // Constructing the channel is handled by tvix_castore::channel::from_url.
 | 
				
			||||||
        let client = PathInfoServiceClient::new(tvix_castore::tonic::channel_from_url(&url).await?);
 | 
					            let client =
 | 
				
			||||||
 | 
					                PathInfoServiceClient::new(tvix_castore::tonic::channel_from_url(&url).await?);
 | 
				
			||||||
            Box::new(GRPCPathInfoService::from_client(client))
 | 
					            Box::new(GRPCPathInfoService::from_client(client))
 | 
				
			||||||
    } else {
 | 
					        }
 | 
				
			||||||
        Err(Error::StorageError(format!(
 | 
					        _ => Err(Error::StorageError(format!(
 | 
				
			||||||
            "unknown scheme: {}",
 | 
					            "unknown scheme: {}",
 | 
				
			||||||
            url.scheme()
 | 
					            url.scheme()
 | 
				
			||||||
        )))?
 | 
					        )))?,
 | 
				
			||||||
    })
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Ok(path_info_service)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[cfg(test)]
 | 
					#[cfg(test)]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue