Skip to main content

cortexfs_channels/
adapter.rs

1use std::future::Future;
2use std::pin::Pin;
3
4use futures_core::Stream;
5
6use crate::{
7    ChannelCapabilities, ChannelError, ChannelHealth, ChannelId, InboundMessage, OutboundMessage,
8};
9
10/// Owned asynchronous inbound stream returned by an adapter.
11pub type ChannelStream = Pin<Box<dyn Stream<Item = Result<InboundMessage, ChannelError>> + Send>>;
12
13/// Runtime-neutral future used by object-safe channel adapters.
14pub type ChannelFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, ChannelError>> + Send + 'a>>;
15
16/// Provider-neutral acknowledgement for one outbound delivery.
17#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
18pub struct DeliveryReceipt {
19    pub channel: ChannelId,
20    pub message_id: String,
21    pub target: crate::MessageTarget,
22    pub timestamp_ms: Option<u64>,
23}
24
25/// Object-safe contract implemented by Telegram, Slack, Discord, or custom adapters.
26pub trait ChannelAdapter: Send + Sync {
27    fn id(&self) -> ChannelId;
28    fn capabilities(&self) -> ChannelCapabilities;
29    fn listen(&self) -> Result<ChannelStream, ChannelError>;
30    fn send(&self, message: OutboundMessage) -> ChannelFuture<'_, DeliveryReceipt>;
31    fn health(&self) -> ChannelFuture<'_, ChannelHealth> {
32        Box::pin(async { Ok(ChannelHealth::ready()) })
33    }
34}