ate_comms/protocol/
version.rs1use serde::*;
2use tokio::io::AsyncRead;
3use tokio::io::AsyncWrite;
4
5use super::MessageProtocolApi;
6
7#[repr(u16)]
9#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
10pub enum MessageProtocolVersion
11{
12 V1 = 1,
13 V2 = 2,
14 V3 = 3,
15}
16
17impl Default
18for MessageProtocolVersion
19{
20 fn default() -> Self {
21 MessageProtocolVersion::V3
22 }
23}
24
25impl MessageProtocolVersion
26{
27 pub fn min(&self, other: MessageProtocolVersion) -> MessageProtocolVersion {
28 let first = *self as u16;
29 let second = other as u16;
30 let min = first.min(second);
31
32 if first == min {
33 *self
34 } else {
35 other
36 }
37 }
38
39 pub fn upgrade(&self, mut proto: Box<dyn MessageProtocolApi + Send + Sync + 'static>) -> Box<dyn MessageProtocolApi + Send + Sync + 'static> {
40 let rx = proto.take_rx();
41 let tx = proto.take_tx();
42 self.create(rx, tx)
43 }
44
45 pub fn create(&self, rx: Option<Box<dyn AsyncRead + Send + Sync + Unpin + 'static>>, tx: Option<Box<dyn AsyncWrite + Send + Sync + Unpin + 'static>>) -> Box<dyn MessageProtocolApi + Send + Sync + 'static>
46 {
47 match self {
48 MessageProtocolVersion::V1 => {
49 Box::new(super::v1::MessageProtocol::new(rx, tx))
50 }
51 MessageProtocolVersion::V2 => {
52 Box::new(super::v2::MessageProtocol::new(rx, tx))
53 }
54 MessageProtocolVersion::V3 => {
55 Box::new(super::v3::MessageProtocol::new(rx, tx))
56 }
57 }
58 }
59}