fvm_ipld_encoding/
raw.rs

1// Copyright 2021-2023 Protocol Labs
2// SPDX-License-Identifier: Apache-2.0, MIT
3use thiserror::Error;
4
5/// Serialize the given value to a vec. This method rejects all types except "raw bytes".
6pub fn to_vec<T: serde::Serialize + ?Sized>(value: &T) -> Result<Vec<u8>, super::Error> {
7    serde::Serialize::serialize(value, Serializer).map_err(|e| super::Error {
8        description: e.to_string(),
9        protocol: crate::CodecProtocol::Raw,
10    })
11}
12
13#[derive(Error, Debug)]
14enum Error {
15    #[error("IPLD kind not supported by the raw codec")]
16    KindNotSupported,
17    #[error("i/o error when serializing: {0}")]
18    Other(String),
19}
20
21impl serde::ser::Error for Error {
22    fn custom<T>(msg: T) -> Self
23    where
24        T: std::fmt::Display,
25    {
26        Error::Other(msg.to_string())
27    }
28}
29
30struct Serializer;
31
32macro_rules! reject {
33    ($($method:ident $t:ty),*) => {
34        $(
35            fn $method(self, _: $t) -> Result<Self::Ok, Self::Error> {
36                Err(Error::KindNotSupported)
37            }
38        )*
39    };
40}
41
42impl serde::ser::Serializer for Serializer {
43    type Ok = Vec<u8>;
44    type Error = Error;
45    type SerializeSeq = serde::ser::Impossible<Vec<u8>, Error>;
46    type SerializeTuple = serde::ser::Impossible<Vec<u8>, Error>;
47    type SerializeTupleStruct = serde::ser::Impossible<Vec<u8>, Error>;
48    type SerializeTupleVariant = serde::ser::Impossible<Vec<u8>, Error>;
49    type SerializeMap = serde::ser::Impossible<Vec<u8>, Error>;
50    type SerializeStruct = serde::ser::Impossible<Vec<u8>, Error>;
51    type SerializeStructVariant = serde::ser::Impossible<Vec<u8>, Error>;
52
53    fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
54        Ok(v.to_owned())
55    }
56
57    reject! {
58        serialize_bool bool,
59        serialize_i8 i8,
60        serialize_i16 i16,
61        serialize_i32 i32,
62        serialize_i64 i64,
63        serialize_u8 u8,
64        serialize_u16 u16,
65        serialize_u32 u32,
66        serialize_u64 u64,
67        serialize_f32 f32,
68        serialize_f64 f64,
69        serialize_char char,
70        serialize_str &str,
71        serialize_unit_struct &'static str
72    }
73
74    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
75        Err(Error::KindNotSupported)
76    }
77
78    fn serialize_some<T>(self, _: &T) -> Result<Self::Ok, Self::Error>
79    where
80        T: serde::Serialize + ?Sized,
81    {
82        Err(Error::KindNotSupported)
83    }
84
85    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
86        Err(Error::KindNotSupported)
87    }
88
89    fn serialize_unit_variant(
90        self,
91        _: &'static str,
92        _: u32,
93        _: &'static str,
94    ) -> Result<Self::Ok, Self::Error> {
95        Err(Error::KindNotSupported)
96    }
97
98    fn serialize_newtype_struct<T>(self, _: &'static str, _: &T) -> Result<Self::Ok, Self::Error>
99    where
100        T: serde::Serialize + ?Sized,
101    {
102        Err(Error::KindNotSupported)
103    }
104
105    fn serialize_newtype_variant<T>(
106        self,
107        _: &'static str,
108        _: u32,
109        _: &'static str,
110        _: &T,
111    ) -> Result<Self::Ok, Self::Error>
112    where
113        T: serde::Serialize + ?Sized,
114    {
115        Err(Error::KindNotSupported)
116    }
117
118    fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
119        Err(Error::KindNotSupported)
120    }
121
122    fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
123        Err(Error::KindNotSupported)
124    }
125
126    fn serialize_tuple_struct(
127        self,
128        _: &'static str,
129        _: usize,
130    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
131        Err(Error::KindNotSupported)
132    }
133
134    fn serialize_tuple_variant(
135        self,
136        _: &'static str,
137        _: u32,
138        _: &'static str,
139        _: usize,
140    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
141        Err(Error::KindNotSupported)
142    }
143
144    fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
145        Err(Error::KindNotSupported)
146    }
147
148    fn serialize_struct(
149        self,
150        _: &'static str,
151        _: usize,
152    ) -> Result<Self::SerializeStruct, Self::Error> {
153        Err(Error::KindNotSupported)
154    }
155
156    fn serialize_struct_variant(
157        self,
158        _: &'static str,
159        _: u32,
160        _: &'static str,
161        _: usize,
162    ) -> Result<Self::SerializeStructVariant, Self::Error> {
163        Err(Error::KindNotSupported)
164    }
165}