serf_types/
version.rs

1/// Unknown delegate version
2#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, thiserror::Error)]
3#[error("V{0} is not a valid delegate version")]
4pub struct UnknownDelegateVersion(u8);
5
6/// Delegate version
7#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
9// #[cfg_attr(
10//   feature = "rkyv",
11//   derive(::rkyv::Serialize, ::rkyv::Deserialize, ::rkyv::Archive)
12// )]
13// #[cfg_attr(feature = "rkyv", archive(compare(PartialEq), check_bytes))]
14// #[cfg_attr(
15//   feature = "rkyv",
16//   archive_attr(
17//     derive(Debug, Copy, Clone, Eq, PartialEq, Hash),
18//     repr(u8),
19//     non_exhaustive
20//   )
21// )]
22#[non_exhaustive]
23#[repr(u8)]
24pub enum DelegateVersion {
25  /// Version 1
26  #[default]
27  V1 = 1,
28}
29
30impl core::fmt::Display for DelegateVersion {
31  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32    match self {
33      DelegateVersion::V1 => write!(f, "V1"),
34    }
35  }
36}
37
38impl TryFrom<u8> for DelegateVersion {
39  type Error = UnknownDelegateVersion;
40  fn try_from(v: u8) -> Result<Self, Self::Error> {
41    match v {
42      1 => Ok(DelegateVersion::V1),
43      _ => Err(UnknownDelegateVersion(v)),
44    }
45  }
46}
47
48#[cfg(feature = "rkyv")]
49const _: () = {
50  impl From<ArchivedDelegateVersion> for DelegateVersion {
51    fn from(value: ArchivedDelegateVersion) -> Self {
52      match value {
53        ArchivedDelegateVersion::V1 => Self::V1,
54      }
55    }
56  }
57
58  impl From<DelegateVersion> for ArchivedDelegateVersion {
59    fn from(value: DelegateVersion) -> Self {
60      match value {
61        DelegateVersion::V1 => Self::V1,
62      }
63    }
64  }
65};
66
67/// Unknown protocol version
68#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, thiserror::Error)]
69#[error("V{0} is not a valid protocol version")]
70pub struct UnknownProtocolVersion(u8);
71
72/// Protocol version
73#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
74#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
75// #[cfg_attr(
76//   feature = "rkyv",
77//   derive(::rkyv::Serialize, ::rkyv::Deserialize, ::rkyv::Archive)
78// )]
79// #[cfg_attr(feature = "rkyv", archive(compare(PartialEq), check_bytes))]
80// #[cfg_attr(
81//   feature = "rkyv",
82//   archive_attr(
83//     derive(Debug, Copy, Clone, Eq, PartialEq, Hash),
84//     repr(u8),
85//     non_exhaustive
86//   )
87// )]
88#[non_exhaustive]
89#[repr(u8)]
90pub enum ProtocolVersion {
91  /// Version 1
92  #[default]
93  V1 = 1,
94}
95
96impl core::fmt::Display for ProtocolVersion {
97  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98    match self {
99      Self::V1 => write!(f, "V1"),
100    }
101  }
102}
103
104impl TryFrom<u8> for ProtocolVersion {
105  type Error = UnknownProtocolVersion;
106  fn try_from(v: u8) -> Result<Self, Self::Error> {
107    match v {
108      1 => Ok(Self::V1),
109      _ => Err(UnknownProtocolVersion(v)),
110    }
111  }
112}
113
114#[cfg(feature = "rkyv")]
115const _: () = {
116  impl From<ArchivedProtocolVersion> for ProtocolVersion {
117    fn from(value: ArchivedProtocolVersion) -> Self {
118      match value {
119        ArchivedProtocolVersion::V1 => Self::V1,
120      }
121    }
122  }
123
124  impl From<ProtocolVersion> for ArchivedProtocolVersion {
125    fn from(value: ProtocolVersion) -> Self {
126      match value {
127        ProtocolVersion::V1 => Self::V1,
128      }
129    }
130  }
131};
132
133#[cfg(test)]
134mod tests {
135  use super::*;
136
137  #[test]
138  fn test_delegate_version() {
139    assert_eq!(DelegateVersion::V1 as u8, 1);
140    assert_eq!(DelegateVersion::V1.to_string(), "V1");
141    assert_eq!(DelegateVersion::try_from(1), Ok(DelegateVersion::V1));
142    assert_eq!(DelegateVersion::try_from(0), Err(UnknownDelegateVersion(0)));
143  }
144
145  #[test]
146  fn test_protocol_version() {
147    assert_eq!(ProtocolVersion::V1 as u8, 1);
148    assert_eq!(ProtocolVersion::V1.to_string(), "V1");
149    assert_eq!(ProtocolVersion::try_from(1), Ok(ProtocolVersion::V1));
150    assert_eq!(ProtocolVersion::try_from(0), Err(UnknownProtocolVersion(0)));
151  }
152}