185 lines
4.2 KiB
Rust
185 lines
4.2 KiB
Rust
|
use crate::error::{Error, Result};
|
||
|
use ser::SerializeSeq;
|
||
|
use serde::{de::MapAccess, ser, Serialize};
|
||
|
|
||
|
pub struct Serializer {
|
||
|
output: String,
|
||
|
}
|
||
|
|
||
|
pub fn to_string<T>(value: &T) -> Result<String>
|
||
|
where
|
||
|
T: Serialize,
|
||
|
{
|
||
|
let mut serializer = Serializer {
|
||
|
output: String::new(),
|
||
|
};
|
||
|
value.serialize(&mut serializer)?;
|
||
|
Ok(serializer.output)
|
||
|
}
|
||
|
|
||
|
impl<'a> ser::Serializer for &'a mut Serializer {
|
||
|
type Ok = ();
|
||
|
type Error = Error;
|
||
|
type SerializeSeq = Self;
|
||
|
type SerializeTuple = Self;
|
||
|
type SerializeTupleStruct = Self;
|
||
|
type SerializeTupleVariant = Self;
|
||
|
type SerializeMap = Self;
|
||
|
type SerializeStruct = Self;
|
||
|
type SerializeStructVariant = Self;
|
||
|
|
||
|
fn serialize_bool(self, v: bool) -> Result<Self::Ok> {
|
||
|
self.output += if v { "true" } else { "false" };
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
fn serialize_i8(self, v: i8) -> Result<Self::Ok> {
|
||
|
self.serialize_i64(i64::from(v))
|
||
|
}
|
||
|
|
||
|
fn serialize_i16(self, v: i16) -> Result<Self::Ok> {
|
||
|
self.serialize_i64(i64::from(v))
|
||
|
}
|
||
|
|
||
|
fn serialize_i32(self, v: i32) -> Result<Self::Ok> {
|
||
|
self.serialize_i64(i64::from(v))
|
||
|
}
|
||
|
|
||
|
fn serialize_i64(self, v: i64) -> Result<Self::Ok> {
|
||
|
self.output += &v.to_string();
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
fn serialize_u8(self, v: u8) -> Result<Self::Ok> {
|
||
|
self.serialize_u64(u64::from(v))
|
||
|
}
|
||
|
|
||
|
fn serialize_u16(self, v: u16) -> Result<Self::Ok> {
|
||
|
self.serialize_u64(u64::from(v))
|
||
|
}
|
||
|
|
||
|
fn serialize_u32(self, v: u32) -> Result<Self::Ok> {
|
||
|
self.serialize_u64(u64::from(v))
|
||
|
}
|
||
|
|
||
|
fn serialize_u64(self, v: u64) -> Result<Self::Ok> {
|
||
|
self.output += &v.to_string();
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
fn serialize_f32(self, v: f32) -> Result<Self::Ok> {
|
||
|
self.serialize_f64(f64::from(v))
|
||
|
}
|
||
|
|
||
|
fn serialize_f64(self, v: f64) -> Result<Self::Ok> {
|
||
|
self.output += &v.to_string();
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
fn serialize_char(self, v: char) -> Result<Self::Ok> {
|
||
|
self.output += &v.to_string();
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
fn serialize_str(self, v: &str) -> Result<Self::Ok> {
|
||
|
self.output += v;
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok> {
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_none(self) -> Result<Self::Ok> {
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok>
|
||
|
where
|
||
|
T: Serialize,
|
||
|
{
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_unit(self) -> Result<Self::Ok> {
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok> {
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_unit_variant(
|
||
|
self,
|
||
|
name: &'static str,
|
||
|
variant_index: u32,
|
||
|
variant: &'static str,
|
||
|
) -> Result<Self::Ok> {
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_newtype_struct<T: ?Sized>(self, name: &'static str, value: &T) -> Result<Self::Ok>
|
||
|
where
|
||
|
T: Serialize,
|
||
|
{
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_newtype_variant<T: ?Sized>(
|
||
|
self,
|
||
|
name: &'static str,
|
||
|
variant_index: u32,
|
||
|
variant: &'static str,
|
||
|
value: &T,
|
||
|
) -> Result<Self::Ok>
|
||
|
where
|
||
|
T: Serialize,
|
||
|
{
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
|
||
|
self.serialize_seq(Some(len))
|
||
|
}
|
||
|
|
||
|
fn serialize_tuple_struct(
|
||
|
self,
|
||
|
name: &'static str,
|
||
|
len: usize,
|
||
|
) -> Result<Self::SerializeTupleStruct> {
|
||
|
self.serialize_seq(Some(len))
|
||
|
}
|
||
|
|
||
|
fn serialize_tuple_variant(
|
||
|
self,
|
||
|
name: &'static str,
|
||
|
variant_index: u32,
|
||
|
variant: &'static str,
|
||
|
len: usize,
|
||
|
) -> Result<Self::SerializeTupleVariant> {
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
|
||
|
todo!()
|
||
|
}
|
||
|
|
||
|
fn serialize_struct_variant(
|
||
|
self,
|
||
|
name: &'static str,
|
||
|
variant_index: u32,
|
||
|
variant: &'static str,
|
||
|
len: usize,
|
||
|
) -> Result<Self::SerializeStructVariant> {
|
||
|
todo!()
|
||
|
}
|
||
|
}
|