use crate::IdentityBox;
use async_trait::async_trait;
use std::{fmt::Debug, sync::Arc};
#[derive(Debug, thiserror::Error)]
pub enum IdentityResolverError {
#[error("Identity not found")]
NotFound,
#[error("Resolve Identitiy failed")]
Other(#[from] anyhow::Error),
}
#[async_trait]
pub trait IdentityResolver: Debug {
async fn resolve(&self, identity: &str) -> Result<IdentityBox, IdentityResolverError>;
fn boxed(self) -> IdentityResolverBox
where
Self: Sized + Clone + Send + Sync + 'static,
{
IdentityResolverBox::new(self)
}
}
#[derive(Debug)]
pub struct IdentityResolverBox {
resolver: Arc<dyn IdentityResolver + Send + Sync + 'static>,
}
impl IdentityResolverBox {
pub fn new<R: IdentityResolver + Clone + Send + Sync + 'static>(resolver: R) -> Self {
Self { resolver: Arc::new(resolver) }
}
}
#[async_trait]
impl IdentityResolver for IdentityResolverBox {
async fn resolve(&self, identity: &str) -> Result<IdentityBox, IdentityResolverError> {
self.resolver.resolve(identity).await
}
}
impl Clone for IdentityResolverBox {
fn clone(&self) -> Self {
Self { resolver: self.resolver.clone() }
}
}