about summary refs log tree commit diff
path: root/tvix/store/src/blobservice/from_addr.rs
blob: 2e0a30697d75c4f4747c67b58ab03cc19b65a355 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::sync::Arc;
use url::Url;

use super::{BlobService, GRPCBlobService, MemoryBlobService, SledBlobService};

/// Constructs a new instance of a [BlobService] from an URI.
///
/// The following schemes are supported by the following services:
/// - `memory://` ([MemoryBlobService])
/// - `sled://` ([SledBlobService])
/// - `grpc+*://` ([GRPCBlobService])
///
/// See their `from_url` methods for more details about their syntax.
pub fn from_addr(uri: &str) -> Result<Arc<dyn BlobService>, crate::Error> {
    let url = Url::parse(uri)
        .map_err(|e| crate::Error::StorageError(format!("unable to parse url: {}", e)))?;

    Ok(if url.scheme() == "memory" {
        Arc::new(MemoryBlobService::from_url(&url)?)
    } else if url.scheme() == "sled" {
        Arc::new(SledBlobService::from_url(&url)?)
    } else if url.scheme().starts_with("grpc+") {
        Arc::new(GRPCBlobService::from_url(&url)?)
    } else {
        Err(crate::Error::StorageError(format!(
            "unknown scheme: {}",
            url.scheme()
        )))?
    })
}