1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#![warn(rust_2018_idioms, missing_debug_implementations)]
mod connection;
mod errors;
pub use connection::Connection;
pub use errors::{ReadError, WriteError};
use std::io::{BufRead, Write};
pub fn read<R: BufRead, T: serde::de::DeserializeOwned>(mut reader: R) -> Result<T, ReadError> {
let mut buf = String::new();
reader.read_line(&mut buf).map_err(ReadError::Io)?;
Ok(serde_json::from_str(&buf).map_err(ReadError::Deserialize)?)
}
pub fn write<W: Write, T: serde::Serialize>(mut writer: W, t: &T) -> Result<(), WriteError> {
let json = serde_json::to_string(t).map_err(WriteError::Serialize)?;
writer.write_all(json.as_bytes()).map_err(WriteError::Io)?;
writer.write_all(b"\n").map_err(WriteError::Io)?;
Ok(())
}