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