mm1_core/context/
messaging.rs1use mm1_address::address::Address;
2use mm1_common::errors::error_of::ErrorOf;
3use mm1_common::impl_error_kind;
4use mm1_proto::{Message, message};
5
6use crate::envelope::{Envelope, EnvelopeHeader};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[message(base_path = ::mm1_proto)]
10pub enum RecvErrorKind {
11 Closed,
12}
13
14impl_error_kind!(RecvErrorKind);
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17#[message(base_path = ::mm1_proto)]
18pub enum SendErrorKind {
19 InternalError,
20 NotFound,
21 Closed,
22 Full,
23}
24
25pub trait Messaging {
26 fn address(&self) -> Address;
27
28 fn recv(&mut self) -> impl Future<Output = Result<Envelope, ErrorOf<RecvErrorKind>>> + Send;
29
30 fn close(&mut self) -> impl Future<Output = ()> + Send;
31
32 fn send(
33 &mut self,
34 envelope: Envelope,
35 ) -> impl Future<Output = Result<(), ErrorOf<SendErrorKind>>> + Send;
36
37 fn forward(
38 &mut self,
39 to: Address,
40 envelope: Envelope,
41 ) -> impl Future<Output = Result<(), ErrorOf<SendErrorKind>>> + Send;
42}
43
44impl_error_kind!(SendErrorKind);
45
46pub trait Tell: Messaging {
47 fn tell<M>(
48 &mut self,
49 to: Address,
50 message: M,
51 ) -> impl Future<Output = Result<(), ErrorOf<SendErrorKind>>> + Send
52 where
53 M: Message,
54 {
55 let info = EnvelopeHeader::to_address(to);
56 let envelope = Envelope::new(info, message);
57 self.send(envelope.into_erased())
58 }
59}
60
61impl<T> Tell for T where T: Messaging {}