use std::fmt::Debug;
use std::sync::Arc;
use async_trait::async_trait;
use sqlx::PgPool;
use super::error::AuthzError;
#[async_trait]
pub trait RoleDirectory: Send + Sync + Debug {
async fn unknown_roles(&self, candidates: &[String]) -> Result<Vec<String>, AuthzError>;
}
pub type SharedRoleDirectory = Arc<dyn RoleDirectory>;
#[derive(Debug, Clone, Copy)]
pub struct RoleDirectoryRegistration {
pub factory: fn(Arc<PgPool>) -> SharedRoleDirectory,
}
inventory::collect!(RoleDirectoryRegistration);
#[must_use]
pub fn discover_role_directory(pool: &Arc<PgPool>) -> Option<SharedRoleDirectory> {
inventory::iter::<RoleDirectoryRegistration>()
.next()
.map(|reg| (reg.factory)(Arc::clone(pool)))
}
#[macro_export]
macro_rules! register_role_directory {
($factory:expr) => {
::inventory::submit! {
$crate::authz::RoleDirectoryRegistration {
factory: $factory,
}
}
};
}