From 396cb8a7ef69dc2a477a6cb443300f28991185ea Mon Sep 17 00:00:00 2001 From: Kieran Date: Fri, 28 Mar 2025 21:45:53 +0000 Subject: [PATCH] fix: avoid unwrap in mikrotik --- src/router/mikrotik.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/router/mikrotik.rs b/src/router/mikrotik.rs index 3295a67..072cb79 100644 --- a/src/router/mikrotik.rs +++ b/src/router/mikrotik.rs @@ -1,6 +1,6 @@ use crate::json_api::JsonApi; use crate::router::{ArpEntry, Router}; -use anyhow::{ensure, Result}; +use anyhow::{ensure, Context, Result}; use base64::engine::general_purpose::STANDARD; use base64::Engine; use log::debug; @@ -33,14 +33,14 @@ impl Router for MikrotikRouter { async fn list_arp_entry(&self) -> Result> { let rsp: Vec = self.api.req(Method::GET, "/rest/ip/arp", ()).await?; - Ok(rsp.into_iter().map(|e| e.into()).collect()) + Ok(rsp.into_iter().map(|e| e.try_into().unwrap()).collect()) } async fn add_arp_entry(&self, entry: &ArpEntry) -> Result { let req: MikrotikArpEntry = entry.clone().into(); let rsp: MikrotikArpEntry = self.api.req(Method::PUT, "/rest/ip/arp", req).await?; debug!("{:?}", rsp); - Ok(rsp.into()) + Ok(rsp.try_into()?) } async fn remove_arp_entry(&self, id: &str) -> Result<()> { @@ -64,7 +64,7 @@ impl Router for MikrotikRouter { ) .await?; debug!("{:?}", rsp); - Ok(rsp.into()) + Ok(rsp.try_into()?) } } @@ -82,15 +82,17 @@ struct MikrotikArpEntry { pub comment: Option, } -impl From for ArpEntry { - fn from(val: MikrotikArpEntry) -> Self { - ArpEntry { - id: val.id, - address: val.address, - mac_address: val.mac_address.unwrap(), - interface: Some(val.interface), - comment: val.comment, - } +impl TryFrom for ArpEntry { + type Error = anyhow::Error; + + fn try_from(value: MikrotikArpEntry) -> std::result::Result { + Ok(ArpEntry { + id: value.id, + address: value.address, + mac_address: value.mac_address.context("Mac address is empty")?, + interface: Some(value.interface), + comment: value.comment, + }) } }