/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rmp-serde-1.3.1/src/bytes.rs
Line | Count | Source |
1 | | //! Hacky serializer that only allows `u8` |
2 | | |
3 | | use serde::ser::Impossible; |
4 | | use serde::Serialize; |
5 | | use std::fmt; |
6 | | |
7 | | pub(crate) struct OnlyBytes; |
8 | | pub(crate) struct Nope; |
9 | | |
10 | | impl std::error::Error for Nope { |
11 | | } |
12 | | |
13 | | impl std::fmt::Display for Nope { |
14 | 0 | fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { |
15 | 0 | Ok(()) |
16 | 0 | } |
17 | | } |
18 | | |
19 | | impl std::fmt::Debug for Nope { |
20 | 0 | fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { |
21 | 0 | Ok(()) |
22 | 0 | } |
23 | | } |
24 | | |
25 | | impl serde::ser::Error for Nope { |
26 | 0 | fn custom<T: fmt::Display>(_: T) -> Self { |
27 | 0 | Self |
28 | 0 | } |
29 | | } |
30 | | |
31 | | impl serde::de::Error for Nope { |
32 | 0 | fn custom<T: fmt::Display>(_: T) -> Self { |
33 | 0 | Self |
34 | 0 | } |
35 | | } |
36 | | |
37 | | impl serde::Serializer for OnlyBytes { |
38 | | type Error = Nope; |
39 | | type Ok = u8; |
40 | | type SerializeMap = Impossible<u8, Nope>; |
41 | | type SerializeSeq = Impossible<u8, Nope>; |
42 | | type SerializeStruct = Impossible<u8, Nope>; |
43 | | type SerializeStructVariant = Impossible<u8, Nope>; |
44 | | type SerializeTuple = Impossible<u8, Nope>; |
45 | | type SerializeTupleStruct = Impossible<u8, Nope>; |
46 | | type SerializeTupleVariant = Impossible<u8, Nope>; |
47 | | |
48 | 0 | fn serialize_u8(self, val: u8) -> Result<u8, Nope> { |
49 | 0 | Ok(val) |
50 | 0 | } |
51 | | |
52 | 0 | fn serialize_bool(self, _: bool) -> Result<u8, Nope> { |
53 | 0 | Err(Nope) |
54 | 0 | } |
55 | | |
56 | 0 | fn serialize_i8(self, _: i8) -> Result<u8, Nope> { |
57 | 0 | Err(Nope) |
58 | 0 | } |
59 | | |
60 | 0 | fn serialize_i16(self, _: i16) -> Result<u8, Nope> { |
61 | 0 | Err(Nope) |
62 | 0 | } |
63 | | |
64 | 0 | fn serialize_i32(self, _: i32) -> Result<u8, Nope> { |
65 | 0 | Err(Nope) |
66 | 0 | } |
67 | | |
68 | 0 | fn serialize_i64(self, _: i64) -> Result<u8, Nope> { |
69 | 0 | Err(Nope) |
70 | 0 | } |
71 | | |
72 | 0 | fn serialize_u16(self, _: u16) -> Result<u8, Nope> { |
73 | 0 | Err(Nope) |
74 | 0 | } |
75 | | |
76 | 0 | fn serialize_u32(self, _: u32) -> Result<u8, Nope> { |
77 | 0 | Err(Nope) |
78 | 0 | } |
79 | | |
80 | 0 | fn serialize_u64(self, _: u64) -> Result<u8, Nope> { |
81 | 0 | Err(Nope) |
82 | 0 | } |
83 | | |
84 | 0 | fn serialize_f32(self, _: f32) -> Result<u8, Nope> { |
85 | 0 | Err(Nope) |
86 | 0 | } |
87 | | |
88 | 0 | fn serialize_f64(self, _: f64) -> Result<u8, Nope> { |
89 | 0 | Err(Nope) |
90 | 0 | } |
91 | | |
92 | 0 | fn serialize_char(self, _: char) -> Result<u8, Nope> { |
93 | 0 | Err(Nope) |
94 | 0 | } |
95 | | |
96 | 0 | fn serialize_str(self, _: &str) -> Result<u8, Nope> { |
97 | 0 | Err(Nope) |
98 | 0 | } |
99 | | |
100 | 0 | fn serialize_bytes(self, _: &[u8]) -> Result<u8, Nope> { |
101 | 0 | Err(Nope) |
102 | 0 | } |
103 | | |
104 | 0 | fn serialize_none(self) -> Result<u8, Nope> { |
105 | 0 | Err(Nope) |
106 | 0 | } |
107 | | |
108 | 0 | fn serialize_some<T>(self, _: &T) -> Result<u8, Nope> where T: Serialize + ?Sized { |
109 | 0 | Err(Nope) |
110 | 0 | } |
111 | | |
112 | 0 | fn serialize_unit(self) -> Result<u8, Nope> { |
113 | 0 | Err(Nope) |
114 | 0 | } |
115 | | |
116 | 0 | fn serialize_unit_struct(self, _: &'static str) -> Result<u8, Nope> { |
117 | 0 | Err(Nope) |
118 | 0 | } |
119 | | |
120 | 0 | fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<u8, Nope> { |
121 | 0 | Err(Nope) |
122 | 0 | } |
123 | | |
124 | 0 | fn serialize_newtype_struct<T>(self, _: &'static str, _: &T) -> Result<u8, Nope> where T: Serialize + ?Sized { |
125 | 0 | Err(Nope) |
126 | 0 | } |
127 | | |
128 | 0 | fn serialize_newtype_variant<T>(self, _: &'static str, _: u32, _: &'static str, _: &T) -> Result<u8, Nope> where T: Serialize + ?Sized { |
129 | 0 | Err(Nope) |
130 | 0 | } |
131 | | |
132 | 0 | fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Nope> { |
133 | 0 | Err(Nope) |
134 | 0 | } |
135 | | |
136 | 0 | fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Nope> { |
137 | 0 | Err(Nope) |
138 | 0 | } |
139 | | |
140 | 0 | fn serialize_tuple_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeTupleStruct, Nope> { |
141 | 0 | Err(Nope) |
142 | 0 | } |
143 | | |
144 | 0 | fn serialize_tuple_variant(self, _: &'static str, _: u32, _: &'static str, _: usize) -> Result<Self::SerializeTupleVariant, Nope> { |
145 | 0 | Err(Nope) |
146 | 0 | } |
147 | | |
148 | 0 | fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Nope> { |
149 | 0 | Err(Nope) |
150 | 0 | } |
151 | | |
152 | 0 | fn serialize_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeStruct, Nope> { |
153 | 0 | Err(Nope) |
154 | 0 | } |
155 | | |
156 | 0 | fn serialize_struct_variant(self, _: &'static str, _: u32, _: &'static str, _: usize) -> Result<Self::SerializeStructVariant, Nope> { |
157 | 0 | Err(Nope) |
158 | 0 | } |
159 | | |
160 | 0 | fn collect_seq<I>(self, _: I) -> Result<u8, Nope> where I: IntoIterator, <I as IntoIterator>::Item: Serialize { |
161 | 0 | Err(Nope) |
162 | 0 | } |
163 | | |
164 | 0 | fn collect_map<K, V, I>(self, _: I) -> Result<u8, Nope> where K: Serialize, V: Serialize, I: IntoIterator<Item = (K, V)> { |
165 | 0 | Err(Nope) |
166 | 0 | } |
167 | | |
168 | 0 | fn collect_str<T>(self, _: &T) -> Result<u8, Nope> where T: fmt::Display + ?Sized { |
169 | 0 | Err(Nope) |
170 | 0 | } |
171 | | } |