Trait Expected
trait Expected
Expected represents an explanation of what data a Visitor was expecting
to receive.
This is used as an argument to the invalid_type, invalid_value, and
invalid_length methods of the Error trait to build error messages. The
message should be a noun or noun phrase that completes the sentence "This
Visitor expects to receive ...", for example the message could be "an
integer between 0 and 64". The message should not be capitalized and should
not end with a period.
Within the context of a Visitor implementation, the Visitor itself
(&self) is an implementation of this trait.
# use serde::de::{self, Unexpected, Visitor};
# use std::fmt;
#
# struct Example;
#
# impl<'de> Visitor<'de> for Example {
# type Value = ();
#
# fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
# write!(formatter, "definitely not a boolean")
# }
#
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
where
E: de::Error,
{
Err(de::Error::invalid_type(Unexpected::Bool(v), &self))
}
# }
Outside of a Visitor, &"..." can be used.
# use serde::de::{self, Unexpected};
#
# fn example<E>() -> Result<(), E>
# where
# E: de::Error,
# {
# let v = true;
return Err(de::Error::invalid_type(
Unexpected::Bool(v),
&"a negative integer",
));
# }
Required Methods
fn fmt(self: &Self, formatter: &mut Formatter<'_>) -> ResultFormat an explanation of what data was being expected. Same signature as the
DisplayandDebugtraits.
Implementors
impl Expected for &strimpl<'de, T> Expected for IgnoredAnyimpl<'de, T> Expected for T