fix: avoid unwrap in mikrotik
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-03-28 21:45:53 +00:00
parent 8068b7d5bc
commit 396cb8a7ef

View File

@ -1,6 +1,6 @@
use crate::json_api::JsonApi; use crate::json_api::JsonApi;
use crate::router::{ArpEntry, Router}; use crate::router::{ArpEntry, Router};
use anyhow::{ensure, Result}; use anyhow::{ensure, Context, Result};
use base64::engine::general_purpose::STANDARD; use base64::engine::general_purpose::STANDARD;
use base64::Engine; use base64::Engine;
use log::debug; use log::debug;
@ -33,14 +33,14 @@ impl Router for MikrotikRouter {
async fn list_arp_entry(&self) -> Result<Vec<ArpEntry>> { async fn list_arp_entry(&self) -> Result<Vec<ArpEntry>> {
let rsp: Vec<MikrotikArpEntry> = self.api.req(Method::GET, "/rest/ip/arp", ()).await?; let rsp: Vec<MikrotikArpEntry> = 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<ArpEntry> { async fn add_arp_entry(&self, entry: &ArpEntry) -> Result<ArpEntry> {
let req: MikrotikArpEntry = entry.clone().into(); let req: MikrotikArpEntry = entry.clone().into();
let rsp: MikrotikArpEntry = self.api.req(Method::PUT, "/rest/ip/arp", req).await?; let rsp: MikrotikArpEntry = self.api.req(Method::PUT, "/rest/ip/arp", req).await?;
debug!("{:?}", rsp); debug!("{:?}", rsp);
Ok(rsp.into()) Ok(rsp.try_into()?)
} }
async fn remove_arp_entry(&self, id: &str) -> Result<()> { async fn remove_arp_entry(&self, id: &str) -> Result<()> {
@ -64,7 +64,7 @@ impl Router for MikrotikRouter {
) )
.await?; .await?;
debug!("{:?}", rsp); debug!("{:?}", rsp);
Ok(rsp.into()) Ok(rsp.try_into()?)
} }
} }
@ -82,15 +82,17 @@ struct MikrotikArpEntry {
pub comment: Option<String>, pub comment: Option<String>,
} }
impl From<MikrotikArpEntry> for ArpEntry { impl TryFrom<MikrotikArpEntry> for ArpEntry {
fn from(val: MikrotikArpEntry) -> Self { type Error = anyhow::Error;
ArpEntry {
id: val.id, fn try_from(value: MikrotikArpEntry) -> std::result::Result<Self, Self::Error> {
address: val.address, Ok(ArpEntry {
mac_address: val.mac_address.unwrap(), id: value.id,
interface: Some(val.interface), address: value.address,
comment: val.comment, mac_address: value.mac_address.context("Mac address is empty")?,
} interface: Some(value.interface),
comment: value.comment,
})
} }
} }