blob: dcd5082799bf235035c1b47fc2413b94c537a845 (
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
56
57
|
use std::sync::Arc;
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>(self, input: S) -> impl Stream<Item = (ipfs::PeerId, GossipMessage)>
where S: Stream<Item = Arc<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((message.source, 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) {
()
}
}
|