lance_namespace/
connect.rs

1//! Connect functionality for Lance Namespace implementations.
2
3use std::collections::HashMap;
4use std::sync::Arc;
5use thiserror::Error;
6
7use crate::dir::DirectoryNamespace;
8use crate::namespace::LanceNamespace;
9use crate::rest::RestNamespace;
10
11/// Error type for connection-related operations
12#[derive(Debug, Error)]
13pub enum ConnectError {
14    #[error("Unknown implementation: {0}")]
15    UnknownImpl(String),
16
17    #[error("Failed to construct implementation: {0}")]
18    ConstructionError(String),
19
20    #[error("Missing required property: {0}")]
21    MissingProperty(String),
22
23    #[error("Other error: {0}")]
24    Other(String),
25}
26
27/// Connect to a Lance namespace implementation.
28///
29/// This function creates a connection to a Lance namespace backend based on
30/// the specified implementation type and configuration properties.
31///
32/// # Arguments
33///
34/// * `impl_name` - Implementation identifier. Currently supported:
35///   - "rest": REST API implementation (when available)
36///   - "dir": Directory-based implementation (when available)
37///
38/// * `properties` - Configuration properties specific to the implementation.
39///   Common properties might include:
40///   - "url": Base URL for REST implementations
41///   - "path": Directory path for file-based implementations
42///   - "auth_token": Authentication token
43///
44/// # Returns
45///
46/// Returns a boxed trait object implementing the `LanceNamespace` trait.
47///
48/// # Examples
49///
50/// ```no_run
51/// use lance_namespace::connect;
52/// use std::collections::HashMap;
53///
54/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
55/// let mut props = HashMap::new();
56/// props.insert("url".to_string(), "http://localhost:8080".to_string());
57/// let namespace = connect("rest", props).await?;
58/// # Ok(())
59/// # }
60/// ```
61pub async fn connect(
62    impl_name: &str,
63    properties: HashMap<String, String>,
64) -> Result<Arc<dyn LanceNamespace>, ConnectError> {
65    // Native implementations will be added here as they are created
66    match impl_name {
67        "rest" => {
68            // Create REST implementation
69            Ok(Arc::new(RestNamespace::new(properties)))
70        }
71        "dir" => {
72            // Create directory implementation
73            DirectoryNamespace::new(properties)
74                .map(|ns| Arc::new(ns) as Arc<dyn LanceNamespace>)
75                .map_err(|e| ConnectError::ConstructionError(e.to_string()))
76        }
77        _ => Err(ConnectError::UnknownImpl(format!(
78            "Implementation '{}' is not yet available",
79            impl_name
80        ))),
81    }
82}