restructuring error code

This commit is contained in:
Michael 2023-02-13 02:06:28 +00:00
parent 391bb0ea2f
commit e183405d5a
Signed by: michael
GPG Key ID: 523BD9EF68BDD44C

View File

@ -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<Success> {
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::<T>(&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<String> {
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()),
}
}
}