use crate::ser::Error; use serde::ser; use std::str; pub struct PartSerializer { sink: S, } impl PartSerializer { pub fn new(sink: S) -> Self { PartSerializer { sink } } } pub trait Sink: Sized { type Ok; fn serialize_static_str( self, value: &'static str, ) -> Result; fn serialize_str(self, value: &str) -> Result; fn serialize_string(self, value: String) -> Result; fn serialize_none(self) -> Result; fn serialize_some( self, value: &T, ) -> Result; fn unsupported(self) -> Error; } impl ser::Serializer for PartSerializer { type Ok = S::Ok; type Error = Error; type SerializeSeq = ser::Impossible; type SerializeTuple = ser::Impossible; type SerializeTupleStruct = ser::Impossible; type SerializeTupleVariant = ser::Impossible; type SerializeMap = ser::Impossible; type SerializeStruct = ser::Impossible; type SerializeStructVariant = ser::Impossible; fn serialize_bool(self, v: bool) -> Result { self.sink .serialize_static_str(if v { "true" } else { "false" }) } fn serialize_i8(self, v: i8) -> Result { self.serialize_integer(v) } fn serialize_i16(self, v: i16) -> Result { self.serialize_integer(v) } fn serialize_i32(self, v: i32) -> Result { self.serialize_integer(v) } fn serialize_i64(self, v: i64) -> Result { self.serialize_integer(v) } fn serialize_u8(self, v: u8) -> Result { self.serialize_integer(v) } fn serialize_u16(self, v: u16) -> Result { self.serialize_integer(v) } fn serialize_u32(self, v: u32) -> Result { self.serialize_integer(v) } fn serialize_u64(self, v: u64) -> Result { self.serialize_integer(v) } fn serialize_u128(self, v: u128) -> Result { self.serialize_integer(v) } fn serialize_i128(self, v: i128) -> Result { self.serialize_integer(v) } fn serialize_f32(self, v: f32) -> Result { self.serialize_floating(v) } fn serialize_f64(self, v: f64) -> Result { self.serialize_floating(v) } fn serialize_char(self, v: char) -> Result { self.sink.serialize_string(v.to_string()) } fn serialize_str(self, value: &str) -> Result { self.sink.serialize_str(value) } fn serialize_bytes(self, value: &[u8]) -> Result { match str::from_utf8(value) { Ok(value) => self.sink.serialize_str(value), Err(err) => Err(Error::Utf8(err)), } } fn serialize_unit(self) -> Result { Err(self.sink.unsupported()) } fn serialize_unit_struct(self, name: &'static str) -> Result { self.sink.serialize_static_str(name) } fn serialize_unit_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, ) -> Result { self.sink.serialize_static_str(variant) } fn serialize_newtype_struct( self, _name: &'static str, value: &T, ) -> Result { value.serialize(self) } fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _value: &T, ) -> Result { Err(self.sink.unsupported()) } fn serialize_none(self) -> Result { self.sink.serialize_none() } fn serialize_some( self, value: &T, ) -> Result { self.sink.serialize_some(value) } fn serialize_seq( self, _len: Option, ) -> Result { Err(self.sink.unsupported()) } fn serialize_tuple( self, _len: usize, ) -> Result { Err(self.sink.unsupported()) } fn serialize_tuple_struct( self, _name: &'static str, _len: usize, ) -> Result { Err(self.sink.unsupported()) } fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { Err(self.sink.unsupported()) } fn serialize_map( self, _len: Option, ) -> Result { Err(self.sink.unsupported()) } fn serialize_struct( self, _name: &'static str, _len: usize, ) -> Result { Err(self.sink.unsupported()) } fn serialize_struct_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { Err(self.sink.unsupported()) } } impl PartSerializer { fn serialize_integer(self, value: I) -> Result where I: itoa::Integer, { let mut buf = itoa::Buffer::new(); let part = buf.format(value); ser::Serializer::serialize_str(self, part) } fn serialize_floating(self, value: F) -> Result where F: ryu::Float, { let mut buf = ryu::Buffer::new(); let part = buf.format(value); ser::Serializer::serialize_str(self, part) } }