1pub mod gossipsub;
2pub mod postcard;
3pub mod request_response;
4
5use crate::gossipsub::messages::GossipTopicTag;
6use libp2p::request_response as libp2p_request_response;
7
8use std::{
9 borrow::Cow,
10 io,
11};
12
13pub trait Encoder: Send {
14 fn into_bytes(self) -> Vec<u8>;
16}
17
18pub trait Encode<T: ?Sized> {
24 type Error;
25 type Encoder<'a>: Encoder
27 where
28 T: 'a;
29
30 fn encode<'a>(&self, t: &'a T) -> Result<Self::Encoder<'a>, Self::Error>;
32}
33
34pub trait Decode<T> {
36 type Error;
37 fn decode(&self, bytes: &[u8]) -> Result<T, Self::Error>;
39}
40
41impl Encoder for Cow<'_, [u8]> {
42 fn into_bytes(self) -> Vec<u8> {
43 self.into_owned()
44 }
45}
46
47pub trait GossipsubCodec {
49 type RequestMessage;
50 type ResponseMessage;
51
52 fn encode(&self, data: Self::RequestMessage) -> Result<Vec<u8>, io::Error>;
53
54 fn decode(
55 &self,
56 encoded_data: &[u8],
57 gossipsub_topic: GossipTopicTag,
58 ) -> Result<Self::ResponseMessage, io::Error>;
59}
60
61pub trait RequestResponseProtocols: libp2p_request_response::Codec {
62 fn get_req_res_protocols(
65 &self,
66 ) -> impl Iterator<Item = <Self as libp2p_request_response::Codec>::Protocol>;
67}