Skip to content

Commit e91cbbc

Browse files
committed
refactor(api): Simplify JSON codec
1 parent 3c11c84 commit e91cbbc

File tree

1 file changed

+2
-28
lines changed

1 file changed

+2
-28
lines changed

src/servers/json/codec.rs

+2-28
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,6 @@ use tokio_util::codec::{Decoder, Encoder, LinesCodec};
44

55
use crate::api::json::message;
66

7-
/// Parse an incoming request as JSON into the corresponding message type
8-
///
9-
/// # Parameters
10-
///
11-
/// * `line`: input request line to parse as a message
12-
///
13-
/// # Errors
14-
///
15-
/// When the line cannot be parsed as JSON, the underlying error is returned from serde_json.
16-
fn parse_request(line: &str) -> serde_json::Result<message::HyperionMessage> {
17-
serde_json::from_str(line)
18-
}
19-
20-
/// Encode an outgoing reply as JSON
21-
///
22-
/// # Parameters
23-
///
24-
/// * `reply`: reply to encode as JSON
25-
///
26-
/// # Errors
27-
///
28-
/// When the reply cannot be encoded as JSON, the underlying error is returned from serde_json.
29-
fn encode_reply(reply: &message::HyperionResponse) -> serde_json::Result<String> {
30-
serde_json::to_string(reply)
31-
}
32-
337
#[derive(Debug, Error)]
348
pub enum JsonCodecError {
359
#[error("i/o error: {0}")]
@@ -62,7 +36,7 @@ impl Decoder for JsonCodec {
6236
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
6337
match self.lines.decode(src) {
6438
Ok(lines_result) => Ok(match lines_result {
65-
Some(ref line) => Some(parse_request(line)?),
39+
Some(ref line) => Some(serde_json::from_str(line)?),
6640
None => None,
6741
}),
6842
Err(error) => Err(error.into()),
@@ -78,7 +52,7 @@ impl Encoder<message::HyperionResponse> for JsonCodec {
7852
item: message::HyperionResponse,
7953
dst: &mut BytesMut,
8054
) -> Result<(), Self::Error> {
81-
match encode_reply(&item) {
55+
match serde_json::to_string(&item) {
8256
Ok(encoded) => Ok(self.lines.encode(encoded, dst)?),
8357
Err(encode_error) => Err(encode_error.into()),
8458
}

0 commit comments

Comments
 (0)