nu_plugin_core/serializers/
msgpack.rs

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::io::ErrorKind;

use nu_plugin_protocol::{PluginInput, PluginOutput};
use nu_protocol::ShellError;
use serde::Deserialize;

use crate::{Encoder, PluginEncoder};

/// A `PluginEncoder` that enables the plugin to communicate with Nushell with MsgPack
/// serialized data.
///
/// Each message is written as a MessagePack object. There is no message envelope or separator.
#[derive(Clone, Copy, Debug)]
pub struct MsgPackSerializer;

impl PluginEncoder for MsgPackSerializer {
    fn name(&self) -> &str {
        "msgpack"
    }
}

impl Encoder<PluginInput> for MsgPackSerializer {
    fn encode(
        &self,
        plugin_input: &PluginInput,
        writer: &mut impl std::io::Write,
    ) -> Result<(), nu_protocol::ShellError> {
        rmp_serde::encode::write_named(writer, plugin_input).map_err(rmp_encode_err)
    }

    fn decode(
        &self,
        reader: &mut impl std::io::BufRead,
    ) -> Result<Option<PluginInput>, ShellError> {
        let mut de = rmp_serde::Deserializer::new(reader);
        PluginInput::deserialize(&mut de)
            .map(Some)
            .or_else(rmp_decode_err)
    }
}

impl Encoder<PluginOutput> for MsgPackSerializer {
    fn encode(
        &self,
        plugin_output: &PluginOutput,
        writer: &mut impl std::io::Write,
    ) -> Result<(), ShellError> {
        rmp_serde::encode::write_named(writer, plugin_output).map_err(rmp_encode_err)
    }

    fn decode(
        &self,
        reader: &mut impl std::io::BufRead,
    ) -> Result<Option<PluginOutput>, ShellError> {
        let mut de = rmp_serde::Deserializer::new(reader);
        PluginOutput::deserialize(&mut de)
            .map(Some)
            .or_else(rmp_decode_err)
    }
}

/// Handle a msgpack encode error
fn rmp_encode_err(err: rmp_serde::encode::Error) -> ShellError {
    match err {
        rmp_serde::encode::Error::InvalidValueWrite(_) => {
            // I/O error
            ShellError::IOError {
                msg: err.to_string(),
            }
        }
        _ => {
            // Something else
            ShellError::PluginFailedToEncode {
                msg: err.to_string(),
            }
        }
    }
}

/// Handle a msgpack decode error. Returns `Ok(None)` on eof
fn rmp_decode_err<T>(err: rmp_serde::decode::Error) -> Result<Option<T>, ShellError> {
    match err {
        rmp_serde::decode::Error::InvalidMarkerRead(err)
        | rmp_serde::decode::Error::InvalidDataRead(err) => {
            if matches!(err.kind(), ErrorKind::UnexpectedEof) {
                // EOF
                Ok(None)
            } else {
                // I/O error
                Err(ShellError::IOError {
                    msg: err.to_string(),
                })
            }
        }
        _ => {
            // Something else
            Err(ShellError::PluginFailedToDecode {
                msg: err.to_string(),
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    crate::serializers::tests::generate_tests!(MsgPackSerializer {});
}