#[cfg(feature = "serde_ron_serializer")]
mod serde_ron_serializer;
#[cfg(feature = "serde_json_serializer")]
mod serde_serializer;
use std::marker::PhantomData;
use std::str::FromStr;
#[cfg(feature = "serde_ron_serializer")]
pub use serde_ron_serializer::SerdeRonSerializer;
#[cfg(feature = "serde_json_serializer")]
pub use serde_serializer::SerdeSerializer;
use crate::Serializer;
pub struct ByteSerializer {
ext: &'static str,
}
impl ByteSerializer {
#[coverage(off)]
pub fn new(ext: &'static str) -> Self {
Self { ext }
}
}
impl crate::traits::Serializer for ByteSerializer {
type Value = Vec<u8>;
#[coverage(off)]
fn extension(&self) -> &str {
self.ext
}
#[coverage(off)]
fn from_data(&self, data: &[u8]) -> Option<Self::Value> {
Some(data.into())
}
#[coverage(off)]
fn to_data(&self, value: &Self::Value) -> Vec<u8> {
value.clone()
}
}
pub struct StringSerializer<StringType>
where
StringType: ToString + FromStr,
{
pub extension: &'static str,
_phantom: PhantomData<StringType>,
}
impl<StringType> StringSerializer<StringType>
where
StringType: ToString + FromStr,
{
#[coverage(off)]
pub fn new(extension: &'static str) -> Self {
Self {
extension,
_phantom: PhantomData,
}
}
}
impl<StringType> Serializer for StringSerializer<StringType>
where
StringType: ToString + FromStr,
{
type Value = StringType;
#[coverage(off)]
fn extension(&self) -> &str {
self.extension
}
#[coverage(off)]
fn from_data(&self, data: &[u8]) -> Option<Self::Value> {
let string = String::from_utf8(data.to_vec()).ok()?;
let value = Self::Value::from_str(&string).ok()?;
Some(value)
}
#[coverage(off)]
fn to_data(&self, value: &Self::Value) -> Vec<u8> {
value.to_string().into_bytes()
}
}