snix_castore/blobservice/
from_addr.rs1use std::sync::Arc;
2
3use url::Url;
4
5use crate::composition::{
6 CompositionContext, DeserializeWithRegistry, REG, ServiceBuilder, with_registry,
7};
8
9use super::BlobService;
10
11pub async fn from_addr(
20 uri: &str,
21) -> Result<Arc<dyn BlobService>, Box<dyn std::error::Error + Send + Sync>> {
22 let url = Url::parse(uri).map_err(|e| format!("unable to parse url: {e}"))?;
23
24 let blob_service_config = with_registry(®, || {
25 <DeserializeWithRegistry<Box<dyn ServiceBuilder<Output = dyn BlobService>>>>::try_from(url)
26 })?
27 .0;
28 let blob_service = blob_service_config
29 .build("anonymous", &CompositionContext::blank(®))
30 .await?;
31
32 Ok(blob_service)
33}
34
35#[cfg(test)]
36mod tests {
37 use super::from_addr;
38 use rstest::rstest;
39
40 #[rstest]
41 #[case::unsupported_scheme("http://foo.example/test", false)]
43 #[case::memory_valid("memory:", true)]
45 #[case::memory_invalid_host("memory://foo", false)]
47 #[case::memory_invalid_root_path("memory:/", false)]
49 #[case::memory_invalid_root_path_foo("memory:/foo", false)]
51 #[case::grpc_valid_unix_socket("grpc+unix:/path/to/somewhere", true)]
53 #[case::grpc_invalid_unix_socket_and_authority("grpc+unix:///path/to/somewhere", false)]
55 #[case::grpc_invalid_unix_socket_and_host("grpc+unix://host.example/path/to/somewhere", false)]
57 #[case::grpc_valid_ipv6_localhost_port_12345("grpc+http://[::1]:12345", true)]
59 #[case::grpc_valid_http_host_without_port("grpc+http://localhost", true)]
61 #[case::grpc_valid_https_host_without_port("grpc+https://localhost", true)]
63 #[case::grpc_invalid_has_path("grpc+http://localhost/some-path", false)]
65 #[case::objectstore_valid_memory("objectstore+memory:///", true)]
67 #[case::objectstore_valid_file("objectstore+file:///foo/bar", true)]
69 #[case::objectstore_valid_http_url("objectstore+https://localhost:8080/some-path", true)]
71 #[cfg_attr(
73 feature = "cloud",
74 case::objectstore_valid_s3_url("objectstore+s3://bucket/path", true)
75 )]
76 #[cfg_attr(
78 feature = "cloud",
79 case::objectstore_valid_gcs_url("objectstore+gs://bucket/path", true)
80 )]
81 #[tokio::test]
82 async fn test_from_addr_tokio(#[case] uri_str: &str, #[case] exp_succeed: bool) {
83 if exp_succeed {
84 from_addr(uri_str).await.expect("should succeed");
85 } else {
86 assert!(from_addr(uri_str).await.is_err(), "should fail");
87 }
88 }
89}