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)),
}
}
}
impl fmt::Display for Variable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::key::*;
match self {
Self::DeviceModel(value) => write!(f, "{} = {}", DEVICE_MODEL, value),
Self::DeviceManufacturer(value) => write!(f, "{} = {}", DEVICE_MANUFACTURER, value),
Self::DeviceSerial(value) => write!(f, "{} = {}", DEVICE_SERIAL, value),
Self::DeviceType(value) => write!(f, "{} = {}", DEVICE_TYPE, value),
Self::DeviceDescription(value) => write!(f, "{} = {}", DEVICE_DESCRIPTION, value),
Self::DeviceContact(value) => write!(f, "{} = {}", DEVICE_CONTACT, value),
Self::DeviceLocation(value) => write!(f, "{} = {}", DEVICE_LOCATION, value),
Self::DevicePart(value) => write!(f, "{} = {}", DEVICE_PART, value),
Self::DeviceMacAddress(value) => write!(f, "{} = {}", DEVICE_MAC_ADDRESS, value),
Self::DeviceUptime(value) => {
write!(f, "{} = {} seconds", DEVICE_UPTIME, value.as_secs())
}
Self::Other((key, value)) => write!(f, "other({}) = {}", key, 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),
}
}
}