use core::fmt::{Display, Formatter};
use crate::de::Deserializer;
use super::Error;
#[inline]
fn display_from_fn<F>(f: F) -> impl Display
where
F: Fn(&mut Formatter<'_>) -> core::fmt::Result,
{
struct FromFn<F> {
inner: F,
}
impl<F> Display for FromFn<F>
where
F: Fn(&mut Formatter<'_>) -> core::fmt::Result,
{
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
(self.inner)(f)
}
}
FromFn { inner: f }
}
pub trait Visitor<'de>: Sized {
type Value;
fn expecting(&self, f: &mut Formatter<'_>) -> core::fmt::Result;
fn visit_comment<E>(self, value: &str) -> Result<Self::Value, E>
where
E: Error,
{
let _ = value;
Err(E::invalid_type(
"comment",
display_from_fn(|f| self.expecting(f)),
))
}
#[inline]
fn visit_comment_borrowed<E>(self, value: &'de str) -> Result<Self::Value, E>
where
E: Error,
{
self.visit_comment(value)
}
fn visit_class<'b, A>(self, access: A) -> Result<Self::Value, A::Error>
where
A: ClassAccess<'de, 'b>,
{
drop(access);
Err(A::Error::invalid_type(
"class",
display_from_fn(|f| self.expecting(f)),
))
}
#[inline]
fn visit_class_borrowed<A>(self, access: A) -> Result<Self::Value, A::Error>
where
A: ClassAccess<'de, 'de>,
{
self.visit_class(access)
}
fn visit_field<'b, A>(self, access: A) -> Result<Self::Value, A::Error>
where
A: FieldAccess<'de, 'b>,
{
drop(access);
Err(A::Error::invalid_type(
"field",
display_from_fn(|f| self.expecting(f)),
))
}
#[inline]
fn visit_field_borrowed<A>(self, access: A) -> Result<Self::Value, A::Error>
where
A: FieldAccess<'de, 'de>,
{
self.visit_field(access)
}
fn visit_method<'b, A>(self, access: A) -> Result<Self::Value, A::Error>
where
A: MethodAccess<'de, 'b>,
{
drop(access);
Err(A::Error::invalid_type(
"method",
display_from_fn(|f| self.expecting(f)),
))
}
#[inline]
fn visit_method_borrowed<A>(self, access: A) -> Result<Self::Value, A::Error>
where
A: MethodAccess<'de, 'de>,
{
self.visit_method(access)
}
fn visit_method_arg<'b, A>(self, access: A) -> Result<Self::Value, A::Error>
where
A: MethodArgAccess<'de, 'b>,
{
drop(access);
Err(A::Error::invalid_type(
"method argument",
display_from_fn(|f| self.expecting(f)),
))
}
#[inline]
fn visit_method_arg_borrowed<A>(self, access: A) -> Result<Self::Value, A::Error>
where
A: MethodArgAccess<'de, 'de>,
{
self.visit_method_arg(access)
}
fn visit_method_var<'b, A>(self, access: A) -> Result<Self::Value, A::Error>
where
A: MethodVarAccess<'de, 'b>,
{
drop(access);
Err(A::Error::invalid_type(
"method variable",
display_from_fn(|f| self.expecting(f)),
))
}
#[inline]
fn visit_method_var_borrowed<A>(self, access: A) -> Result<Self::Value, A::Error>
where
A: MethodVarAccess<'de, 'de>,
{
self.visit_method_var(access)
}
}
pub trait ClassAccess<'de, 's> {
type Error: Error;
type ContentDeserializer: Deserializer<'de, Error = Self::Error>;
fn src(&self) -> &'s str;
fn dst(&self) -> impl Iterator<Item = &'s str>;
fn content(self) -> Self::ContentDeserializer;
}
pub trait FieldAccess<'de, 's> {
type Error: Error;
type ContentDeserializer: Deserializer<'de, Error = Self::Error>;
fn src(&self) -> &'s str;
fn dst(&self) -> impl Iterator<Item = &'s str>;
fn desc(&self) -> Option<&'s str>;
fn dst_desc(&self) -> Option<impl Iterator<Item = &'s str>>;
fn content(self) -> Self::ContentDeserializer;
}
pub trait MethodAccess<'de, 's> {
type Error: Error;
type ContentDeserializer: Deserializer<'de, Error = Self::Error>;
fn src(&self) -> &'s str;
fn dst(&self) -> impl Iterator<Item = &'s str>;
fn desc(&self) -> Option<&'s str>;
fn dst_desc(&self) -> Option<impl Iterator<Item = &'s str>>;
fn content(self) -> Self::ContentDeserializer;
}
pub trait MethodArgAccess<'de, 's> {
type Error: Error;
type ContentDeserializer: Deserializer<'de, Error = Self::Error>;
fn src(&self) -> Option<&'s str>;
fn dst(&self) -> Option<impl Iterator<Item = &'s str>>;
fn pos(&self) -> Option<usize>;
#[doc(alias = "slot")]
fn lv_index(&self) -> Option<usize>;
fn content(self) -> Self::ContentDeserializer;
}
pub trait MethodVarAccess<'de, 's> {
type Error: Error;
type ContentDeserializer: Deserializer<'de, Error = Self::Error>;
fn src(&self) -> Option<&'s str>;
fn dst(&self) -> Option<impl Iterator<Item = &'s str>>;
#[doc(alias = "slot")]
fn lv_index(&self) -> Option<usize>;
fn lvt_row_index(&self) -> Option<usize>;
fn op_idx(&self) -> Option<(usize, Option<usize>)>;
fn content(self) -> Self::ContentDeserializer;
}