use alloc::vec::Vec;
use core::convert::Infallible;
use future_form::{FutureForm, Local, Sendable, future_form};
use sedimentree_core::id::SedimentreeId;
use subduction_core::peer::id::PeerId;
pub trait EphemeralPolicy<K: FutureForm + ?Sized> {
type SubscribeDisallowed: core::error::Error;
type PublishDisallowed: core::error::Error;
fn authorize_subscribe(
&self,
peer: PeerId,
id: SedimentreeId,
) -> K::Future<'_, Result<(), Self::SubscribeDisallowed>>;
fn authorize_publish(
&self,
peer: PeerId,
id: SedimentreeId,
) -> K::Future<'_, Result<(), Self::PublishDisallowed>>;
fn filter_authorized_subscribers(
&self,
id: SedimentreeId,
peers: Vec<PeerId>,
) -> K::Future<'_, Vec<PeerId>>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct OpenEphemeralPolicy;
#[future_form(Sendable, Local)]
impl<K: FutureForm> EphemeralPolicy<K> for OpenEphemeralPolicy {
type SubscribeDisallowed = Infallible;
type PublishDisallowed = Infallible;
fn authorize_subscribe(
&self,
_peer: PeerId,
_id: SedimentreeId,
) -> K::Future<'_, Result<(), Self::SubscribeDisallowed>> {
K::from_future(async { Ok(()) })
}
fn authorize_publish(
&self,
_peer: PeerId,
_id: SedimentreeId,
) -> K::Future<'_, Result<(), Self::PublishDisallowed>> {
K::from_future(async { Ok(()) })
}
fn filter_authorized_subscribers(
&self,
_id: SedimentreeId,
peers: Vec<PeerId>,
) -> K::Future<'_, Vec<PeerId>> {
K::from_future(async { peers })
}
}