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 pub const fn as_i32(self) -> i32 {
30 match self {
31 Version::V1 => 0,
32 Version::V2c => 1,
33 Version::V3 => 3,
34 }
35 }
36
37 pub const fn from_i32(value: i32) -> Option<Self> {
50 match value {
51 0 => Some(Version::V1),
52 1 => Some(Version::V2c),
53 3 => Some(Version::V3),
54 _ => None,
55 }
56 }
57}
58
59impl TryFrom<i32> for Version {
60 type Error = i32;
61
62 fn try_from(value: i32) -> std::result::Result<Self, i32> {
63 Self::from_i32(value).ok_or(value)
64 }
65}
66
67impl std::fmt::Display for Version {
68 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69 match self {
70 Version::V1 => write!(f, "SNMPv1"),
71 Version::V2c => write!(f, "SNMPv2c"),
72 Version::V3 => write!(f, "SNMPv3"),
73 }
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80
81 #[test]
82 fn test_version_try_from() {
83 assert_eq!(Version::try_from(0), Ok(Version::V1));
84 assert_eq!(Version::try_from(1), Ok(Version::V2c));
85 assert_eq!(Version::try_from(3), Ok(Version::V3));
86 assert_eq!(Version::try_from(2), Err(2));
87 assert_eq!(Version::try_from(-1), Err(-1));
88 }
89}