use core::fmt;
use core::marker::PhantomData;
use crate::expecting::{self, Expecting};
use crate::Context;
use super::{Decoder, MapDecoder, SequenceDecoder, SizeHint, UnsizedVisitor, VariantDecoder};
pub trait Visitor<'de, C: ?Sized + Context>: Sized {
type Ok;
type String: UnsizedVisitor<'de, C, str, Ok = Self::Ok>;
type Bytes: UnsizedVisitor<'de, C, [u8], Ok = Self::Ok>;
#[doc(hidden)]
type __UseMusliVisitorAttributeMacro;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
#[inline]
fn visit_empty(self, cx: &C) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Empty,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_bool(self, cx: &C, _: bool) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Bool,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_char(self, cx: &C, _: char) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Char,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_u8(self, cx: &C, _: u8) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Unsigned8,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_u16(self, cx: &C, _: u16) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Unsigned16,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_u32(self, cx: &C, _: u32) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Unsigned32,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_u64(self, cx: &C, _: u64) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Unsigned64,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_u128(self, cx: &C, _: u128) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Unsigned128,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_i8(self, cx: &C, _: i8) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Signed8,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_i16(self, cx: &C, _: i16) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Signed16,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_i32(self, cx: &C, _: i32) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Signed32,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_i64(self, cx: &C, _: i64) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Signed64,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_i128(self, cx: &C, _: i128) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Signed128,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_usize(self, cx: &C, _: usize) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Usize,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_isize(self, cx: &C, _: isize) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Isize,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_f32(self, cx: &C, _: f32) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Float32,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_f64(self, cx: &C, _: f64) -> Result<Self::Ok, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::Float64,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_option<D>(self, cx: &C, _: Option<D>) -> Result<Self::Ok, C::Error>
where
D: Decoder<'de, Cx = C, Error = C::Error, Mode = C::Mode>,
{
Err(cx.message(expecting::unsupported_type(
&expecting::Option,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_sequence<D>(self, cx: &C, decoder: &mut D) -> Result<Self::Ok, C::Error>
where
D: ?Sized + SequenceDecoder<'de, Cx = C>,
{
Err(cx.message(expecting::unsupported_type(
&expecting::SequenceWith(decoder.size_hint()),
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_map<D>(self, cx: &C, decoder: &mut D) -> Result<Self::Ok, <D::Cx as Context>::Error>
where
D: ?Sized + MapDecoder<'de, Cx = C>,
{
Err(cx.message(expecting::unsupported_type(
&expecting::MapWith(decoder.size_hint()),
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_string(self, cx: &C, hint: SizeHint) -> Result<Self::String, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::StringWith(hint),
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_bytes(self, cx: &C, hint: SizeHint) -> Result<Self::Bytes, C::Error> {
Err(cx.message(expecting::unsupported_type(
&expecting::BytesWith(hint),
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_variant<D>(self, cx: &C, _: &mut D) -> Result<Self::Ok, C::Error>
where
D: VariantDecoder<'de, Cx = C>,
{
Err(cx.message(expecting::unsupported_type(
&expecting::Variant,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn visit_unknown<D>(self, cx: &D::Cx, _: D) -> Result<Self::Ok, D::Error>
where
D: Decoder<'de, Cx = C, Error = C::Error, Mode = C::Mode>,
{
Err(cx.message(expecting::unsupported_type(
&expecting::Any,
ExpectingWrapper::new(&self),
)))
}
}
#[repr(transparent)]
struct ExpectingWrapper<'a, T, C: ?Sized> {
inner: T,
_marker: PhantomData<&'a C>,
}
impl<'a, T, C: ?Sized> ExpectingWrapper<'a, T, C> {
fn new(inner: &T) -> &Self {
unsafe { &*(inner as *const T as *const Self) }
}
}
impl<'a, 'de, T, C> Expecting for ExpectingWrapper<'a, T, C>
where
C: ?Sized + Context,
T: Visitor<'de, C>,
{
#[inline]
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.expecting(f)
}
}