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)
23 .map_err(|e| crate::Error::StorageError(format!("unable to parse url: {}", e)))?;
24
25 let blob_service_config = with_registry(®, || {
26 <DeserializeWithRegistry<Box<dyn ServiceBuilder<Output = dyn BlobService>>>>::try_from(url)
27 })?
28 .0;
29 let blob_service = blob_service_config
30 .build("anonymous", &CompositionContext::blank(®))
31 .await?;
32
33 Ok(blob_service)
34}
35
36#[cfg(test)]
37mod tests {
38 use super::from_addr;
39 use rstest::rstest;
40
41 #[rstest]
42 #[case::unsupported_scheme("http://foo.example/test", false)]
44 #[case::memory_valid("memory://", true)]
46 #[case::memory_invalid_host("memory://foo", false)]
48 #[case::memory_invalid_root_path("memory:///", false)]
50 #[case::memory_invalid_root_path_foo("memory:///foo", false)]
52 #[case::grpc_valid_unix_socket("grpc+unix:///path/to/somewhere", true)]
54 #[case::grpc_invalid_unix_socket_and_host("grpc+unix://host.example/path/to/somewhere", false)]
56 #[case::grpc_valid_ipv6_localhost_port_12345("grpc+http://[::1]:12345", true)]
58 #[case::grpc_valid_http_host_without_port("grpc+http://localhost", true)]
60 #[case::grpc_valid_https_host_without_port("grpc+https://localhost", true)]
62 #[case::grpc_invalid_has_path("grpc+http://localhost/some-path", false)]
64 #[case::objectstore_valid_memory("objectstore+memory:///", true)]
66 #[case::objectstore_valid_file("objectstore+file:///foo/bar", true)]
68 #[case::objectstore_valid_http_url("objectstore+https://localhost:8080/some-path", true)]
70 #[cfg_attr(
72 feature = "cloud",
73 case::objectstore_valid_s3_url("objectstore+s3://bucket/path", true)
74 )]
75 #[cfg_attr(
77 feature = "cloud",
78 case::objectstore_valid_gcs_url("objectstore+gs://bucket/path", true)
79 )]
80 #[tokio::test]
81 async fn test_from_addr_tokio(#[case] uri_str: &str, #[case] exp_succeed: bool) {
82 if exp_succeed {
83 from_addr(uri_str).await.expect("should succeed");
84 } else {
85 assert!(from_addr(uri_str).await.is_err(), "should fail");
86 }
87 }
88}