use serde::{ser, Serialize};
use crate::error::{Error, Result};
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, _: bool) -> Result<()> {
Err(Error::NotSupported)
}
fn serialize_i8(self, v: i8) -> Result<()> {
self.serialize_i64(i64::from(v))
}
fn serialize_i16(self, v: i16) -> Result<()> {
self.serialize_i64(i64::from(v))
}
fn serialize_i32(self, v: i32) -> Result<()> {
self.serialize_i64(i64::from(v))
}
fn serialize_i64(self, v: i64) -> Result<()> {
self.output += &v.to_string();
Ok(())
}
fn serialize_u8(self, v: u8) -> Result<()> {
self.serialize_u64(u64::from(v))
}
fn serialize_u16(self, v: u16) -> Result<()> {
self.serialize_u64(u64::from(v))
}
fn serialize_u32(self, v: u32) -> Result<()> {
self.serialize_u64(u64::from(v))
}
fn serialize_u64(self, v: u64) -> Result<()> {
self.output += &v.to_string();
Ok(())
}
fn serialize_f32(self, v: f32) -> Result<()> {
self.serialize_f64(f64::from(v))
}
fn serialize_f64(self, _v: f64) -> Result<()> {
Err(Error::NotSupported)
}
fn serialize_char(self, v: char) -> Result<()> {
self.serialize_str(&v.to_string())
}
fn serialize_str(self, v: &str) -> Result<()> {
self.output += v;
Ok(())
}
fn serialize_bytes(self, _v: &[u8]) -> Result<()> {
Err(Error::NotSupported)
}
fn serialize_none(self) -> Result<()> {
self.serialize_unit()
}
fn serialize_some<T>(self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
value.serialize(self)
}
fn serialize_unit(self) -> Result<()> {
self.output += "\n";
Ok(())
}
fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
self.serialize_unit()
}
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,
{
value.serialize(self)
}
fn serialize_newtype_variant<T>(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
value: &T,
) -> Result<()>
where
T: ?Sized + Serialize,
{
self.output += "%";
variant.serialize(&mut *self)?;
self.output += "%";
value.serialize(&mut *self)?;
self.output += "\n";
Ok(())
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
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> {
self.serialize_seq(Some(len))
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant> {
Err(Error::NotSupported)
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
Ok(self)
}
fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
self.serialize_map(Some(len))
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant> {
Err(Error::NotSupported)
}
}
impl<'a> ser::SerializeSeq for &'a mut Serializer {
type Ok = ();
type Error = Error;
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
value.serialize(&mut **self)?;
self.output += "\n";
Ok(())
}
fn end(self) -> Result<()> {
self.output = self.output[..self.output.len() - 1].to_owned(); Ok(())
}
}
impl<'a> ser::SerializeTuple for &'a mut Serializer {
type Ok = ();
type Error = Error;
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
value.serialize(&mut **self)?;
self.output += "\n";
Ok(())
}
fn end(self) -> Result<()> {
self.output = self.output[..self.output.len() - 1].to_owned(); Ok(())
}
}
impl<'a> ser::SerializeTupleStruct for &'a mut Serializer {
type Ok = ();
type Error = Error;
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
value.serialize(&mut **self)?;
self.output += "\n";
Ok(())
}
fn end(self) -> Result<()> {
self.output = self.output[..self.output.len() - 1].to_owned(); Ok(())
}
}
impl<'a> ser::SerializeTupleVariant for &'a mut Serializer {
type Ok = ();
type Error = Error;
fn serialize_field<T>(&mut self, _: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
Err(Error::NotSupported)
}
fn end(self) -> Result<()> {
Err(Error::NotSupported)
}
}
impl<'a> ser::SerializeMap for &'a mut Serializer {
type Ok = ();
type Error = Error;
fn serialize_key<T>(&mut self, key: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
self.output += "%";
key.serialize(&mut **self)?;
self.output += "%";
Ok(())
}
fn serialize_value<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
self.output += "\n";
value.serialize(&mut **self)?;
self.output += "\n";
self.output += "\n";
Ok(())
}
fn end(self) -> Result<()> {
Ok(())
}
}
impl<'a> ser::SerializeStruct for &'a mut Serializer {
type Ok = ();
type Error = Error;
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
self.output += "%";
key.serialize(&mut **self)?;
self.output += "%";
self.output += "\n";
value.serialize(&mut **self)?;
self.output += "\n";
self.output += "\n";
Ok(())
}
fn end(self) -> Result<()> {
Ok(())
}
}
impl<'a> ser::SerializeStructVariant for &'a mut Serializer {
type Ok = ();
type Error = Error;
fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
Err(Error::NotSupported)
}
fn end(self) -> Result<()> {
Err(Error::NotSupported)
}
}
#[cfg(test)]
mod test {
use serde::Serialize;
#[test]
fn test_struct() {
#[derive(Serialize, PartialEq, Debug)]
struct Test {
#[serde(rename = "NAME")]
name: String,
#[serde(rename = "BUILDDATE")]
build_date: u32,
#[serde(rename = "DEPENDS")]
depends: Vec<String>,
}
let j = r#"%NAME%
mingw-w64-x86_64-vcdimager
%BUILDDATE%
1592300880
%DEPENDS%
mingw-w64-x86_64-libcdio
mingw-w64-x86_64-libxml2
mingw-w64-x86_64-popt
"#;
let val = Test {
build_date: 1592300880,
depends: vec![
"mingw-w64-x86_64-libcdio".to_owned(),
"mingw-w64-x86_64-libxml2".to_owned(),
"mingw-w64-x86_64-popt".to_owned(),
],
name: "mingw-w64-x86_64-vcdimager".to_owned(),
};
assert_eq!(crate::to_string(&val).unwrap(), j);
}
}