use std::pin::Pin;
use std::sync::Arc;
pub trait BlockerResolver: Send + Sync + 'static {
fn is_open(&self, blocker: &str) -> impl Future<Output = Result<bool, BlockerError>> + Send;
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum BlockerError {
#[error("the blocker {blocker} cannot be resolved: {reason}")]
Unavailable {
blocker: String,
reason: String,
},
}
type IsOpen<'a> = Pin<Box<dyn Future<Output = Result<bool, BlockerError>> + Send + 'a>>;
trait BlockerResolverImpl: Send + Sync {
fn is_open<'a>(&'a self, blocker: &'a str) -> IsOpen<'a>;
}
impl<T: BlockerResolver> BlockerResolverImpl for T {
fn is_open<'a>(&'a self, blocker: &'a str) -> IsOpen<'a> {
Box::pin(BlockerResolver::is_open(self, blocker))
}
}
#[derive(Clone)]
pub struct AnyBlockerResolver(Arc<dyn BlockerResolverImpl>);
impl AnyBlockerResolver {
#[must_use]
pub fn new(resolver: impl BlockerResolver) -> Self {
Self(Arc::new(resolver))
}
pub fn is_open<'a>(
&'a self,
blocker: &'a str,
) -> impl Future<Output = Result<bool, BlockerError>> + Send + 'a {
self.0.is_open(blocker)
}
}
impl std::fmt::Debug for AnyBlockerResolver {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AnyBlockerResolver").finish_non_exhaustive()
}
}