491 lines
11 KiB
Rust
491 lines
11 KiB
Rust
mod error;
|
|
mod string_writer;
|
|
mod format;
|
|
mod config;
|
|
#[macro_use]
|
|
mod macros;
|
|
|
|
use std::io::Write;
|
|
use std::marker::PhantomData;
|
|
use serde::ser::{self, Serialize};
|
|
use crate::syntax;
|
|
|
|
pub use self::format::*;
|
|
pub use self::config::*;
|
|
pub use self::string_writer::StringWriter;
|
|
pub use self::error::{Error, ErrorKind, Result};
|
|
|
|
pub struct Serializer<W: Write, C: Config> {
|
|
out: W,
|
|
tag_stack: Vec<bool>,
|
|
first_element: bool,
|
|
config: PhantomData<C>,
|
|
format_engine: <C::Format as Format>::Engine
|
|
}
|
|
|
|
impl<W: Write, C: Config> Serializer<W, C> {
|
|
#[inline]
|
|
fn set_first_element(&mut self) {
|
|
self.first_element = true;
|
|
}
|
|
|
|
#[inline]
|
|
fn check_not_first_element(&mut self) -> bool {
|
|
if self.first_element {
|
|
self.first_element = false;
|
|
false
|
|
} else {
|
|
true
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
fn write_delim(&mut self, delim: u8) -> Result<()> {
|
|
self.format_engine.mark_delim(delim);
|
|
self.write_bytes(&[delim])
|
|
}
|
|
|
|
#[inline]
|
|
fn write_bytes(&mut self, bytes: &[u8]) -> Result<()> {
|
|
<C::Format as Format>::write::<W>(&mut self.format_engine, &mut self.out, bytes)
|
|
}
|
|
|
|
#[inline]
|
|
fn write_real<R>(&mut self, r: R) -> Result<()> where <C::Format as Format>::RealFormat: WriteReal<R> {
|
|
<C::Format as Format>::RealFormat::write_real::<W>(&mut self.out, r)
|
|
}
|
|
}
|
|
|
|
pub fn to_string_with_config<T, C>(value: &T) -> Result<String>
|
|
where
|
|
T: Serialize,
|
|
C: Config
|
|
{
|
|
let mut serializer = Serializer {
|
|
out: StringWriter::new(),
|
|
config: PhantomData::<C>,
|
|
tag_stack: Vec::with_capacity(C::INITIAL_TAG_STACK_SIZE),
|
|
first_element: false,
|
|
format_engine: Default::default()
|
|
};
|
|
value.serialize(&mut serializer)?;
|
|
Ok(serializer.out.to_string().expect("valid utf8"))
|
|
}
|
|
|
|
pub fn to_string<T>(value: &T) -> Result<String>
|
|
where
|
|
T: Serialize,
|
|
{
|
|
to_string_with_config::<T, DefaultConfig>(value)
|
|
}
|
|
|
|
impl<'a, W: Write, C: Config> ser::Serializer for &'a mut Serializer<W, C> {
|
|
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;
|
|
|
|
impl_ser_real!(serialize_i8, i8);
|
|
impl_ser_real!(serialize_i16, i16);
|
|
impl_ser_real!(serialize_i32, i32);
|
|
impl_ser_real!(serialize_i64, i64);
|
|
impl_ser_real!(serialize_u8, u8);
|
|
impl_ser_real!(serialize_u16, u16);
|
|
impl_ser_real!(serialize_u32, u32);
|
|
impl_ser_real!(serialize_u64, u64);
|
|
impl_ser_real!(serialize_f32, f32);
|
|
impl_ser_real!(serialize_f64, f64);
|
|
|
|
fn serialize_char(self, v: char) -> Result<()> {
|
|
let mut char_bytes: [u8; 4] = unsafe { std::mem::uninitialized() };
|
|
let char_str = v.encode_utf8(&mut char_bytes[..]);
|
|
self.serialize_str(&char_str)
|
|
}
|
|
|
|
fn serialize_str(self, v: &str) -> Result<()> {
|
|
self.write_delim(syntax::TEXT_CHAR)?;
|
|
if C::DISABLE_STRING_ESCAPING {
|
|
self.write_bytes(v.as_bytes())?;
|
|
} else {
|
|
// TODO: Escape characters
|
|
self.write_bytes(v.as_bytes())?;
|
|
}
|
|
self.write_delim(syntax::TEXT_CHAR)
|
|
}
|
|
|
|
fn serialize_bool(self, v: bool) -> Result<()> {
|
|
self.write_bytes(
|
|
if v {
|
|
syntax::BOOLEAN_TRUE_KEYWORD
|
|
} else {
|
|
syntax::BOOLEAN_FALSE_KEYWORD
|
|
}
|
|
)
|
|
}
|
|
|
|
fn serialize_bytes(self, v: &[u8]) -> Result<()> {
|
|
use serde::ser::SerializeSeq;
|
|
let mut seq = self.serialize_seq(Some(v.len()))?;
|
|
for byte in v {
|
|
seq.serialize_element(byte)?;
|
|
}
|
|
seq.end()
|
|
}
|
|
|
|
fn serialize_some<T>(self, value: &T) -> Result<()>
|
|
where
|
|
T: ?Sized + Serialize,
|
|
{
|
|
value.serialize(self)
|
|
}
|
|
|
|
fn serialize_none(self) -> Result<()> {
|
|
self.serialize_unit()
|
|
}
|
|
|
|
fn serialize_unit(self) -> Result<()> {
|
|
self.write_bytes(b"none")
|
|
}
|
|
|
|
fn serialize_unit_struct(self, name: &'static str) -> Result<()> {
|
|
if C::UNIT_STRUCT_TO_KIND {
|
|
self.write_bytes(name.as_bytes())
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn serialize_unit_variant(
|
|
self,
|
|
_name: &'static str,
|
|
_variant_index: u32,
|
|
variant: &'static str,
|
|
) -> Result<()> {
|
|
self.serialize_str(variant)
|
|
}
|
|
|
|
fn serialize_newtype_struct<T>(
|
|
self,
|
|
name: &'static str,
|
|
value: &T,
|
|
) -> Result<()>
|
|
where
|
|
T: ?Sized + Serialize,
|
|
{
|
|
tag_start!(self, C::TAG_NEWTYPE_STRUCTS, name);
|
|
value.serialize(&mut *self)?;
|
|
tag_end!(self, C::TAG_NEWTYPE_STRUCTS)
|
|
}
|
|
|
|
fn serialize_newtype_variant<T>(
|
|
self,
|
|
_name: &'static str,
|
|
_variant_index: u32,
|
|
variant: &'static str,
|
|
value: &T,
|
|
) -> Result<()>
|
|
where
|
|
T: ?Sized + Serialize,
|
|
{
|
|
self.write_delim(syntax::DICT_START_CHAR)?;
|
|
variant.serialize(&mut *self)?;
|
|
self.write_delim(syntax::DICT_KV_SEPARATOR_CHAR)?;
|
|
value.serialize(&mut *self)?;
|
|
self.write_delim(syntax::DICT_END_CHAR)
|
|
}
|
|
|
|
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
|
|
self.write_delim(syntax::LIST_START_CHAR)?;
|
|
self.set_first_element();
|
|
Ok(self)
|
|
}
|
|
|
|
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> {
|
|
tag_start!(self, C::TAG_TUPLE_STRUCTS, name);
|
|
self.serialize_seq(Some(len))
|
|
}
|
|
|
|
fn serialize_tuple_variant(
|
|
self,
|
|
_name: &'static str,
|
|
_variant_index: u32,
|
|
variant: &'static str,
|
|
_len: usize,
|
|
) -> Result<Self::SerializeTupleVariant> {
|
|
tag_start!(self, C::TAG_TUPLE_VARIANTS, variant);
|
|
self.write_delim(syntax::DICT_START_CHAR)?;
|
|
variant.serialize(&mut *self)?;
|
|
self.write_delim(syntax::DICT_KV_SEPARATOR_CHAR)?;
|
|
self.write_delim(syntax::LIST_START_CHAR)?;
|
|
self.set_first_element();
|
|
Ok(self)
|
|
}
|
|
|
|
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
|
|
self.write_delim(syntax::DICT_START_CHAR)?;
|
|
self.set_first_element();
|
|
Ok(self)
|
|
}
|
|
|
|
fn serialize_struct(
|
|
self,
|
|
name: &'static str,
|
|
len: usize,
|
|
) -> Result<Self::SerializeStruct> {
|
|
tag_start!(self, C::TAG_STRUCTS, name);
|
|
self.serialize_map(Some(len))?;
|
|
self.set_first_element();
|
|
Ok(self)
|
|
}
|
|
|
|
fn serialize_struct_variant(
|
|
self,
|
|
_name: &'static str,
|
|
_variant_index: u32,
|
|
variant: &'static str,
|
|
_len: usize,
|
|
) -> Result<Self::SerializeStructVariant> {
|
|
tag_start!(self, C::TAG_STRUCT_VARIANTS, variant);
|
|
self.write_delim(syntax::DICT_START_CHAR)?;
|
|
variant.serialize(&mut *self)?;
|
|
self.write_delim(syntax::DICT_KV_SEPARATOR_CHAR)?;
|
|
self.write_delim(syntax::DICT_START_CHAR)?;
|
|
self.set_first_element();
|
|
Ok(self)
|
|
}
|
|
}
|
|
|
|
impl<'a, W: Write, C: Config> ser::SerializeSeq for &'a mut Serializer<W, C> {
|
|
type Ok = ();
|
|
type Error = Error;
|
|
|
|
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
|
|
where
|
|
T: ?Sized + Serialize,
|
|
{
|
|
if self.check_not_first_element() {
|
|
self.write_delim(syntax::LIST_SEPARATOR_CHAR)?;
|
|
}
|
|
value.serialize(&mut **self)
|
|
}
|
|
|
|
fn end(self) -> Result<()> {
|
|
self.write_delim(syntax::LIST_END_CHAR)
|
|
}
|
|
}
|
|
|
|
impl<'a, W: Write, C: Config> ser::SerializeTuple for &'a mut Serializer<W, C> {
|
|
type Ok = ();
|
|
type Error = Error;
|
|
|
|
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
|
|
where
|
|
T: ?Sized + Serialize,
|
|
{
|
|
if self.check_not_first_element() {
|
|
self.write_delim(syntax::LIST_SEPARATOR_CHAR)?;
|
|
}
|
|
value.serialize(&mut **self)
|
|
}
|
|
|
|
fn end(self) -> Result<()> {
|
|
self.write_delim(syntax::LIST_END_CHAR)
|
|
}
|
|
}
|
|
|
|
impl<'a, W: Write, C: Config> ser::SerializeTupleStruct for &'a mut Serializer<W, C> {
|
|
type Ok = ();
|
|
type Error = Error;
|
|
|
|
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
|
|
where
|
|
T: ?Sized + Serialize,
|
|
{
|
|
if self.check_not_first_element() {
|
|
self.write_delim(syntax::LIST_SEPARATOR_CHAR)?;
|
|
}
|
|
value.serialize(&mut **self)
|
|
}
|
|
|
|
fn end(self) -> Result<()> {
|
|
self.write_delim(syntax::LIST_END_CHAR)?;
|
|
tag_end!(self, C::TAG_TUPLE_STRUCTS)
|
|
}
|
|
}
|
|
|
|
impl<'a, W: Write, C: Config> ser::SerializeTupleVariant for &'a mut Serializer<W, C> {
|
|
type Ok = ();
|
|
type Error = Error;
|
|
|
|
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
|
|
where
|
|
T: ?Sized + Serialize,
|
|
{
|
|
if self.check_not_first_element() {
|
|
self.write_delim(syntax::LIST_SEPARATOR_CHAR)?;
|
|
}
|
|
value.serialize(&mut **self)
|
|
}
|
|
|
|
fn end(self) -> Result<()> {
|
|
self.write_delim(syntax::LIST_END_CHAR)?;
|
|
self.write_delim(syntax::DICT_END_CHAR)?;
|
|
tag_end!(self, C::TAG_TUPLE_VARIANTS)
|
|
}
|
|
}
|
|
|
|
|
|
impl<'a, W: Write, C: Config> ser::SerializeMap for &'a mut Serializer<W, C> {
|
|
type Ok = ();
|
|
type Error = Error;
|
|
|
|
fn serialize_key<T>(&mut self, key: &T) -> Result<()>
|
|
where
|
|
T: ?Sized + Serialize,
|
|
{
|
|
if self.check_not_first_element() {
|
|
self.write_delim(syntax::DICT_PAIR_SEPARATOR_CHAR)?;
|
|
}
|
|
key.serialize(&mut **self)
|
|
}
|
|
|
|
fn serialize_value<T>(&mut self, value: &T) -> Result<()>
|
|
where
|
|
T: ?Sized + Serialize,
|
|
{
|
|
self.write_delim(syntax::DICT_KV_SEPARATOR_CHAR)?;
|
|
value.serialize(&mut **self)
|
|
}
|
|
|
|
fn end(self) -> Result<()> {
|
|
self.write_delim(syntax::DICT_END_CHAR)
|
|
}
|
|
}
|
|
|
|
impl<'a, W: Write, C: Config> ser::SerializeStruct for &'a mut Serializer<W, C> {
|
|
type Ok = ();
|
|
type Error = Error;
|
|
|
|
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
|
|
where
|
|
T: ?Sized + Serialize,
|
|
{
|
|
if self.check_not_first_element() {
|
|
self.write_delim(syntax::DICT_PAIR_SEPARATOR_CHAR)?;
|
|
}
|
|
key.serialize(&mut **self)?;
|
|
self.write_delim(syntax::DICT_KV_SEPARATOR_CHAR)?;
|
|
value.serialize(&mut **self)
|
|
}
|
|
|
|
fn end(self) -> Result<()> {
|
|
self.write_delim(syntax::DICT_END_CHAR)?;
|
|
tag_end!(self, C::TAG_STRUCTS)
|
|
}
|
|
}
|
|
|
|
impl<'a, W: Write, C: Config> ser::SerializeStructVariant for &'a mut Serializer<W, C> {
|
|
type Ok = ();
|
|
type Error = Error;
|
|
|
|
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
|
|
where
|
|
T: ?Sized + Serialize,
|
|
{
|
|
if self.check_not_first_element() {
|
|
self.write_delim(syntax::DICT_PAIR_SEPARATOR_CHAR)?;
|
|
}
|
|
key.serialize(&mut **self)?;
|
|
self.write_delim(syntax::DICT_KV_SEPARATOR_CHAR)?;
|
|
value.serialize(&mut **self)
|
|
}
|
|
|
|
fn end(self) -> Result<()> {
|
|
self.write_delim(syntax::DICT_END_CHAR)?;
|
|
self.write_delim(syntax::DICT_END_CHAR)?;
|
|
tag_end!(self, C::TAG_STRUCT_VARIANTS)
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#[test]
|
|
fn test_struct() {
|
|
#[derive(Serialize)]
|
|
struct Test {
|
|
int: u32,
|
|
seq: Vec<&'static str>,
|
|
}
|
|
|
|
let test = Test {
|
|
int: 1,
|
|
seq: vec!["a", "b"],
|
|
};
|
|
let expected = r#"{"int":1,"seq":["a","b"]}"#;
|
|
assert_eq!(to_string(&test).unwrap(), expected);
|
|
}
|
|
|
|
#[test]
|
|
fn test_enum() {
|
|
#[derive(Serialize)]
|
|
enum E {
|
|
Unit,
|
|
Newtype(u32),
|
|
Tuple(u32, u32),
|
|
Struct { a: u32 },
|
|
}
|
|
|
|
let u = E::Unit;
|
|
let expected = r#""Unit""#;
|
|
assert_eq!(to_string(&u).unwrap(), expected);
|
|
|
|
let n = E::Newtype(1);
|
|
let expected = r#"{"Newtype":1}"#;
|
|
assert_eq!(to_string(&n).unwrap(), expected);
|
|
|
|
let t = E::Tuple(1, 2);
|
|
let expected = r#"{"Tuple":[1,2]}"#;
|
|
assert_eq!(to_string(&t).unwrap(), expected);
|
|
|
|
let s = E::Struct { a: 1 };
|
|
let expected = r#"{"Struct":{"a":1}}"#;
|
|
assert_eq!(to_string(&s).unwrap(), expected);
|
|
}
|
|
|
|
#[test]
|
|
fn test_simple() {
|
|
#[derive(Serialize)]
|
|
#[serde(rename = "Test/Unit")]
|
|
struct TestUnitStruct;
|
|
|
|
#[derive(Serialize)]
|
|
#[serde(rename = "Test/NewtypeStruct")]
|
|
struct TestNewtypeStruct(i8);
|
|
|
|
#[derive(Serialize)]
|
|
#[serde(rename = "")]
|
|
struct TestTransparentUnitStruct;
|
|
|
|
#[derive(Serialize)]
|
|
#[serde(transparent)]
|
|
struct TestTransparentNewtypeStruct(i8);
|
|
|
|
assert_eq!(to_string(&TestUnitStruct).unwrap(), "Test/Unit");
|
|
assert_eq!(to_string(&TestNewtypeStruct(1)).unwrap(), "Test/NewtypeStruct(1)");
|
|
assert_eq!(to_string(&TestTransparentUnitStruct).unwrap(), "");
|
|
assert_eq!(to_string(&TestTransparentNewtypeStruct(1)).unwrap(), "1");
|
|
} |