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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/// Unknown delegate version
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, thiserror::Error)]
#[error("V{0} is not a valid delegate version")]
pub struct UnknownDelegateVersion(u8);

/// Delegate version
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
// #[cfg_attr(
//   feature = "rkyv",
//   derive(::rkyv::Serialize, ::rkyv::Deserialize, ::rkyv::Archive)
// )]
// #[cfg_attr(feature = "rkyv", archive(compare(PartialEq), check_bytes))]
// #[cfg_attr(
//   feature = "rkyv",
//   archive_attr(
//     derive(Debug, Copy, Clone, Eq, PartialEq, Hash),
//     repr(u8),
//     non_exhaustive
//   )
// )]
#[non_exhaustive]
#[repr(u8)]
pub enum DelegateVersion {
  /// Version 1
  #[default]
  V1 = 1,
}

impl core::fmt::Display for DelegateVersion {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      DelegateVersion::V1 => write!(f, "V1"),
    }
  }
}

impl TryFrom<u8> for DelegateVersion {
  type Error = UnknownDelegateVersion;
  fn try_from(v: u8) -> Result<Self, Self::Error> {
    match v {
      1 => Ok(DelegateVersion::V1),
      _ => Err(UnknownDelegateVersion(v)),
    }
  }
}

#[cfg(feature = "rkyv")]
const _: () = {
  impl From<ArchivedDelegateVersion> for DelegateVersion {
    fn from(value: ArchivedDelegateVersion) -> Self {
      match value {
        ArchivedDelegateVersion::V1 => Self::V1,
      }
    }
  }

  impl From<DelegateVersion> for ArchivedDelegateVersion {
    fn from(value: DelegateVersion) -> Self {
      match value {
        DelegateVersion::V1 => Self::V1,
      }
    }
  }
};

/// Unknown protocol version
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, thiserror::Error)]
#[error("V{0} is not a valid protocol version")]
pub struct UnknownProtocolVersion(u8);

/// Protocol version
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
// #[cfg_attr(
//   feature = "rkyv",
//   derive(::rkyv::Serialize, ::rkyv::Deserialize, ::rkyv::Archive)
// )]
// #[cfg_attr(feature = "rkyv", archive(compare(PartialEq), check_bytes))]
// #[cfg_attr(
//   feature = "rkyv",
//   archive_attr(
//     derive(Debug, Copy, Clone, Eq, PartialEq, Hash),
//     repr(u8),
//     non_exhaustive
//   )
// )]
#[non_exhaustive]
#[repr(u8)]
pub enum ProtocolVersion {
  /// Version 1
  #[default]
  V1 = 1,
}

impl core::fmt::Display for ProtocolVersion {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::V1 => write!(f, "V1"),
    }
  }
}

impl TryFrom<u8> for ProtocolVersion {
  type Error = UnknownProtocolVersion;
  fn try_from(v: u8) -> Result<Self, Self::Error> {
    match v {
      1 => Ok(Self::V1),
      _ => Err(UnknownProtocolVersion(v)),
    }
  }
}

#[cfg(feature = "rkyv")]
const _: () = {
  impl From<ArchivedProtocolVersion> for ProtocolVersion {
    fn from(value: ArchivedProtocolVersion) -> Self {
      match value {
        ArchivedProtocolVersion::V1 => Self::V1,
      }
    }
  }

  impl From<ProtocolVersion> for ArchivedProtocolVersion {
    fn from(value: ProtocolVersion) -> Self {
      match value {
        ProtocolVersion::V1 => Self::V1,
      }
    }
  }
};

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_delegate_version() {
    assert_eq!(DelegateVersion::V1 as u8, 1);
    assert_eq!(DelegateVersion::V1.to_string(), "V1");
    assert_eq!(DelegateVersion::try_from(1), Ok(DelegateVersion::V1));
    assert_eq!(DelegateVersion::try_from(0), Err(UnknownDelegateVersion(0)));
  }

  #[test]
  fn test_protocol_version() {
    assert_eq!(ProtocolVersion::V1 as u8, 1);
    assert_eq!(ProtocolVersion::V1.to_string(), "V1");
    assert_eq!(ProtocolVersion::try_from(1), Ok(ProtocolVersion::V1));
    assert_eq!(ProtocolVersion::try_from(0), Err(UnknownProtocolVersion(0)));
  }
}