use std::fmt::Debug;
use serde::Deserialize;
use serde::Serialize;
use super::AZDLS_SCHEME;
use super::backend::AzdlsBuilder;
#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(default)]
pub struct AzdlsConfig {
pub root: Option<String>,
pub filesystem: String,
pub endpoint: Option<String>,
pub account_name: Option<String>,
pub account_key: Option<String>,
pub client_secret: Option<String>,
pub tenant_id: Option<String>,
pub client_id: Option<String>,
pub sas_token: Option<String>,
pub authority_host: Option<String>,
pub enable_hns: bool,
}
impl Debug for AzdlsConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AzdlsConfig")
.field("root", &self.root)
.field("filesystem", &self.filesystem)
.field("endpoint", &self.endpoint)
.finish_non_exhaustive()
}
}
impl opendal_core::Configurator for AzdlsConfig {
type Builder = AzdlsBuilder;
fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
let mut map = uri.options().clone();
if let Some(authority) = uri.authority() {
map.insert("endpoint".to_string(), format!("https://{authority}"));
}
if let Some(account) = uri
.name()
.and_then(|host| host.split('.').next())
.filter(|account| !account.is_empty())
{
map.entry("account_name".to_string())
.or_insert_with(|| account.to_string());
}
if let Some(root) = uri.root() {
if let Some((filesystem, rest)) = root.split_once('/') {
if filesystem.is_empty() {
return Err(opendal_core::Error::new(
opendal_core::ErrorKind::ConfigInvalid,
"filesystem is required in uri path",
)
.with_context("service", AZDLS_SCHEME));
}
map.insert("filesystem".to_string(), filesystem.to_string());
if !rest.is_empty() {
map.insert("root".to_string(), rest.to_string());
}
} else if !root.is_empty() {
map.insert("filesystem".to_string(), root.to_string());
}
}
if !map.contains_key("filesystem") {
return Err(opendal_core::Error::new(
opendal_core::ErrorKind::ConfigInvalid,
"filesystem is required",
)
.with_context("service", AZDLS_SCHEME));
}
Self::from_iter(map)
}
#[allow(deprecated)]
fn into_builder(self) -> Self::Builder {
AzdlsBuilder { config: self }
}
}
#[cfg(test)]
mod tests {
use super::*;
use opendal_core::Configurator;
use opendal_core::OperatorUri;
#[test]
fn from_uri_sets_endpoint_filesystem_root_and_account() {
let uri = OperatorUri::new(
"azdls://account.dfs.core.windows.net/fs/data/2024",
Vec::<(String, String)>::new(),
)
.unwrap();
let cfg = AzdlsConfig::from_uri(&uri).unwrap();
assert_eq!(
cfg.endpoint.as_deref(),
Some("https://account.dfs.core.windows.net")
);
assert_eq!(cfg.filesystem, "fs".to_string());
assert_eq!(cfg.root.as_deref(), Some("data/2024"));
assert_eq!(cfg.account_name.as_deref(), Some("account"));
}
#[test]
fn from_uri_accepts_filesystem_from_query() {
let uri = OperatorUri::new(
"azdls://account.dfs.core.windows.net",
vec![("filesystem".to_string(), "logs".to_string())],
)
.unwrap();
let cfg = AzdlsConfig::from_uri(&uri).unwrap();
assert_eq!(cfg.filesystem, "logs".to_string());
}
}