simplified error handling with custom result type
This commit is contained in:
parent
ffa987e00a
commit
60404db64e
13
src/lib.rs
13
src/lib.rs
@ -4,13 +4,14 @@ mod ipc;
|
|||||||
|
|
||||||
use std::os::unix::net::UnixStream;
|
use std::os::unix::net::UnixStream;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::io::Result;
|
|
||||||
|
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use i3msg::I3msg;
|
use i3msg::I3msg;
|
||||||
use types::*;
|
use types::*;
|
||||||
use ipc::*;
|
use ipc::*;
|
||||||
|
|
||||||
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
pub struct Sway {
|
pub struct Sway {
|
||||||
stream: UnixStream,
|
stream: UnixStream,
|
||||||
}
|
}
|
||||||
@ -107,17 +108,11 @@ impl Sway {
|
|||||||
let mut buffer = [0; 25000];
|
let mut buffer = [0; 25000];
|
||||||
let n = self.stream.read(&mut buffer)?;
|
let n = self.stream.read(&mut buffer)?;
|
||||||
|
|
||||||
match serde_json::from_slice::<T>(&buffer[14..n]) {
|
Ok(serde_json::from_slice::<T>(&buffer[14..n])?)
|
||||||
Ok(result) => Ok(result),
|
|
||||||
Err(e) => Err(e.into()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_events(&mut self, events: Vec<&str>) -> Result<String> {
|
fn parse_events(&mut self, events: Vec<&str>) -> Result<String> {
|
||||||
match serde_json::to_string(&events) {
|
Ok(serde_json::to_string(&events)?)
|
||||||
Ok(json) => Ok(json),
|
|
||||||
Err(e) => Err(e.into()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user