Object

Enum Object 

Source
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>

Source

pub fn into_token(self) -> Token<'ser>

Source

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")));
Source

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()))
);
Source

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());
Source

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")));
Source

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()))
);
Source

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());
Source

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());
Source

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()
);
Source

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());
Source

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());
Source

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()
);
Source

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>

Source§

fn try_from( object: Object<'obj, 'ser>, ) -> Result<Inspectable<'ser>, DecodeError>

Parses a Decoder provided Object into an Inspectable.

Source§

type Error = Error

The type returned in the event of a conversion error.

Auto Trait Implementations§

§

impl<'obj, 'ser> Freeze for Object<'obj, 'ser>

§

impl<'obj, 'ser> !RefUnwindSafe for Object<'obj, 'ser>

§

impl<'obj, 'ser> Send for Object<'obj, 'ser>

§

impl<'obj, 'ser> Sync for Object<'obj, 'ser>

§

impl<'obj, 'ser> Unpin for Object<'obj, 'ser>

§

impl<'obj, 'ser> !UnwindSafe for Object<'obj, 'ser>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.