use macaddr::MacAddr;
use std::time::SystemTime;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GatewayEvent {
pub timestamp: SystemTime,
pub meshif: u32,
pub meshif_name: String,
pub action: GatewayEventAction,
pub gateway_mac: Option<MacAddr>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum GatewayEventAction {
Add = 1,
Change = 2,
Delete = 3,
}
impl GatewayEvent {
pub fn new(
meshif: u32,
meshif_name: String,
action: GatewayEventAction,
gateway_mac: Option<MacAddr>,
) -> Self {
Self {
timestamp: SystemTime::now(),
meshif,
meshif_name,
action,
gateway_mac,
}
}
pub fn has_gateway(&self) -> bool {
self.gateway_mac.is_some()
}
}
impl std::fmt::Display for GatewayEventAction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Add => write!(f, "ADD"),
Self::Change => write!(f, "CHANGE"),
Self::Delete => write!(f, "DELETE"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gateway_event_creation() {
let mac = "60:09:c3:aa:bb:cc".parse().unwrap();
let event = GatewayEvent::new(6, "bat0".to_string(), GatewayEventAction::Add, Some(mac));
assert_eq!(event.meshif, 6);
assert_eq!(event.meshif_name, "bat0");
assert_eq!(event.action, GatewayEventAction::Add);
assert_eq!(event.gateway_mac, Some(mac));
assert!(event.has_gateway());
}
#[test]
fn test_delete_event_no_gateway() {
let event = GatewayEvent::new(6, "bat0".to_string(), GatewayEventAction::Delete, None);
assert_eq!(event.action, GatewayEventAction::Delete);
assert!(!event.has_gateway());
}
#[test]
fn test_action_display() {
assert_eq!(GatewayEventAction::Add.to_string(), "ADD");
assert_eq!(GatewayEventAction::Change.to_string(), "CHANGE");
assert_eq!(GatewayEventAction::Delete.to_string(), "DELETE");
}
}