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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use core::fmt;
use std::time::Duration;
pub mod key {
pub const DEVICE_MODEL: &str = "device.model";
pub const DEVICE_MANUFACTURER: &str = "device.mfr";
pub const DEVICE_SERIAL: &str = "device.serial";
pub const DEVICE_TYPE: &str = "device.type";
pub const DEVICE_DESCRIPTION: &str = "device.description";
pub const DEVICE_CONTACT: &str = "device.contact";
pub const DEVICE_LOCATION: &str = "device.location";
pub const DEVICE_PART: &str = "device.part";
pub const DEVICE_MAC_ADDRESS: &str = "device.macaddr";
pub const DEVICE_UPTIME: &str = "device.uptime";
}
#[derive(Debug, Clone)]
pub enum Variable {
DeviceModel(String),
DeviceManufacturer(String),
DeviceSerial(String),
DeviceType(DeviceType),
DeviceDescription(String),
DeviceContact(String),
DeviceLocation(String),
DevicePart(String),
DeviceMacAddress(String),
DeviceUptime(Duration),
Other((String, String)),
}
impl Variable {
pub fn parse(name: &str, value: String) -> Variable {
use self::key::*;
match name {
DEVICE_MODEL => Self::DeviceModel(value),
DEVICE_MANUFACTURER => Self::DeviceManufacturer(value),
DEVICE_SERIAL => Self::DeviceSerial(value),
DEVICE_TYPE => Self::DeviceType(DeviceType::from(value)),
DEVICE_DESCRIPTION => Self::DeviceDescription(value),
DEVICE_CONTACT => Self::DeviceContact(value),
DEVICE_LOCATION => Self::DeviceLocation(value),
DEVICE_PART => Self::DevicePart(value),
DEVICE_MAC_ADDRESS => Self::DeviceMacAddress(value),
DEVICE_UPTIME => Self::DeviceUptime(Duration::from_secs(
value.parse().expect("invalid uptime value"),
)),
_ => Self::Other((name.into(), value)),
}
}
pub fn name(&self) -> &str {
use self::key::*;
match self {
Self::DeviceModel(_) => DEVICE_MODEL,
Self::DeviceManufacturer(_) => DEVICE_MANUFACTURER,
Self::DeviceSerial(_) => DEVICE_SERIAL,
Self::DeviceType(_) => DEVICE_TYPE,
Self::DeviceDescription(_) => DEVICE_DESCRIPTION,
Self::DeviceContact(_) => DEVICE_CONTACT,
Self::DeviceLocation(_) => DEVICE_LOCATION,
Self::DevicePart(_) => DEVICE_PART,
Self::DeviceMacAddress(_) => DEVICE_MAC_ADDRESS,
Self::DeviceUptime(_) => DEVICE_UPTIME,
Self::Other((name, _)) => name.as_str(),
}
}
pub fn value(&self) -> String {
match self {
Self::DeviceModel(value) => value.clone(),
Self::DeviceManufacturer(value) => value.clone(),
Self::DeviceSerial(value) => value.clone(),
Self::DeviceType(value) => value.to_string(),
Self::DeviceDescription(value) => value.clone(),
Self::DeviceContact(value) => value.clone(),
Self::DeviceLocation(value) => value.clone(),
Self::DevicePart(value) => value.clone(),
Self::DeviceMacAddress(value) => value.clone(),
Self::DeviceUptime(value) => value.as_secs().to_string(),
Self::Other((_, value)) => value.clone(),
}
}
}
impl fmt::Display for Variable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.name(), self.value())
}
}
#[derive(Debug, Clone)]
pub enum DeviceType {
Ups,
Pdu,
Scd,
Psu,
Ats,
Other(String),
}
impl DeviceType {
pub fn from(v: String) -> DeviceType {
match v.as_str() {
"ups" => Self::Ups,
"pdu" => Self::Pdu,
"scd" => Self::Scd,
"psu" => Self::Psu,
"ats" => Self::Ats,
_ => Self::Other(v),
}
}
}
impl fmt::Display for DeviceType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Ups => write!(f, "ups"),
Self::Pdu => write!(f, "pdu"),
Self::Scd => write!(f, "scd"),
Self::Psu => write!(f, "psu"),
Self::Ats => write!(f, "ats"),
Self::Other(val) => write!(f, "other({})", val),
}
}
}