pub enum Object<'obj, 'ser: 'obj> {
List(ListDecoder<'obj, 'ser>),
Dict(DictDecoder<'obj, 'ser>),
Integer(&'ser str),
Bytes(&'ser [u8]),
}Expand description
An object read from a decoder
Variants§
List(ListDecoder<'obj, 'ser>)
A list of arbitrary objects
Dict(DictDecoder<'obj, 'ser>)
A map of string-valued keys to arbitrary objects
Integer(&'ser str)
An unparsed integer
Bytes(&'ser [u8])
A byte string
Implementations§
Source§impl<'obj, 'ser: 'obj> Object<'obj, 'ser>
impl<'obj, 'ser: 'obj> Object<'obj, 'ser>
pub fn into_token(self) -> Token<'ser>
Sourcepub fn bytes_or<ErrorT>(
self,
default: Result<&'ser [u8], ErrorT>,
) -> Result<&'ser [u8], ErrorT>
pub fn bytes_or<ErrorT>( self, default: Result<&'ser [u8], ErrorT>, ) -> Result<&'ser [u8], ErrorT>
Try to treat the object as a byte string, mapping Object::Bytes(v) into
Ok(v). Any other variant returns the given default value.
Default arguments passed into bytes_or are eagerly evaluated; if you
are passing the result of a function call, it is recommended to use
bytes_or_else, which is lazily evaluated.
§Examples
use bendy::decoding::Object;
let x = Object::Bytes(b"foo");
assert_eq!(Ok(&b"foo"[..]), x.bytes_or(Err("failure")));
let x = Object::Integer("foo");
assert_eq!(Err("failure"), x.bytes_or(Err("failure")));Sourcepub fn bytes_or_else<ErrorT>(
self,
op: impl FnOnce(Self) -> Result<&'ser [u8], ErrorT>,
) -> Result<&'ser [u8], ErrorT>
pub fn bytes_or_else<ErrorT>( self, op: impl FnOnce(Self) -> Result<&'ser [u8], ErrorT>, ) -> Result<&'ser [u8], ErrorT>
Try to treat the object as a byte string, mapping Object::Bytes(v) into
Ok(v). Any other variant is passed into the given fallback method.
§Examples
use bendy::decoding::Object;
let x = Object::Bytes(b"foo");
assert_eq!(
Ok(&b"foo"[..]),
x.bytes_or_else(|obj| Err(obj.into_token().name()))
);
let x = Object::Integer("foo");
assert_eq!(
Err("Num"),
x.bytes_or_else(|obj| Err(obj.into_token().name()))
);Sourcepub fn try_into_bytes(self) -> Result<&'ser [u8], Error>
pub fn try_into_bytes(self) -> Result<&'ser [u8], Error>
Try to treat the object as a byte string, mapping Object::Bytes(v) into
Ok(v). Any other variant results in an ErrorKind::UnexpectedToken.
§Examples
use bendy::decoding::Object;
let x = Object::Bytes(b"foo");
assert_eq!(b"foo", x.try_into_bytes().unwrap());
let x = Object::Integer("foo");
assert!(x.try_into_bytes().is_err());Sourcepub fn integer_or<ErrorT>(
self,
default: Result<&'ser str, ErrorT>,
) -> Result<&'ser str, ErrorT>
pub fn integer_or<ErrorT>( self, default: Result<&'ser str, ErrorT>, ) -> Result<&'ser str, ErrorT>
Try to treat the object as an integer and return the internal string representation,
mapping Object::Integer(v) into Ok(v). Any other variant returns the given
default value.
Default arguments passed into integer_or are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use integer_or_else, which
is lazily evaluated.
§Examples
use bendy::decoding::Object;
let x = Object::Integer("123");
assert_eq!(Ok(&"123"[..]), x.integer_or(Err("failure")));
let x = Object::Bytes(b"foo");
assert_eq!(Err("failure"), x.integer_or(Err("failure")));Sourcepub fn integer_or_else<ErrorT>(
self,
op: impl FnOnce(Self) -> Result<&'ser str, ErrorT>,
) -> Result<&'ser str, ErrorT>
pub fn integer_or_else<ErrorT>( self, op: impl FnOnce(Self) -> Result<&'ser str, ErrorT>, ) -> Result<&'ser str, ErrorT>
Try to treat the object as an integer and return the internal string representation,
mapping Object::Integer(v) into Ok(v). Any other variant is passed into the
given fallback method.
§Examples
use bendy::decoding::Object;
let x = Object::Integer("123");
assert_eq!(
Ok(&"123"[..]),
x.integer_or_else(|obj| Err(obj.into_token().name()))
);
let x = Object::Bytes(b"foo");
assert_eq!(
Err("String"),
x.integer_or_else(|obj| Err(obj.into_token().name()))
);Sourcepub fn try_into_integer(self) -> Result<&'ser str, Error>
pub fn try_into_integer(self) -> Result<&'ser str, Error>
Try to treat the object as an integer and return the internal string representation,
mapping Object::Integer(v) into Ok(v). Any other variant results in an
ErrorKind::UnexpectedToken.
§Examples
use bendy::decoding::Object;
let x = Object::Integer("123");
assert_eq!("123", x.try_into_integer().unwrap());
let x = Object::Bytes(b"foo");
assert!(x.try_into_integer().is_err());Sourcepub fn list_or<ErrorT>(
self,
default: Result<ListDecoder<'obj, 'ser>, ErrorT>,
) -> Result<ListDecoder<'obj, 'ser>, ErrorT>
pub fn list_or<ErrorT>( self, default: Result<ListDecoder<'obj, 'ser>, ErrorT>, ) -> Result<ListDecoder<'obj, 'ser>, ErrorT>
Try to treat the object as a list and return the internal list content decoder,
mapping Object::List(v) into Ok(v). Any other variant returns the given
default value.
Default arguments passed into list_or are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use list_or_else, which is
lazily evaluated.
§Examples
use bendy::decoding::{Decoder, Object};
let mut list_decoder = Decoder::new(b"le");
let x = list_decoder.next_object().unwrap().unwrap();
assert!(x.list_or(Err("failure")).is_ok());
let x = Object::Bytes(b"foo");
assert_eq!("failure", x.list_or(Err("failure")).unwrap_err());Sourcepub fn list_or_else<ErrorT>(
self,
op: impl FnOnce(Self) -> Result<ListDecoder<'obj, 'ser>, ErrorT>,
) -> Result<ListDecoder<'obj, 'ser>, ErrorT>
pub fn list_or_else<ErrorT>( self, op: impl FnOnce(Self) -> Result<ListDecoder<'obj, 'ser>, ErrorT>, ) -> Result<ListDecoder<'obj, 'ser>, ErrorT>
Try to treat the object as a list and return the internal list content decoder,
mapping Object::List(v) into Ok(v). Any other variant is passed into the
given fallback method.
§Examples
use bendy::decoding::{Decoder, Object};
let mut list_decoder = Decoder::new(b"le");
let x = list_decoder.next_object().unwrap().unwrap();
assert!(x.list_or_else(|obj| Err(obj.into_token().name())).is_ok());
let x = Object::Bytes(b"foo");
assert_eq!(
"String",
x.list_or_else(|obj| Err(obj.into_token().name()))
.unwrap_err()
);Sourcepub fn try_into_list(self) -> Result<ListDecoder<'obj, 'ser>, Error>
pub fn try_into_list(self) -> Result<ListDecoder<'obj, 'ser>, Error>
Try to treat the object as a list and return the internal list content decoder,
mapping Object::List(v) into Ok(v). Any other variant results in an
ErrorKind::UnexpectedToken.
§Examples
use bendy::decoding::{Decoder, Object};
let mut list_decoder = Decoder::new(b"le");
let x = list_decoder.next_object().unwrap().unwrap();
assert!(x.try_into_list().is_ok());
let x = Object::Bytes(b"foo");
assert!(x.try_into_list().is_err());Sourcepub fn dictionary_or<ErrorT>(
self,
default: Result<DictDecoder<'obj, 'ser>, ErrorT>,
) -> Result<DictDecoder<'obj, 'ser>, ErrorT>
pub fn dictionary_or<ErrorT>( self, default: Result<DictDecoder<'obj, 'ser>, ErrorT>, ) -> Result<DictDecoder<'obj, 'ser>, ErrorT>
Try to treat the object as a dictionary and return the internal dictionary content
decoder, mapping Object::Dict(v) into Ok(v). Any other variant returns the
given default value.
Default arguments passed to dictionary_or are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use dictionary_or_else, which
is lazily evaluated.
§Examples
use bendy::decoding::{Decoder, Object};
let mut dict_decoder = Decoder::new(b"de");
let x = dict_decoder.next_object().unwrap().unwrap();
assert!(x.dictionary_or(Err("failure")).is_ok());
let x = Object::Bytes(b"foo");
assert_eq!("failure", x.dictionary_or(Err("failure")).unwrap_err());Sourcepub fn dictionary_or_else<ErrorT>(
self,
op: impl FnOnce(Self) -> Result<DictDecoder<'obj, 'ser>, ErrorT>,
) -> Result<DictDecoder<'obj, 'ser>, ErrorT>
pub fn dictionary_or_else<ErrorT>( self, op: impl FnOnce(Self) -> Result<DictDecoder<'obj, 'ser>, ErrorT>, ) -> Result<DictDecoder<'obj, 'ser>, ErrorT>
Try to treat the object as a dictionary and return the internal dictionary content
decoder, mapping Object::Dict(v) into Ok(v). Any other variant is passed
into the given fallback method.
§Examples
use bendy::decoding::{Decoder, Object};
let mut dict_decoder = Decoder::new(b"de");
let x = dict_decoder.next_object().unwrap().unwrap();
assert!(
x.dictionary_or_else(|obj| Err(obj.into_token().name()))
.is_ok()
);
let x = Object::Bytes(b"foo");
assert_eq!(
"String",
x.dictionary_or_else(|obj| Err(obj.into_token().name()))
.unwrap_err()
);Sourcepub fn try_into_dictionary(self) -> Result<DictDecoder<'obj, 'ser>, Error>
pub fn try_into_dictionary(self) -> Result<DictDecoder<'obj, 'ser>, Error>
Try to treat the object as a dictionary and return the internal dictionary content
decoder, mapping Object::Dict(v) into Ok(v). Any other variant results in
an ErrorKind::UnexpectedToken.
§Examples
use bendy::decoding::{Decoder, Object};
let mut dict_decoder = Decoder::new(b"de");
let x = dict_decoder.next_object().unwrap().unwrap();
assert!(x.try_into_dictionary().is_ok());
let x = Object::Bytes(b"foo");
assert!(x.try_into_dictionary().is_err());Trait Implementations§
Source§impl<'obj, 'ser> TryFrom<Object<'obj, 'ser>> for Inspectable<'ser>
impl<'obj, 'ser> TryFrom<Object<'obj, 'ser>> for Inspectable<'ser>
Source§fn try_from(
object: Object<'obj, 'ser>,
) -> Result<Inspectable<'ser>, DecodeError>
fn try_from( object: Object<'obj, 'ser>, ) -> Result<Inspectable<'ser>, DecodeError>
Parses a Decoder provided Object into an Inspectable.