From e183405d5a0120b456e4ebcecb46d7e8b54ceec4 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 13 Feb 2023 02:06:28 +0000 Subject: [PATCH] restructuring error code --- src/lib.rs | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c52f468..d797425 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,9 @@ -#![warn(clippy::all, rust_2018_idioms)] - mod types; mod i3msg; use std::os::unix::net::UnixStream; use std::io::prelude::*; -use std::io::{Result, Error, ErrorKind}; +use std::io::Result; use serde::de::DeserializeOwned; use i3msg::I3msg; @@ -61,10 +59,7 @@ impl Sway { /// Subscribe the IPC connection to the event listed in the payload pub fn subscribe(&mut self, events: Vec<&str>) -> Result { - let events = match self.parse_events(events) { - Ok(events) => events, - Err(e) => return Err(e), - }; + let events = self.parse_events(events)?; self.run(&events, IPC_SUBSCRIBE) } @@ -132,32 +127,22 @@ impl Sway { where T: DeserializeOwned, { - let buffer = match I3msg::new(payload, payload_type).construct_packet() { - Ok(buffer) => buffer, - Err(e) => return Err(e), - }; - - match self.stream.write_all(&buffer.as_slice()) { - Ok(_) => {}, - Err(e) => return Err(e), - } + let buffer = I3msg::new(payload, payload_type).construct_packet()?; + self.stream.write_all(&buffer.as_slice())?; let mut buffer = [0; 25000]; - let n = match self.stream.read(&mut buffer) { - Ok(n) => n, - Err(e) => return Err(e), - }; + let n = self.stream.read(&mut buffer)?; match serde_json::from_slice::(&buffer[14..n]) { Ok(result) => return Ok(result), - Err(e) => return Err(Error::new(ErrorKind::Other, e.to_string())), + Err(e) => return Err(e.into()), } } fn parse_events(&mut self, events: Vec<&str>) -> Result { match serde_json::to_string(&events) { Ok(json) => Ok(json), - Err(e) => return Err(Error::new(ErrorKind::Other, e.to_string())), + Err(e) => return Err(e.into()), } } }