1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[non_exhaustive]
6#[derive(Default)]
7pub enum Version {
8 V1,
10 #[default]
12 V2c,
13 V3,
15}
16
17impl Version {
18 #[must_use]
30 pub const fn as_i32(self) -> i32 {
31 match self {
32 Version::V1 => 0,
33 Version::V2c => 1,
34 Version::V3 => 3,
35 }
36 }
37
38 #[must_use]
51 pub const fn from_i32(value: i32) -> Option<Self> {
52 match value {
53 0 => Some(Version::V1),
54 1 => Some(Version::V2c),
55 3 => Some(Version::V3),
56 _ => None,
57 }
58 }
59}
60
61impl TryFrom<i32> for Version {
62 type Error = i32;
63
64 fn try_from(value: i32) -> std::result::Result<Self, i32> {
65 Self::from_i32(value).ok_or(value)
66 }
67}
68
69impl std::fmt::Display for Version {
70 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71 match self {
72 Version::V1 => write!(f, "SNMPv1"),
73 Version::V2c => write!(f, "SNMPv2c"),
74 Version::V3 => write!(f, "SNMPv3"),
75 }
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 #[test]
84 fn test_version_try_from() {
85 assert_eq!(Version::try_from(0), Ok(Version::V1));
86 assert_eq!(Version::try_from(1), Ok(Version::V2c));
87 assert_eq!(Version::try_from(3), Ok(Version::V3));
88 assert_eq!(Version::try_from(2), Err(2));
89 assert_eq!(Version::try_from(-1), Err(-1));
90 }
91}