use escape8259::escape;
use serde::{ser, Serialize};
use std::{error, fmt::Display};
pub struct Serializer {
output: String,
}
#[derive(Debug)]
pub struct JaclSerError;
impl Display for JaclSerError {
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
unreachable!();
}
}
impl error::Error for JaclSerError {}
impl ser::Error for JaclSerError {
fn custom<T>(_: T) -> Self
where
T: std::fmt::Display,
{
unreachable!();
}
}
pub fn to_string<T>(value: &T) -> Result<String, JaclSerError>
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 = JaclSerError;
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<(), JaclSerError> {
self.output += if v { "true" } else { "false" };
Ok(())
}
fn serialize_i8(self, v: i8) -> Result<(), JaclSerError> {
self.serialize_i64(i64::from(v))
}
fn serialize_i16(self, v: i16) -> Result<(), JaclSerError> {
self.serialize_i64(i64::from(v))
}
fn serialize_i32(self, v: i32) -> Result<(), JaclSerError> {
self.serialize_i64(i64::from(v))
}
fn serialize_i64(self, v: i64) -> Result<(), JaclSerError> {
self.output += &v.to_string();
Ok(())
}
fn serialize_u8(self, v: u8) -> Result<(), JaclSerError> {
self.serialize_u64(u64::from(v))
}
fn serialize_u16(self, v: u16) -> Result<(), JaclSerError> {
self.serialize_u64(u64::from(v))
}
fn serialize_u32(self, v: u32) -> Result<(), JaclSerError> {
self.serialize_u64(u64::from(v))
}
fn serialize_u64(self, v: u64) -> Result<(), JaclSerError> {
self.output += &v.to_string();
Ok(())
}
fn serialize_f32(self, v: f32) -> Result<(), JaclSerError> {
self.serialize_f64(f64::from(v))
}
fn serialize_f64(self, v: f64) -> Result<(), JaclSerError> {
self.output += &v.to_string();
Ok(())
}
fn serialize_char(self, v: char) -> Result<(), JaclSerError> {
self.serialize_str(&v.to_string())
}
fn serialize_str(self, v: &str) -> Result<(), JaclSerError> {
self.output += "\"";
self.output += &escape(v);
self.output += "\"";
Ok(())
}
fn serialize_bytes(self, v: &[u8]) -> Result<(), JaclSerError> {
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_none(self) -> Result<(), JaclSerError> {
self.serialize_unit()
}
fn serialize_some<T>(self, value: &T) -> Result<(), JaclSerError>
where
T: ?Sized + Serialize,
{
value.serialize(self)
}
fn serialize_unit(self) -> Result<(), JaclSerError> {
self.output += "null";
Ok(())
}
fn serialize_unit_struct(self, _name: &'static str) -> Result<(), JaclSerError> {
self.serialize_unit()
}
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
) -> Result<(), JaclSerError> {
return Err(JaclSerError);
}
fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<(), JaclSerError>
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<(), JaclSerError>
where
T: ?Sized + Serialize,
{
return Err(JaclSerError);
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, JaclSerError> {
self.output += "[";
Ok(self)
}
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, JaclSerError> {
self.serialize_seq(Some(len))
}
fn serialize_tuple_struct(
self,
_name: &'static str,
len: usize,
) -> Result<Self::SerializeTupleStruct, JaclSerError> {
self.serialize_seq(Some(len))
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant, JaclSerError> {
return Err(JaclSerError);
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, JaclSerError> {
self.output += "{";
Ok(self)
}
fn serialize_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct, JaclSerError> {
self.output += "(";
Ok(self)
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, JaclSerError> {
return Err(JaclSerError);
}
}
impl<'a> ser::SerializeTupleVariant for &'a mut Serializer {
type Ok = ();
type Error = JaclSerError;
fn serialize_field<T: ?Sized>(&mut self, _value: &T) -> Result<(), Self::Error>
where
T: Serialize,
{
return Err(JaclSerError);
}
fn end(self) -> Result<Self::Ok, Self::Error> {
return Err(JaclSerError);
}
}
impl<'a> ser::SerializeStructVariant for &'a mut Serializer {
type Ok = ();
type Error = JaclSerError;
fn serialize_field<T: ?Sized>(
&mut self,
_key: &'static str,
_value: &T,
) -> Result<(), Self::Error>
where
T: Serialize,
{
return Err(JaclSerError);
}
fn end(self) -> Result<Self::Ok, Self::Error> {
return Err(JaclSerError);
}
}
impl<'a> ser::SerializeSeq for &'a mut Serializer {
type Ok = ();
type Error = JaclSerError;
fn serialize_element<T>(&mut self, value: &T) -> Result<(), JaclSerError>
where
T: ?Sized + Serialize,
{
if !self.output.ends_with('[') {
self.output += " ";
}
value.serialize(&mut **self)
}
fn end(self) -> Result<(), JaclSerError> {
self.output += "]";
Ok(())
}
}
impl<'a> ser::SerializeTuple for &'a mut Serializer {
type Ok = ();
type Error = JaclSerError;
fn serialize_element<T>(&mut self, value: &T) -> Result<(), JaclSerError>
where
T: ?Sized + Serialize,
{
if !self.output.ends_with('[') {
self.output += " ";
}
value.serialize(&mut **self)
}
fn end(self) -> Result<(), JaclSerError> {
self.output += "]";
Ok(())
}
}
impl<'a> ser::SerializeTupleStruct for &'a mut Serializer {
type Ok = ();
type Error = JaclSerError;
fn serialize_field<T>(&mut self, value: &T) -> Result<(), JaclSerError>
where
T: ?Sized + Serialize,
{
if !self.output.ends_with('[') {
self.output += " ";
}
value.serialize(&mut **self)
}
fn end(self) -> Result<(), JaclSerError> {
self.output += "]";
Ok(())
}
}
impl<'a> ser::SerializeMap for &'a mut Serializer {
type Ok = ();
type Error = JaclSerError;
fn serialize_key<T>(&mut self, key: &T) -> Result<(), JaclSerError>
where
T: ?Sized + Serialize,
{
if !self.output.ends_with('{') {
self.output += " ";
}
key.serialize(&mut **self)
}
fn serialize_value<T>(&mut self, value: &T) -> Result<(), JaclSerError>
where
T: ?Sized + Serialize,
{
self.output += ":";
value.serialize(&mut **self)
}
fn end(self) -> Result<(), JaclSerError> {
self.output += "}";
Ok(())
}
}
impl<'a> ser::SerializeStruct for &'a mut Serializer {
type Ok = ();
type Error = JaclSerError;
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), JaclSerError>
where
T: ?Sized + Serialize,
{
if !self.output.ends_with('(') {
self.output += " ";
}
self.output += key;
self.output += ":";
value.serialize(&mut **self)
}
fn end(self) -> Result<(), JaclSerError> {
self.output += ")";
Ok(())
}
}
#[test]
fn test_struct() {
#[derive(Serialize)]
struct Test {
int: u32,
seq: Vec<&'static str>,
}
let test = Test {
int: 1,
seq: vec![r#" "a" "#, "b"],
};
assert_eq!(to_string(&test).unwrap(), r#"(int:1 seq:[" \"a\" " "b"])"#);
}