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
use crate::{
plugin::PluginEncoder,
protocol::{PluginCall, PluginResponse},
};
use nu_protocol::ShellError;
pub mod json;
pub mod msgpack;
#[derive(Clone, Debug)]
pub enum EncodingType {
Json(json::JsonSerializer),
MsgPack(msgpack::MsgPackSerializer),
}
impl EncodingType {
pub fn try_from_bytes(bytes: &[u8]) -> Option<Self> {
match bytes {
b"json" => Some(Self::Json(json::JsonSerializer {})),
b"msgpack" => Some(Self::MsgPack(msgpack::MsgPackSerializer {})),
_ => None,
}
}
pub fn encode_call(
&self,
plugin_call: &PluginCall,
writer: &mut impl std::io::Write,
) -> Result<(), ShellError> {
match self {
EncodingType::Json(encoder) => encoder.encode_call(plugin_call, writer),
EncodingType::MsgPack(encoder) => encoder.encode_call(plugin_call, writer),
}
}
pub fn decode_call(
&self,
reader: &mut impl std::io::BufRead,
) -> Result<PluginCall, ShellError> {
match self {
EncodingType::Json(encoder) => encoder.decode_call(reader),
EncodingType::MsgPack(encoder) => encoder.decode_call(reader),
}
}
pub fn encode_response(
&self,
plugin_response: &PluginResponse,
writer: &mut impl std::io::Write,
) -> Result<(), ShellError> {
match self {
EncodingType::Json(encoder) => encoder.encode_response(plugin_response, writer),
EncodingType::MsgPack(encoder) => encoder.encode_response(plugin_response, writer),
}
}
pub fn decode_response(
&self,
reader: &mut impl std::io::BufRead,
) -> Result<PluginResponse, ShellError> {
match self {
EncodingType::Json(encoder) => encoder.decode_response(reader),
EncodingType::MsgPack(encoder) => encoder.decode_response(reader),
}
}
pub fn to_str(&self) -> &'static str {
match self {
Self::Json(_) => "json",
Self::MsgPack(_) => "msgpack",
}
}
}