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 TtlExhausted,
24}
25
26pub trait Messaging {
27 fn address(&self) -> Address;
28
29 fn recv(&mut self) -> impl Future<Output = Result<Envelope, ErrorOf<RecvErrorKind>>> + Send;
30
31 fn close(&mut self) -> impl Future<Output = ()> + Send;
32
33 fn send(
34 &mut self,
35 envelope: Envelope,
36 ) -> impl Future<Output = Result<(), ErrorOf<SendErrorKind>>> + Send;
37
38 fn forward(
39 &mut self,
40 to: Address,
41 envelope: Envelope,
42 ) -> impl Future<Output = Result<(), ErrorOf<SendErrorKind>>> + Send;
43}
44
45impl_error_kind!(SendErrorKind);
46
47pub trait Tell: Messaging {
48 fn tell<M>(
49 &mut self,
50 to: Address,
51 message: M,
52 ) -> impl Future<Output = Result<(), ErrorOf<SendErrorKind>>> + Send
53 where
54 M: Message,
55 {
56 let info = EnvelopeHeader::to_address(to);
57 let envelope = Envelope::new(info, message);
58 self.send(envelope.into_erased())
59 }
60}
61
62impl<T> Tell for T where T: Messaging {}