Skip to main content

ArrayReader

Struct ArrayReader 

Source
pub struct ArrayReader<'r, 'de> { /* private fields */ }
Expand description

Reads the items of an array one by one; from Reader::read_array.

Errors in an item get its index in their path.

Implementations§

Source§

impl<'de> ArrayReader<'_, 'de>

Source

pub fn next_item<T: FromJson<'de>>(&mut self) -> Result<Option<T>, DecodeError>

Reads the next item as a T, or None after the last one, when the array’s ] has been read.

§Errors

The DecodeError of T, with the item’s index in its path.

§Examples
let mut r = jsonx_lib::Reader::new(r#"[1, "x"]"#);
let mut items = r.read_array()?;
assert_eq!(items.next_item::<u8>()?, Some(1));
assert_eq!(items.next_item::<u8>().unwrap_err().pointer(), "/1");
Source

pub fn next_with<T>( &mut self, read: impl FnOnce(&mut Reader<'de>) -> Result<T, DecodeError>, ) -> Result<Option<T>, DecodeError>

Reads the next item with read, or returns None after the last.

§Errors

Whatever read returns, with the item’s index in its path.

§Examples
let mut r = jsonx_lib::Reader::new("[[1, 2], [3]]");
let mut rows = r.read_array()?;
let mut lengths = Vec::new();
while let Some(n) = rows.next_with(|r| Ok(r.decode::<Vec<u8>>()?.len()))? {
    lengths.push(n);
}
assert_eq!(lengths, [2, 1]);
Source

pub fn skip(&mut self) -> Result<bool, DecodeError>

Skips the next item, validating it; false after the last.

§Errors

Any error of the reader inside the item.

§Examples
let mut r = jsonx_lib::Reader::new("[{}, []]");
let mut items = r.read_array()?;
assert!(items.skip()? && items.skip()?);
assert!(!items.skip()?);
Source

pub fn end(self) -> Result<(), DecodeError>

Checks that no item is left: for a fixed-length array or a tuple, after its items were read.

§Errors

DecodeErrorKind::WrongLength at the [ if items remain (all of them are skipped and counted), or an error of the reader.

§Examples
use jsonx_lib::DecodeErrorKind;

let mut r = jsonx_lib::Reader::new("[1, 2, 3]");
let mut items = r.read_array()?;
items.next_item::<u8>()?;
let err = items.end().unwrap_err();
assert_eq!(err.kind(), &DecodeErrorKind::WrongLength { expected: 1, found: 3 });
Source

pub fn index(&self) -> usize

How many items have been read or skipped.

Source

pub fn offset(&self) -> usize

The byte offset of the array’s [.

Auto Trait Implementations§

§

impl<'r, 'de> !UnwindSafe for ArrayReader<'r, 'de>

§

impl<'r, 'de> Forget for ArrayReader<'r, 'de>

§

impl<'r, 'de> Freeze for ArrayReader<'r, 'de>

§

impl<'r, 'de> Move for ArrayReader<'r, 'de>

§

impl<'r, 'de> RefUnwindSafe for ArrayReader<'r, 'de>

§

impl<'r, 'de> Send for ArrayReader<'r, 'de>

§

impl<'r, 'de> Sync for ArrayReader<'r, 'de>

§

impl<'r, 'de> Unpin for ArrayReader<'r, 'de>

§

impl<'r, 'de> UnsafeUnpin for ArrayReader<'r, 'de>

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.