From 1ca847b4fcc8daffc1b84e721a4b07e9639a684e Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Fri, 3 Feb 2023 19:39:22 +0100 Subject: [PATCH] use anyhow::{...} --- kubi-udp/src/client.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/kubi-udp/src/client.rs b/kubi-udp/src/client.rs index fd96620..1551723 100644 --- a/kubi-udp/src/client.rs +++ b/kubi-udp/src/client.rs @@ -1,3 +1,4 @@ +use anyhow::{Result, bail}; use std::{ net::{UdpSocket, SocketAddr}, marker::PhantomData, time::{Instant, Duration} @@ -35,7 +36,7 @@ pub struct Client where S: Encode + Decode, R: Encode + Decode { _r: PhantomData<*const R>, } impl Client where S: Encode + Decode, R: Encode + Decode { - pub fn new(addr: SocketAddr, config: ClientConfig) -> anyhow::Result { + pub fn new(addr: SocketAddr, config: ClientConfig) -> Result { let bind_addr: SocketAddr = "127.0.0.1:0".parse().unwrap(); let socket = UdpSocket::bind(bind_addr)?; socket.set_nonblocking(true)?; @@ -52,9 +53,9 @@ impl Client where S: Encode + Decode, R: Encode + Decode { _r: PhantomData, }) } - pub fn connect(&mut self) -> anyhow::Result<()> { + pub fn connect(&mut self) -> Result<()> { if self.status != ClientStatus::Disconnected { - anyhow::bail!("Not Disconnected"); + bail!("Not Disconnected"); } self.status = ClientStatus::Connecting; self.timeout = Instant::now(); @@ -63,30 +64,30 @@ impl Client where S: Encode + Decode, R: Encode + Decode { self.send_raw_packet(ClientPacket::Connect)?; Ok(()) } - fn send_raw_packet(&self, packet: ClientPacket) -> anyhow::Result<()> { + fn send_raw_packet(&self, packet: ClientPacket) -> Result<()> { let id_packet = IdClientPacket(self.client_id, packet); let bytes = bincode::encode_to_vec(id_packet, BINCODE_CONFIG)?; self.socket.send(&bytes)?; Ok(()) } - pub fn send_message(&self, message: S) -> anyhow::Result<()> { + pub fn send_message(&self, message: S) -> Result<()> { self.send_raw_packet(ClientPacket::Data(message))?; Ok(()) } - fn disconnect_inner(&mut self, reason: DisconnectReason) -> anyhow::Result<()> { + fn disconnect_inner(&mut self, reason: DisconnectReason) -> Result<()> { self.send_raw_packet(ClientPacket::Disconnect)?; self.status = ClientStatus::Disconnected; - self.disconnect_reason = DisconnectReason::ClientDisconnected; + self.disconnect_reason = reason; Ok(()) } - pub fn disconnect(&mut self) -> anyhow::Result<()> { + pub fn disconnect(&mut self) -> Result<()> { if self.status != ClientStatus::Connected { - anyhow::bail!("Not Connected"); + bail!("Not Connected"); } self.disconnect_inner(DisconnectReason::ClientDisconnected)?; Ok(()) } - pub fn update(&mut self) -> anyhow::Result<()> { + pub fn update(&mut self) -> Result<()> { if self.status == ClientStatus::Disconnected { return Ok(()) }