1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#![warn(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
unreachable_pub
)]
use std::error::Error;
use std::fmt::Debug;
use futures_channel::mpsc::{SendError, Sender};
use futures_core::Stream;
use futures_sink::Sink;
use futures_util::{sink::SinkExt, stream::TryStreamExt};
pub trait Message: Debug {
fn payload(&self) -> &[u8];
fn topic(&self) -> &str;
fn key(&self) -> Option<&str> {
None
}
}
#[derive(Debug)]
pub struct AckMessage<M: Message> {
message: M,
acks_tx: Sender<M>,
}
impl<M: Message> AckMessage<M> {
pub fn new(message: M, acks_tx: Sender<M>) -> Self {
Self { message, acks_tx }
}
pub fn message(&self) -> &M {
&self.message
}
pub async fn ack(mut self) -> Result<(), SendError> {
self.acks_tx.send(self.message).await?;
Ok(())
}
}
pub trait Subscriber:
Stream<Item = Result<AckMessage<<Self as Subscriber>::Message>, <Self as Subscriber>::Error>>
+ Unpin
{
type Message: Message;
type Error: Error + Send + Sync + 'static;
}
pub trait Publisher:
Sink<<Self as Publisher>::Message, Error = <Self as Publisher>::Error> + Unpin
{
type Message: Message;
type Error: Error + Send + Sync + 'static;
}
pub trait AckHandler:
Stream<Item = Result<<Self as AckHandler>::Output, <Self as AckHandler>::Error>> + Unpin
{
type Output;
type Error: Error + Send + Sync + 'static;
}
pub trait Statuser {
type Error: Error + Send + Sync + 'static;
fn status(&self) -> Result<(), Self::Error>;
}
pub async fn noop_ack_handler<A: AckHandler<Output = ()>>(mut handler: A) -> Result<(), A::Error> {
while let Some(_) = handler.try_next().await? {}
Ok(())
}