pub trait SerializerConfig: sealed::SerializerConfig {}
impl<T: sealed::SerializerConfig> SerializerConfig for T {}
pub(crate) mod sealed {
use crate::config::BytesMode;
pub trait SerializerConfig: Copy {
fn is_human_readable(&self) -> bool;
fn is_named(&self) -> bool;
fn bytes(&self) -> BytesMode;
}
}
#[derive(Copy, Clone, Debug)]
pub(crate) struct RuntimeConfig {
pub(crate) is_human_readable: bool,
pub(crate) is_named: bool,
pub(crate) bytes: BytesMode,
}
#[non_exhaustive]
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub enum BytesMode {
#[default]
Normal,
ForceIterables,
ForceAll,
}
impl RuntimeConfig {
pub(crate) fn new(other: impl sealed::SerializerConfig) -> Self {
Self {
is_human_readable: other.is_human_readable(),
is_named: other.is_named(),
bytes: other.bytes(),
}
}
}
impl sealed::SerializerConfig for RuntimeConfig {
#[inline]
fn is_human_readable(&self) -> bool {
self.is_human_readable
}
#[inline]
fn is_named(&self) -> bool {
self.is_named
}
#[inline]
fn bytes(&self) -> BytesMode {
self.bytes
}
}
#[derive(Copy, Clone, Debug)]
pub struct DefaultConfig;
impl sealed::SerializerConfig for DefaultConfig {
#[inline(always)]
fn is_named(&self) -> bool {
false
}
#[inline(always)]
fn is_human_readable(&self) -> bool {
false
}
#[inline(always)]
fn bytes(&self) -> BytesMode {
BytesMode::default()
}
}
#[derive(Copy, Clone, Debug)]
pub struct StructMapConfig<C>(C);
impl<C> StructMapConfig<C> {
#[inline]
pub fn new(inner: C) -> Self {
StructMapConfig(inner)
}
}
impl<C> sealed::SerializerConfig for StructMapConfig<C>
where
C: sealed::SerializerConfig,
{
#[inline(always)]
fn is_named(&self) -> bool {
true
}
#[inline(always)]
fn is_human_readable(&self) -> bool {
self.0.is_human_readable()
}
fn bytes(&self) -> BytesMode {
self.0.bytes()
}
}
#[derive(Copy, Clone, Debug)]
pub struct StructTupleConfig<C>(C);
impl<C> StructTupleConfig<C> {
#[inline]
pub fn new(inner: C) -> Self {
StructTupleConfig(inner)
}
}
impl<C> sealed::SerializerConfig for StructTupleConfig<C>
where
C: sealed::SerializerConfig,
{
#[inline(always)]
fn is_named(&self) -> bool {
false
}
#[inline(always)]
fn is_human_readable(&self) -> bool {
self.0.is_human_readable()
}
fn bytes(&self) -> BytesMode {
self.0.bytes()
}
}
#[derive(Copy, Clone, Debug)]
pub struct HumanReadableConfig<C>(C);
impl<C> HumanReadableConfig<C> {
#[inline]
pub fn new(inner: C) -> Self {
Self(inner)
}
}
impl<C> sealed::SerializerConfig for HumanReadableConfig<C>
where
C: sealed::SerializerConfig,
{
#[inline(always)]
fn is_named(&self) -> bool {
self.0.is_named()
}
#[inline(always)]
fn is_human_readable(&self) -> bool {
true
}
fn bytes(&self) -> BytesMode {
self.0.bytes()
}
}
#[derive(Copy, Clone, Debug)]
pub struct BinaryConfig<C>(C);
impl<C> BinaryConfig<C> {
#[inline(always)]
pub fn new(inner: C) -> Self {
Self(inner)
}
}
impl<C> sealed::SerializerConfig for BinaryConfig<C>
where
C: sealed::SerializerConfig,
{
#[inline(always)]
fn is_named(&self) -> bool {
self.0.is_named()
}
#[inline(always)]
fn is_human_readable(&self) -> bool {
false
}
fn bytes(&self) -> BytesMode {
self.0.bytes()
}
}