use crate::error::Result;
use crate::value::{ConfigMap, ConfigValue};
use async_trait::async_trait;
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ProviderId(pub String);
impl ProviderId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for ProviderId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for ProviderId {
fn from(value: &str) -> Self {
Self::new(value)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ProviderCapability {
pub bulk: bool,
pub versioned: bool,
pub local: bool,
}
#[derive(Clone, Debug)]
pub struct ProviderMeta {
pub id: ProviderId,
pub name: String,
pub capabilities: ProviderCapability,
}
#[async_trait]
pub trait Provider: Send + Sync {
fn meta(&self) -> &ProviderMeta;
async fn get(&self, key: &str) -> Result<ConfigValue>;
async fn get_many(&self, keys: &[String]) -> Result<ConfigMap> {
let mut out = ConfigMap::new();
for key in keys {
match self.get(key).await {
Ok(v) => {
out.insert(key.clone(), v);
}
Err(crate::error::Error::NotFound { .. }) => {}
Err(e) => return Err(e),
}
}
Ok(out)
}
async fn health(&self) -> Result<()> {
Ok(())
}
}