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