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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Implementations of [Codec] for stdlib types.

use bytes::BufMut;

use crate::Codec;

impl Codec for () {
    fn codec_name() -> String {
        "()".into()
    }

    fn encode<B>(&self, _buf: &mut B)
    where
        B: BufMut,
    {
        // No-op.
    }

    fn decode<'a>(buf: &'a [u8]) -> Result<Self, String> {
        if !buf.is_empty() {
            return Err(format!("decode expected empty buf got {} bytes", buf.len()));
        }
        Ok(())
    }
}

impl Codec for String {
    fn codec_name() -> String {
        "String".into()
    }

    fn encode<B>(&self, buf: &mut B)
    where
        B: BufMut,
    {
        buf.put(self.as_bytes())
    }

    fn decode<'a>(buf: &'a [u8]) -> Result<Self, String> {
        String::from_utf8(buf.to_owned()).map_err(|err| err.to_string())
    }
}

impl Codec for Vec<u8> {
    fn codec_name() -> String {
        "Vec<u8>".into()
    }

    fn encode<B>(&self, buf: &mut B)
    where
        B: BufMut,
    {
        buf.put(self.as_slice())
    }

    fn decode<'a>(buf: &'a [u8]) -> Result<Self, String> {
        Ok(buf.to_owned())
    }
}

const RESULT_OK: u8 = 0;
const RESULT_ERR: u8 = 1;
impl<T: Codec, E: Codec> Codec for Result<T, E> {
    fn codec_name() -> String {
        "Result".into()
    }

    fn encode<B>(&self, buf: &mut B)
    where
        B: BufMut,
    {
        match self {
            Ok(r) => {
                buf.put(&[RESULT_OK][..]);
                r.encode(buf);
            }
            Err(err) => {
                buf.put(&[RESULT_ERR][..]);
                err.encode(buf);
            }
        }
    }

    fn decode<'a>(buf: &'a [u8]) -> Result<Self, String> {
        let typ = buf[0];
        let result = match typ {
            RESULT_OK => {
                let result_slice = &buf[1..(buf.len())];
                Ok(T::decode(result_slice)?)
            }
            RESULT_ERR => {
                let err_slice = &buf[1..(buf.len())];
                Err(E::decode(err_slice)?)
            }
            typ => return Err(format!("Unexpected Result variant: {}.", typ)),
        };
        Ok(result)
    }
}

#[cfg(test)]
mod tests {
    use crate::Codec;

    #[test]
    fn test_result_ok_roundtrip() -> Result<(), String> {
        let original: Result<String, String> = Ok("ciao!".to_string());
        let mut encoded = Vec::new();
        original.encode(&mut encoded);
        let decoded: Result<String, String> = Result::decode(&encoded)?;

        assert_eq!(decoded, original);

        Ok(())
    }

    #[test]
    fn test_result_err_roundtrip() -> Result<(), String> {
        let original: Result<String, String> = Err("ciao!".to_string());
        let mut encoded = Vec::new();
        original.encode(&mut encoded);
        let decoded: Result<String, String> = Result::decode(&encoded)?;

        assert_eq!(decoded, original);

        Ok(())
    }

    #[test]
    fn test_result_decoding_error() -> Result<(), String> {
        let encoded = vec![42];
        let decoded: Result<Result<String, String>, String> = Result::decode(&encoded);

        assert_eq!(decoded, Err("Unexpected Result variant: 42.".to_string()));

        Ok(())
    }
}