volans_request/
codec.rs

1#[cfg(feature = "json")]
2mod json;
3
4#[cfg(feature = "json")]
5pub use json::JsonCodec;
6
7#[cfg(feature = "protobuf")]
8mod protobuf;
9
10#[cfg(feature = "protobuf")]
11pub use protobuf::ProtobufCodec;
12
13use std::io;
14
15use async_trait::async_trait;
16use futures::{AsyncRead, AsyncWrite};
17
18#[async_trait]
19pub trait Codec {
20    type Protocol: AsRef<str> + Send + Clone;
21    type Request: Send;
22    type Response: Send;
23
24    async fn read_request<T>(
25        &mut self,
26        protocol: &Self::Protocol,
27        io: &mut T,
28    ) -> io::Result<Self::Request>
29    where
30        T: AsyncRead + Unpin + Send;
31    async fn read_response<T>(
32        &mut self,
33        protocol: &Self::Protocol,
34        io: &mut T,
35    ) -> io::Result<Self::Response>
36    where
37        T: AsyncRead + Unpin + Send;
38    async fn write_request<T>(
39        &mut self,
40        protocol: &Self::Protocol,
41        io: &mut T,
42        request: Self::Request,
43    ) -> io::Result<()>
44    where
45        T: AsyncWrite + Unpin + Send;
46    async fn write_response<T>(
47        &mut self,
48        protocol: &Self::Protocol,
49        io: &mut T,
50        response: Self::Response,
51    ) -> io::Result<()>
52    where
53        T: AsyncWrite + Unpin + Send;
54}