use serde::ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer};
use crate::mat::error::MatError;
use crate::mat::options::Options;
use super::emit::{emit_file, emit_file_to};
use super::emit_with_builder::{emit_file_with_options, emit_file_with_options_to};
use super::value_ser::{ValueSerializer, to_value};
use crate::mat::value::MatValue;
pub fn to_bytes<T: Serialize + ?Sized>(value: &T) -> Result<Vec<u8>, MatError> {
let options = Options::default();
let fields = value.serialize(RootSerializer::new(&options))?;
emit_file(fields)
}
pub fn to_bytes_with_options<T: Serialize + ?Sized>(
value: &T,
options: &Options,
) -> Result<Vec<u8>, MatError> {
let fields = value.serialize(RootSerializer::new(options))?;
emit_file_with_options(fields, options)
}
pub fn to_writer<T: Serialize + ?Sized, W: std::io::Write>(
value: &T,
w: W,
) -> Result<(), MatError> {
let options = Options::default();
let fields = value.serialize(RootSerializer::new(&options))?;
emit_file_to(fields, w)
}
pub fn to_writer_with_options<T: Serialize + ?Sized, W: std::io::Write>(
value: &T,
options: &Options,
w: W,
) -> Result<(), MatError> {
let fields = value.serialize(RootSerializer::new(options))?;
emit_file_with_options_to(fields, options, w)
}
pub fn to_path<T: Serialize + ?Sized, P: AsRef<std::path::Path>>(
value: &T,
path: P,
) -> Result<(), MatError> {
let options = Options::default();
let fields = value.serialize(RootSerializer::new(&options))?;
emit_file_to(fields, create(path)?)
}
pub fn to_path_with_options<T: Serialize + ?Sized, P: AsRef<std::path::Path>>(
value: &T,
path: P,
options: &Options,
) -> Result<(), MatError> {
let fields = value.serialize(RootSerializer::new(options))?;
emit_file_with_options_to(fields, options, create(path)?)
}
fn create<P: AsRef<std::path::Path>>(path: P) -> Result<std::fs::File, MatError> {
std::fs::File::create(path).map_err(MatError::Io)
}
pub(crate) struct RootSerializer<'a> {
opts: &'a Options,
}
impl<'a> RootSerializer<'a> {
pub(crate) fn new(opts: &'a Options) -> Self {
Self { opts }
}
fn root_null(self) -> Result<Vec<(String, MatValue)>, MatError> {
super::value_ser::null_value(self.opts)?;
Ok(Vec::new())
}
}
impl<'a> Serializer for RootSerializer<'a> {
type Ok = Vec<(String, MatValue)>;
type Error = MatError;
type SerializeSeq = Impossible<Vec<(String, MatValue)>, MatError>;
type SerializeTuple = Impossible<Vec<(String, MatValue)>, MatError>;
type SerializeTupleStruct = Impossible<Vec<(String, MatValue)>, MatError>;
type SerializeTupleVariant = Impossible<Vec<(String, MatValue)>, MatError>;
type SerializeMap = RootMapSer<'a>;
type SerializeStruct = RootStructSer<'a>;
type SerializeStructVariant = Impossible<Vec<(String, MatValue)>, MatError>;
fn serialize_bool(self, _: bool) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_i8(self, _: i8) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_i16(self, _: i16) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_i32(self, _: i32) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_i64(self, _: i64) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_i128(self, _: i128) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_u8(self, _: u8) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_u16(self, _: u16) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_u32(self, _: u32) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_u64(self, _: u64) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_u128(self, _: u128) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_f32(self, _: f32) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_f64(self, _: f64) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_char(self, _: char) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_str(self, _: &str) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_none(self) -> Result<Self::Ok, MatError> {
self.root_null()
}
fn serialize_some<T: Serialize + ?Sized>(self, value: &T) -> Result<Self::Ok, MatError> {
value.serialize(self)
}
fn serialize_unit(self) -> Result<Self::Ok, MatError> {
self.root_null()
}
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, MatError> {
self.root_null()
}
fn serialize_unit_variant(
self,
_name: &'static str,
_idx: u32,
_variant: &'static str,
) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_newtype_struct<T: Serialize + ?Sized>(
self,
_name: &'static str,
value: &T,
) -> Result<Self::Ok, MatError> {
value.serialize(self)
}
fn serialize_newtype_variant<T: Serialize + ?Sized>(
self,
_name: &'static str,
_idx: u32,
_variant: &'static str,
_value: &T,
) -> Result<Self::Ok, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_idx: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant, MatError> {
Err(MatError::RootMustBeStruct)
}
fn serialize_map(self, _len: Option<usize>) -> Result<RootMapSer<'a>, MatError> {
Ok(RootMapSer::new(self.opts))
}
fn serialize_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<RootStructSer<'a>, MatError> {
Ok(RootStructSer::new(self.opts))
}
fn serialize_struct_variant(
self,
_name: &'static str,
_idx: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, MatError> {
Err(MatError::RootMustBeStruct)
}
}
pub(crate) struct RootStructSer<'a> {
fields: Vec<(String, MatValue)>,
opts: &'a Options,
}
impl<'a> RootStructSer<'a> {
fn new(opts: &'a Options) -> Self {
Self {
fields: Vec::new(),
opts,
}
}
}
impl SerializeStruct for RootStructSer<'_> {
type Ok = Vec<(String, MatValue)>;
type Error = MatError;
fn serialize_field<T: Serialize + ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), MatError> {
let v = to_value(value, self.opts)?;
if !matches!(v, MatValue::Omit) {
self.fields.push((key.to_owned(), v));
}
Ok(())
}
fn end(self) -> Result<Vec<(String, MatValue)>, MatError> {
Ok(self.fields)
}
}
pub(crate) struct RootMapSer<'a> {
fields: Vec<(String, MatValue)>,
pending_key: Option<String>,
opts: &'a Options,
}
impl<'a> RootMapSer<'a> {
fn new(opts: &'a Options) -> Self {
Self {
fields: Vec::new(),
pending_key: None,
opts,
}
}
}
impl SerializeMap for RootMapSer<'_> {
type Ok = Vec<(String, MatValue)>;
type Error = MatError;
fn serialize_key<T: Serialize + ?Sized>(&mut self, key: &T) -> Result<(), MatError> {
let key_val = key.serialize(ValueSerializer::new(self.opts))?;
match key_val {
MatValue::String(s) => {
self.pending_key = Some(s);
Ok(())
}
other => Err(MatError::Custom(format!(
"root map keys must be strings, got {}",
other.kind()
))),
}
}
fn serialize_value<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), MatError> {
let k = self
.pending_key
.take()
.ok_or_else(|| MatError::Custom("serialize_value before serialize_key".into()))?;
let v = to_value(value, self.opts)?;
if !matches!(v, MatValue::Omit) {
self.fields.push((k, v));
}
Ok(())
}
fn end(self) -> Result<Vec<(String, MatValue)>, MatError> {
Ok(self.fields)
}
}
#[allow(dead_code)]
fn _touch<E: ser::Error>() -> E {
E::custom("x")
}