pub(crate) mod base; pub(crate) mod ssz; pub(crate) mod ssz_snappy; use self::base::{BaseInboundCodec, BaseOutboundCodec}; use self::ssz::{SSZInboundCodec, SSZOutboundCodec}; use self::ssz_snappy::{SSZSnappyInboundCodec, SSZSnappyOutboundCodec}; use crate::rpc::protocol::RPCError; use crate::rpc::{RPCErrorResponse, RPCRequest}; use libp2p::bytes::BytesMut; use tokio::codec::{Decoder, Encoder}; use types::EthSpec; // Known types of codecs pub enum InboundCodec { SSZSnappy(BaseInboundCodec, TSpec>), SSZ(BaseInboundCodec, TSpec>), } pub enum OutboundCodec { SSZSnappy(BaseOutboundCodec, TSpec>), SSZ(BaseOutboundCodec, TSpec>), } impl Encoder for InboundCodec { type Item = RPCErrorResponse; type Error = RPCError; fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> { match self { InboundCodec::SSZ(codec) => codec.encode(item, dst), InboundCodec::SSZSnappy(codec) => codec.encode(item, dst), } } } impl Decoder for InboundCodec { type Item = RPCRequest; type Error = RPCError; fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { match self { InboundCodec::SSZ(codec) => codec.decode(src), InboundCodec::SSZSnappy(codec) => codec.decode(src), } } } impl Encoder for OutboundCodec { type Item = RPCRequest; type Error = RPCError; fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> { match self { OutboundCodec::SSZ(codec) => codec.encode(item, dst), OutboundCodec::SSZSnappy(codec) => codec.encode(item, dst), } } } impl Decoder for OutboundCodec { type Item = RPCErrorResponse; type Error = RPCError; fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { match self { OutboundCodec::SSZ(codec) => codec.decode(src), OutboundCodec::SSZSnappy(codec) => codec.decode(src), } } }