blob: a50644fe103e7c6168955622de6f8e5b9174fbb0 (
plain)
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
|
use anyhow::Result;
use futures::Stream;
use futures::StreamExt;
use crate::gossip::GossipMessage;
pub struct GossipDeserializer<ErrStrategy = LogStrategy>
where ErrStrategy: GossipDeserializerErrorStrategy
{
strategy: std::marker::PhantomData<ErrStrategy>,
}
impl<ErrStrategy> GossipDeserializer<ErrStrategy>
where ErrStrategy: GossipDeserializerErrorStrategy
{
pub fn new() -> Self {
Self {
strategy: std::marker::PhantomData,
}
}
pub fn run<S>(mut self, input: S) -> impl Stream<Item = GossipMessage>
where S: Stream<Item = ipfs::PubsubMessage>
{
input.filter_map(|message| async move {
log::trace!("Received gossip message");
match serde_json::from_slice(&message.data).map_err(anyhow::Error::from) {
Ok(m) => Some(m),
Err(e) => {
ErrStrategy::handle_error(e);
None
}
}
})
}
}
pub trait GossipDeserializerErrorStrategy {
fn handle_error(err: anyhow::Error);
}
pub struct LogStrategy;
impl GossipDeserializerErrorStrategy for LogStrategy {
fn handle_error(err: anyhow::Error) {
log::trace!("Error: {}", err);
}
}
pub struct IgnoreStrategy;
impl GossipDeserializerErrorStrategy for IgnoreStrategy {
fn handle_error(_: anyhow::Error) {
()
}
}
|