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