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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#![warn(rust_2018_idioms, missing_debug_implementations)]
mod connection;
mod errors;
pub use connection::Connection;
pub use errors::{ReadError, WriteError};
#[cfg(not(feature = "tokio"))]
mod imp {
use super::*;
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();
let num_bytes_read = reader.read_line(&mut buf).map_err(ReadError::Io)?;
if num_bytes_read == 0 {
return Err(ReadError::Eof);
}
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(())
}
}
#[cfg(feature = "tokio")]
mod imp {
use super::*;
use tokio::io::{AsyncBufRead as BufRead, AsyncBufReadExt, AsyncWrite as Write, AsyncWriteExt};
pub async fn read<R: BufRead + Unpin, T: serde::de::DeserializeOwned>(
mut reader: R,
) -> Result<T, ReadError> {
let mut buf = String::new();
let num_bytes_read = reader.read_line(&mut buf).await.map_err(ReadError::Io)?;
if num_bytes_read == 0 {
return Err(ReadError::Eof);
}
Ok(serde_json::from_str(&buf).map_err(ReadError::Deserialize)?)
}
pub async fn write<W: Write + Unpin, 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())
.await
.map_err(WriteError::Io)?;
writer.write_all(b"\n").await.map_err(WriteError::Io)?;
Ok(())
}
}
pub use imp::*;