From 3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Thu, 15 Feb 2024 17:17:30 +0100 Subject: Closes #10 & #12. Added `thiserror` crate and changed to `IntoSocketAddr` for easier usage and error handling --- src/wol.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src/wol.rs') diff --git a/src/wol.rs b/src/wol.rs index 83c0ee6..31cf350 100644 --- a/src/wol.rs +++ b/src/wol.rs @@ -1,4 +1,4 @@ -use std::net::{SocketAddr, UdpSocket}; +use std::net::{ToSocketAddrs, UdpSocket}; use crate::error::Error; @@ -11,8 +11,8 @@ pub fn create_buffer(mac_addr: &str) -> Result, Error> { let mut mac = Vec::new(); let sp = mac_addr.split(':'); for f in sp { - mac.push(u8::from_str_radix(f, 16).map_err(Error::BufferParse)?); - }; + mac.push(u8::from_str_radix(f, 16)?); + } let mut buf = vec![255; 6]; for _ in 0..16 { for i in &mac { @@ -23,8 +23,12 @@ pub fn create_buffer(mac_addr: &str) -> Result, Error> { } /// Sends a buffer on UDP broadcast -pub fn send_packet(bind_addr: &SocketAddr, broadcast_addr: &SocketAddr, buffer: &[u8]) -> Result { - let socket = UdpSocket::bind(bind_addr).map_err(Error::Broadcast)?; - socket.set_broadcast(true).map_err(Error::Broadcast)?; - socket.send_to(buffer, broadcast_addr).map_err(Error::Broadcast) +pub fn send_packet( + bind_addr: A, + broadcast_addr: A, + buffer: &[u8], +) -> Result { + let socket = UdpSocket::bind(bind_addr)?; + socket.set_broadcast(true)?; + Ok(socket.send_to(buffer, broadcast_addr)?) } -- cgit v1.2.3