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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
use thiserror::Error;

/// Serialize the given value to a vec. This method rejects all types except "raw bytes".
pub fn to_vec<T: serde::Serialize + ?Sized>(value: &T) -> Result<Vec<u8>, super::Error> {
    serde::Serialize::serialize(value, Serializer).map_err(|e| super::Error {
        description: e.to_string(),
        protocol: crate::CodecProtocol::Raw,
    })
}

#[derive(Error, Debug)]
enum Error {
    #[error("IPLD kind not supported by the raw codec")]
    KindNotSupported,
    #[error("i/o error when serializing: {0}")]
    Other(String),
}

impl serde::ser::Error for Error {
    fn custom<T>(msg: T) -> Self
    where
        T: std::fmt::Display,
    {
        Error::Other(msg.to_string())
    }
}

struct Serializer;

macro_rules! reject {
    ($($method:ident $t:ty),*) => {
        $(
            fn $method(self, _: $t) -> Result<Self::Ok, Self::Error> {
                Err(Error::KindNotSupported)
            }
        )*
    };
}

impl serde::ser::Serializer for Serializer {
    type Ok = Vec<u8>;
    type Error = Error;
    type SerializeSeq = serde::ser::Impossible<Vec<u8>, Error>;
    type SerializeTuple = serde::ser::Impossible<Vec<u8>, Error>;
    type SerializeTupleStruct = serde::ser::Impossible<Vec<u8>, Error>;
    type SerializeTupleVariant = serde::ser::Impossible<Vec<u8>, Error>;
    type SerializeMap = serde::ser::Impossible<Vec<u8>, Error>;
    type SerializeStruct = serde::ser::Impossible<Vec<u8>, Error>;
    type SerializeStructVariant = serde::ser::Impossible<Vec<u8>, Error>;

    fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
        Ok(v.to_owned())
    }

    reject! {
        serialize_bool bool,
        serialize_i8 i8,
        serialize_i16 i16,
        serialize_i32 i32,
        serialize_i64 i64,
        serialize_u8 u8,
        serialize_u16 u16,
        serialize_u32 u32,
        serialize_u64 u64,
        serialize_f32 f32,
        serialize_f64 f64,
        serialize_char char,
        serialize_str &str,
        serialize_unit_struct &'static str
    }

    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
        Err(Error::KindNotSupported)
    }

    fn serialize_some<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error>
    where
        T: serde::Serialize,
    {
        Err(Error::KindNotSupported)
    }

    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
        Err(Error::KindNotSupported)
    }

    fn serialize_unit_variant(
        self,
        _: &'static str,
        _: u32,
        _: &'static str,
    ) -> Result<Self::Ok, Self::Error> {
        Err(Error::KindNotSupported)
    }

    fn serialize_newtype_struct<T: ?Sized>(
        self,
        _: &'static str,
        _: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: serde::Serialize,
    {
        Err(Error::KindNotSupported)
    }

    fn serialize_newtype_variant<T: ?Sized>(
        self,
        _: &'static str,
        _: u32,
        _: &'static str,
        _: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: serde::Serialize,
    {
        Err(Error::KindNotSupported)
    }

    fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
        Err(Error::KindNotSupported)
    }

    fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
        Err(Error::KindNotSupported)
    }

    fn serialize_tuple_struct(
        self,
        _: &'static str,
        _: usize,
    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
        Err(Error::KindNotSupported)
    }

    fn serialize_tuple_variant(
        self,
        _: &'static str,
        _: u32,
        _: &'static str,
        _: usize,
    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
        Err(Error::KindNotSupported)
    }

    fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
        Err(Error::KindNotSupported)
    }

    fn serialize_struct(
        self,
        _: &'static str,
        _: usize,
    ) -> Result<Self::SerializeStruct, Self::Error> {
        Err(Error::KindNotSupported)
    }

    fn serialize_struct_variant(
        self,
        _: &'static str,
        _: u32,
        _: &'static str,
        _: usize,
    ) -> Result<Self::SerializeStructVariant, Self::Error> {
        Err(Error::KindNotSupported)
    }
}