use serde::de::{Error as DeError, Visitor};
use std::{
convert::TryFrom,
fmt::{Formatter, Result as FmtResult},
marker::PhantomData,
};
pub mod null_boolean {
use serde::{
de::{Deserializer, Error as DeError, Visitor},
ser::Serializer,
};
use std::fmt::{Formatter, Result as FmtResult};
struct NullBooleanVisitor;
impl<'de> Visitor<'de> for NullBooleanVisitor {
type Value = bool;
fn expecting(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str("null")
}
fn visit_none<E: DeError>(self) -> Result<Self::Value, E> {
Ok(true)
}
fn visit_unit<E: DeError>(self) -> Result<Self::Value, E> {
Ok(true)
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn serialize<S: Serializer>(_: &bool, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_none()
}
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<bool, D::Error> {
deserializer.deserialize_option(NullBooleanVisitor)
}
}
pub struct U16EnumVisitor<'a> {
description: &'a str,
phantom: PhantomData<u16>,
}
impl<'a> U16EnumVisitor<'a> {
pub const fn new(description: &'a str) -> Self {
Self {
description,
phantom: PhantomData,
}
}
}
impl<'de> Visitor<'de> for U16EnumVisitor<'_> {
type Value = u16;
fn expecting(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str(self.description)
}
fn visit_u16<E: DeError>(self, value: u16) -> Result<Self::Value, E> {
Ok(value)
}
fn visit_u64<E: DeError>(self, value: u64) -> Result<Self::Value, E> {
let smaller = u16::try_from(value).map_err(E::custom)?;
self.visit_u16(smaller)
}
}