snix_castore/directoryservice/
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::DirectoryService;
10
11pub async fn from_addr(
27 uri: &str,
28) -> Result<Arc<dyn DirectoryService>, Box<dyn std::error::Error + Send + Sync>> {
29 #[allow(unused_mut)]
30 let mut url = Url::parse(uri)
31 .map_err(|e| crate::Error::StorageError(format!("unable to parse url: {}", e)))?;
32
33 let directory_service_config = with_registry(®, || {
34 <DeserializeWithRegistry<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>>>::try_from(
35 url,
36 )
37 })?
38 .0;
39 let directory_service = directory_service_config
40 .build("anonymous", &CompositionContext::blank(®))
41 .await?;
42
43 Ok(directory_service)
44}
45
46#[cfg(test)]
47mod tests {
48 use std::sync::LazyLock;
49
50 use super::from_addr;
51 use rstest::rstest;
52 use tempfile::TempDir;
53
54 static TMPDIR_REDB_1: LazyLock<TempDir> = LazyLock::new(|| TempDir::new().unwrap());
55 static TMPDIR_REDB_2: LazyLock<TempDir> = LazyLock::new(|| TempDir::new().unwrap());
56
57 #[rstest]
58 #[case::unsupported_scheme("http://foo.example/test", false)]
60 #[case::memory_valid("memory://", true)]
62 #[case::memory_invalid_host("memory://foo", false)]
64 #[case::memory_invalid_root_path("memory:///", false)]
66 #[case::memory_invalid_root_path_foo("memory:///foo", false)]
68 #[case::redb_valid_temporary("redb://", true)]
70 #[case::redb_invalid_root("redb:///", false)]
72 #[case::redb_invalid_host("redb://foo.example", false)]
74 #[case::redb_valid_path(&format!("redb://{}", &TMPDIR_REDB_1.path().join("foo").to_str().unwrap()), true)]
76 #[case::redb_invalid_host_with_valid_path(&format!("redb://foo.example{}", &TMPDIR_REDB_2.path().join("bar").to_str().unwrap()), false)]
78 #[case::grpc_valid_unix_socket("grpc+unix:///path/to/somewhere", true)]
80 #[case::grpc_invalid_unix_socket_and_host("grpc+unix://host.example/path/to/somewhere", false)]
82 #[case::grpc_valid_ipv6_localhost_port_12345("grpc+http://[::1]:12345", true)]
84 #[case::grpc_valid_http_host_without_port("grpc+http://localhost", true)]
86 #[case::grpc_valid_https_host_without_port("grpc+https://localhost", true)]
88 #[case::grpc_invalid_host_and_path("grpc+http://localhost/some-path", false)]
90 #[cfg_attr(
92 feature = "xp-composition-url-refs",
93 case::anonymous_url_composition("cache://?near=memory://&far=memory://", true)
94 )]
95 #[cfg_attr(
97 not(feature = "xp-composition-url-refs"),
98 case::anonymous_url_composition("cache://?near=memory://&far=memory://", false)
99 )]
100 #[cfg_attr(
102 all(feature = "cloud", feature = "integration"),
103 case::bigtable_valid_url(
104 "bigtable://instance-1?project_id=project-1&table_name=table-1&family_name=cf1",
105 true
106 )
107 )]
108 #[cfg_attr(
110 all(feature = "cloud", feature = "integration"),
111 case::bigtable_valid_url(
112 "bigtable://instance-1?project_id=project-1&table_name=table-1&family_name=cf1&channel_size=10&timeout=10",
113 true
114 )
115 )]
116 #[cfg_attr(
118 all(feature = "cloud", feature = "integration"),
119 case::bigtable_invalid_url("bigtable://instance-1", false)
120 )]
121 #[tokio::test]
122 async fn test_from_addr_tokio(#[case] uri_str: &str, #[case] exp_succeed: bool) {
123 if exp_succeed {
124 from_addr(uri_str).await.expect("should succeed");
125 } else {
126 assert!(from_addr(uri_str).await.is_err(), "should fail");
127 }
128 }
129}