simplified error handling with custom result type

This commit is contained in:
Michael 2023-03-04 00:17:04 +00:00
parent ffa987e00a
commit 60404db64e
Signed by: michael
GPG Key ID: 523BD9EF68BDD44C

View File

@ -4,13 +4,14 @@ mod ipc;
use std::os::unix::net::UnixStream;
use std::io::prelude::*;
use std::io::Result;
use serde::de::DeserializeOwned;
use i3msg::I3msg;
use types::*;
use ipc::*;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
pub struct Sway {
stream: UnixStream,
}
@ -107,17 +108,11 @@ impl Sway {
let mut buffer = [0; 25000];
let n = self.stream.read(&mut buffer)?;
match serde_json::from_slice::<T>(&buffer[14..n]) {
Ok(result) => Ok(result),
Err(e) => Err(e.into()),
}
Ok(serde_json::from_slice::<T>(&buffer[14..n])?)
}
fn parse_events(&mut self, events: Vec<&str>) -> Result<String> {
match serde_json::to_string(&events) {
Ok(json) => Ok(json),
Err(e) => Err(e.into()),
}
Ok(serde_json::to_string(&events)?)
}
}