snix_castore/blobservice/
from_addr.rs

1use std::sync::Arc;
2
3use url::Url;
4
5use crate::composition::{
6    CompositionContext, DeserializeWithRegistry, REG, ServiceBuilder, with_registry,
7};
8
9use super::BlobService;
10
11/// Constructs a new instance of a [BlobService] from an URI.
12///
13/// The following schemes are supported by the following services:
14/// - `memory://` ([super::MemoryBlobService])
15/// - `grpc+*://` ([super::GRPCBlobService])
16/// - `objectstore+*://` ([super::ObjectStoreBlobService])
17///
18/// See their `from_url` methods for more details about their syntax.
19pub 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(&REG, || {
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(&REG))
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    /// This uses an unsupported scheme.
42    #[case::unsupported_scheme("http://foo.example/test", false)]
43    /// This correctly sets the scheme, and doesn't set a path.
44    #[case::memory_valid("memory:", true)]
45    /// This sets a memory url host to `foo`
46    #[case::memory_invalid_host("memory://foo", false)]
47    /// This sets a memory url path to "/", which is invalid.
48    #[case::memory_invalid_root_path("memory:/", false)]
49    /// This sets a memory url path to "/foo", which is invalid.
50    #[case::memory_invalid_root_path_foo("memory:/foo", false)]
51    /// Correct scheme to connect to a unix socket.
52    #[case::grpc_valid_unix_socket("grpc+unix:/path/to/somewhere", true)]
53    /// Correct scheme for unix socket, but setting authority, which is invalid.
54    #[case::grpc_invalid_unix_socket_and_authority("grpc+unix:///path/to/somewhere", false)]
55    /// Correct scheme for unix socket, but setting a host too, which is invalid.
56    #[case::grpc_invalid_unix_socket_and_host("grpc+unix://host.example/path/to/somewhere", false)]
57    /// Correct scheme to connect to localhost, with port 12345
58    #[case::grpc_valid_ipv6_localhost_port_12345("grpc+http://[::1]:12345", true)]
59    /// Correct scheme to connect to localhost over http, without specifying a port.
60    #[case::grpc_valid_http_host_without_port("grpc+http://localhost", true)]
61    /// Correct scheme to connect to localhost over http, without specifying a port.
62    #[case::grpc_valid_https_host_without_port("grpc+https://localhost", true)]
63    /// Correct scheme to connect to localhost over http, but with additional path, which is invalid.
64    #[case::grpc_invalid_has_path("grpc+http://localhost/some-path", false)]
65    /// An example for object store (InMemory)
66    #[case::objectstore_valid_memory("objectstore+memory:///", true)]
67    /// An example for object store (LocalFileSystem)
68    #[case::objectstore_valid_file("objectstore+file:///foo/bar", true)]
69    // An example for object store (HTTP / WebDAV)
70    #[case::objectstore_valid_http_url("objectstore+https://localhost:8080/some-path", true)]
71    /// An example for object store (S3)
72    #[cfg_attr(
73        feature = "cloud",
74        case::objectstore_valid_s3_url("objectstore+s3://bucket/path", true)
75    )]
76    /// An example for object store (GCS)
77    #[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}