use {
byteorder::WriteBytesExt,
serde::ser::{self, Serialize},
std::{
fmt::Display,
io::{self, Write},
},
};
pub use self::errors::{CompatSerializationError, Result, SerializationError};
pub struct Serializer<'w, W>
where
W: WriteBytesExt + 'w,
{
writer: &'w mut W,
}
impl<'w, W> Serializer<'w, W>
where
W: WriteBytesExt + 'w,
{
pub fn new(writer: &'w mut W) -> Self {
Serializer { writer }
}
fn serialize_failure<T, S>(
type_name: &str,
value: T,
error: S,
) -> CompatSerializationError
where
T: Display,
S: Into<CompatSerializationError>,
{
SerializationError::Failure {
what: format!("a value {} of type {}", value, type_name),
cause: Box::new(error.into()),
}
.into()
}
fn serialize_opaque_failure<S>(
length: usize,
error: S,
) -> SerializationError
where
S: Into<CompatSerializationError>,
{
SerializationError::Failure {
what: format!("opaque data of length {}", length),
cause: Box::new(error.into()),
}
}
fn io_error<T>(
type_name: &str,
value: T,
error: io::Error,
) -> SerializationError
where
T: Display,
{
SerializationError::IoError {
what: format!("a value {} of type {}", value, type_name),
cause: error,
}
}
fn serialize_opaque_io_error(
length: usize,
error: io::Error,
) -> SerializationError {
SerializationError::IoError {
what: format!("opaque data of length {}", length),
cause: error,
}
}
}
mod serializer;
impl<'w, W> ser::SerializeMap for Serializer<'w, W>
where
W: WriteBytesExt + 'w,
{
type Ok = Self;
type Error = CompatSerializationError;
fn serialize_key<T>(&mut self, _key: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
unreachable!("map is not supported")
}
fn serialize_value<T>(&mut self, _value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
unreachable!("map is not supported")
}
fn end(self) -> Result<Self> {
unreachable!("map is not supported")
}
}
pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
where
T: Serialize,
{
let mut bytes = Vec::new();
value.serialize(Serializer::new(&mut bytes))?;
Ok(bytes)
}
pub fn to_writer<W, T>(writer: &mut W, value: &T) -> Result<()>
where
W: Write,
T: Serialize,
{
value.serialize(Serializer::new(writer))?;
Ok(())
}
mod errors;