|
| 1 | +use base64::prelude::*; |
| 2 | +use serde::ser::{self, Serialize}; |
| 3 | +use yaml_rust2::yaml::Yaml; |
| 4 | + |
| 5 | +#[derive(Debug)] |
| 6 | +pub struct YamlSerializerError { |
| 7 | + msg: String, |
| 8 | +} |
| 9 | + |
| 10 | +impl std::fmt::Display for YamlSerializerError { |
| 11 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 12 | + write!(f, "YamlSerializerError: {}", self.msg) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +impl std::error::Error for YamlSerializerError {} |
| 17 | + |
| 18 | +impl ser::Error for YamlSerializerError { |
| 19 | + fn custom<T>(msg: T) -> Self |
| 20 | + where |
| 21 | + T: std::fmt::Display, |
| 22 | + { |
| 23 | + YamlSerializerError { |
| 24 | + msg: format!("{msg}"), |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +pub struct YamlSerializer; |
| 30 | + |
| 31 | +impl YamlSerializer { |
| 32 | + pub fn serialize<T>(value: &T) -> Result<Yaml, YamlSerializerError> |
| 33 | + where |
| 34 | + T: Serialize, |
| 35 | + { |
| 36 | + value.serialize(YamlSerializer) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl ser::Serializer for YamlSerializer { |
| 41 | + type Ok = Yaml; |
| 42 | + type Error = YamlSerializerError; |
| 43 | + |
| 44 | + type SerializeSeq = SeqSerializer; |
| 45 | + type SerializeTuple = SeqSerializer; |
| 46 | + type SerializeTupleStruct = SeqSerializer; |
| 47 | + type SerializeTupleVariant = SeqSerializer; |
| 48 | + type SerializeMap = MapSerializer; |
| 49 | + type SerializeStruct = MapSerializer; |
| 50 | + type SerializeStructVariant = MapSerializer; |
| 51 | + |
| 52 | + fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> { |
| 53 | + Ok(Yaml::Boolean(v)) |
| 54 | + } |
| 55 | + |
| 56 | + fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> { |
| 57 | + Ok(Yaml::Integer(v as i64)) |
| 58 | + } |
| 59 | + |
| 60 | + fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> { |
| 61 | + Ok(Yaml::Integer(v as i64)) |
| 62 | + } |
| 63 | + |
| 64 | + fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> { |
| 65 | + Ok(Yaml::Integer(v as i64)) |
| 66 | + } |
| 67 | + |
| 68 | + fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> { |
| 69 | + Ok(Yaml::Integer(v)) |
| 70 | + } |
| 71 | + |
| 72 | + fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> { |
| 73 | + Ok(Yaml::Integer(v as i64)) |
| 74 | + } |
| 75 | + |
| 76 | + fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> { |
| 77 | + Ok(Yaml::Integer(v as i64)) |
| 78 | + } |
| 79 | + |
| 80 | + fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> { |
| 81 | + Ok(Yaml::Integer(v as i64)) |
| 82 | + } |
| 83 | + |
| 84 | + fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> { |
| 85 | + Ok(Yaml::Real(v.to_string())) |
| 86 | + } |
| 87 | + |
| 88 | + fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> { |
| 89 | + Ok(Yaml::Real(v.to_string())) |
| 90 | + } |
| 91 | + |
| 92 | + fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> { |
| 93 | + Ok(Yaml::Real(v.to_string())) |
| 94 | + } |
| 95 | + |
| 96 | + fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> { |
| 97 | + Ok(Yaml::String(v.to_string())) |
| 98 | + } |
| 99 | + |
| 100 | + fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { |
| 101 | + Ok(Yaml::String(v.to_owned())) |
| 102 | + } |
| 103 | + |
| 104 | + fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> { |
| 105 | + let encoded = BASE64_STANDARD.encode(v); |
| 106 | + Ok(Yaml::String(encoded)) |
| 107 | + } |
| 108 | + |
| 109 | + fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
| 110 | + Ok(Yaml::Null) |
| 111 | + } |
| 112 | + |
| 113 | + fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error> |
| 114 | + where |
| 115 | + T: Serialize + ?Sized, |
| 116 | + { |
| 117 | + value.serialize(self) |
| 118 | + } |
| 119 | + |
| 120 | + fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
| 121 | + Ok(Yaml::Hash(Default::default())) |
| 122 | + } |
| 123 | + |
| 124 | + fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> { |
| 125 | + Ok(Yaml::Hash(Default::default())) |
| 126 | + } |
| 127 | + |
| 128 | + fn serialize_unit_variant( |
| 129 | + self, |
| 130 | + _name: &'static str, |
| 131 | + _variant_index: u32, |
| 132 | + variant: &'static str, |
| 133 | + ) -> Result<Self::Ok, Self::Error> { |
| 134 | + Ok(Yaml::String(variant.to_owned())) |
| 135 | + } |
| 136 | + |
| 137 | + fn serialize_newtype_struct<T>( |
| 138 | + self, |
| 139 | + _name: &'static str, |
| 140 | + value: &T, |
| 141 | + ) -> Result<Self::Ok, Self::Error> |
| 142 | + where |
| 143 | + T: Serialize + ?Sized, |
| 144 | + { |
| 145 | + value.serialize(self) |
| 146 | + } |
| 147 | + |
| 148 | + fn serialize_newtype_variant<T>( |
| 149 | + self, |
| 150 | + _name: &'static str, |
| 151 | + _variant_index: u32, |
| 152 | + variant: &'static str, |
| 153 | + value: &T, |
| 154 | + ) -> Result<Self::Ok, Self::Error> |
| 155 | + where |
| 156 | + T: Serialize + ?Sized, |
| 157 | + { |
| 158 | + let mut hash = yaml_rust2::yaml::Hash::new(); |
| 159 | + hash.insert(Yaml::String(variant.to_owned()), value.serialize(self)?); |
| 160 | + Ok(Yaml::Hash(hash)) |
| 161 | + } |
| 162 | + |
| 163 | + fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
| 164 | + Ok(SeqSerializer { |
| 165 | + vec: Vec::with_capacity(len.unwrap_or(0)), |
| 166 | + }) |
| 167 | + } |
| 168 | + |
| 169 | + fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
| 170 | + self.serialize_seq(Some(len)) |
| 171 | + } |
| 172 | + |
| 173 | + fn serialize_tuple_struct( |
| 174 | + self, |
| 175 | + _name: &'static str, |
| 176 | + len: usize, |
| 177 | + ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 178 | + self.serialize_seq(Some(len)) |
| 179 | + } |
| 180 | + |
| 181 | + fn serialize_tuple_variant( |
| 182 | + self, |
| 183 | + _name: &'static str, |
| 184 | + _variant_index: u32, |
| 185 | + _variant: &'static str, |
| 186 | + len: usize, |
| 187 | + ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 188 | + Ok(SeqSerializer { |
| 189 | + vec: Vec::with_capacity(len), |
| 190 | + }) |
| 191 | + } |
| 192 | + |
| 193 | + fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
| 194 | + Ok(MapSerializer { |
| 195 | + map: yaml_rust2::yaml::Hash::new(), |
| 196 | + next_key: None, |
| 197 | + }) |
| 198 | + } |
| 199 | + |
| 200 | + fn serialize_struct( |
| 201 | + self, |
| 202 | + _name: &'static str, |
| 203 | + len: usize, |
| 204 | + ) -> Result<Self::SerializeStruct, Self::Error> { |
| 205 | + self.serialize_map(Some(len)) |
| 206 | + } |
| 207 | + |
| 208 | + fn serialize_struct_variant( |
| 209 | + self, |
| 210 | + _name: &'static str, |
| 211 | + _variant_index: u32, |
| 212 | + _variant: &'static str, |
| 213 | + len: usize, |
| 214 | + ) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 215 | + self.serialize_map(Some(len)) |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +pub struct SeqSerializer { |
| 220 | + vec: Vec<Yaml>, |
| 221 | +} |
| 222 | + |
| 223 | +impl ser::SerializeSeq for SeqSerializer { |
| 224 | + type Ok = Yaml; |
| 225 | + type Error = YamlSerializerError; |
| 226 | + |
| 227 | + fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error> |
| 228 | + where |
| 229 | + T: Serialize + ?Sized, |
| 230 | + { |
| 231 | + self.vec.push(value.serialize(YamlSerializer)?); |
| 232 | + Ok(()) |
| 233 | + } |
| 234 | + |
| 235 | + fn end(self) -> Result<Self::Ok, Self::Error> { |
| 236 | + Ok(Yaml::Array(self.vec)) |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +impl ser::SerializeTuple for SeqSerializer { |
| 241 | + type Ok = Yaml; |
| 242 | + type Error = YamlSerializerError; |
| 243 | + |
| 244 | + fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error> |
| 245 | + where |
| 246 | + T: Serialize + ?Sized, |
| 247 | + { |
| 248 | + ser::SerializeSeq::serialize_element(self, value) |
| 249 | + } |
| 250 | + |
| 251 | + fn end(self) -> Result<Self::Ok, Self::Error> { |
| 252 | + ser::SerializeSeq::end(self) |
| 253 | + } |
| 254 | +} |
| 255 | + |
| 256 | +impl ser::SerializeTupleStruct for SeqSerializer { |
| 257 | + type Ok = Yaml; |
| 258 | + type Error = YamlSerializerError; |
| 259 | + |
| 260 | + fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error> |
| 261 | + where |
| 262 | + T: Serialize + ?Sized, |
| 263 | + { |
| 264 | + ser::SerializeSeq::serialize_element(self, value) |
| 265 | + } |
| 266 | + |
| 267 | + fn end(self) -> Result<Self::Ok, Self::Error> { |
| 268 | + ser::SerializeSeq::end(self) |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +impl ser::SerializeTupleVariant for SeqSerializer { |
| 273 | + type Ok = Yaml; |
| 274 | + type Error = YamlSerializerError; |
| 275 | + |
| 276 | + fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error> |
| 277 | + where |
| 278 | + T: Serialize + ?Sized, |
| 279 | + { |
| 280 | + ser::SerializeSeq::serialize_element(self, value) |
| 281 | + } |
| 282 | + |
| 283 | + fn end(self) -> Result<Self::Ok, Self::Error> { |
| 284 | + ser::SerializeSeq::end(self) |
| 285 | + } |
| 286 | +} |
| 287 | + |
| 288 | +pub struct MapSerializer { |
| 289 | + map: yaml_rust2::yaml::Hash, |
| 290 | + next_key: Option<Yaml>, |
| 291 | +} |
| 292 | + |
| 293 | +impl ser::SerializeMap for MapSerializer { |
| 294 | + type Ok = Yaml; |
| 295 | + type Error = YamlSerializerError; |
| 296 | + |
| 297 | + fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error> |
| 298 | + where |
| 299 | + T: Serialize + ?Sized, |
| 300 | + { |
| 301 | + self.next_key = Some(key.serialize(YamlSerializer)?); |
| 302 | + Ok(()) |
| 303 | + } |
| 304 | + |
| 305 | + fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error> |
| 306 | + where |
| 307 | + T: Serialize + ?Sized, |
| 308 | + { |
| 309 | + let key = self.next_key.take().unwrap(); |
| 310 | + self.map.insert(key, value.serialize(YamlSerializer)?); |
| 311 | + Ok(()) |
| 312 | + } |
| 313 | + |
| 314 | + fn end(self) -> Result<Self::Ok, Self::Error> { |
| 315 | + Ok(Yaml::Hash(self.map)) |
| 316 | + } |
| 317 | +} |
| 318 | + |
| 319 | +impl ser::SerializeStruct for MapSerializer { |
| 320 | + type Ok = Yaml; |
| 321 | + type Error = YamlSerializerError; |
| 322 | + |
| 323 | + fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
| 324 | + where |
| 325 | + T: Serialize + ?Sized, |
| 326 | + { |
| 327 | + ser::SerializeMap::serialize_entry(self, key, value) |
| 328 | + } |
| 329 | + |
| 330 | + fn end(self) -> Result<Self::Ok, Self::Error> { |
| 331 | + ser::SerializeMap::end(self) |
| 332 | + } |
| 333 | +} |
| 334 | + |
| 335 | +impl ser::SerializeStructVariant for MapSerializer { |
| 336 | + type Ok = Yaml; |
| 337 | + type Error = YamlSerializerError; |
| 338 | + |
| 339 | + fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
| 340 | + where |
| 341 | + T: Serialize + ?Sized, |
| 342 | + { |
| 343 | + ser::SerializeMap::serialize_entry(self, key, value) |
| 344 | + } |
| 345 | + |
| 346 | + fn end(self) -> Result<Self::Ok, Self::Error> { |
| 347 | + ser::SerializeMap::end(self) |
| 348 | + } |
| 349 | +} |
0 commit comments