Skip to main content

Cursor

Struct Cursor 

Source
pub struct Cursor<'a> { /* private fields */ }
Expand description

A strict JSON pull cursor over text.

Make one from text with new, or from bytes with from_bytes, which checks their UTF-8 once, in one pass. Every key, string and number is then a slice of the text, with no further check.

Read the next value with value, or with the call for its kind: string, number, bool, null, an object’s keys with first_key and next_key, an array’s items with first_item and next_item, or any value with skip. peek says which kind is next. finish checks that nothing follows.

Every byte read is checked against RFC 8259, skipped values included: a call returns an Error at the first byte that is not JSON. After an error, the cursor’s position is unspecified, so start again from the input.

Implementations§

Source§

impl<'a> Cursor<'a>

Source

pub fn new(text: &'a str) -> Self

A cursor at the start of text.

Source

pub fn from_bytes(bytes: &'a [u8]) -> Result<Self, Error>

A cursor at the start of bytes, once they are checked as UTF-8, in one pass. Err with ErrorKind::Utf8 at the first byte that is not.

Source

pub fn with_depth_limit(self, limit: usize) -> Self

How deep skip goes into arrays and objects before it returns ErrorKind::TooDeep. The skip keeps its own stack, so no limit can overflow the call stack. DEFAULT_DEPTH_LIMIT when not set.

Source

pub fn position(&self) -> usize

The offset of the next byte to read.

Source

pub fn peek(&mut self) -> Result<Kind, Error>

The kind of the next value, after any whitespace. Nothing is consumed.

Source

pub fn value<'s>(&mut self, buf: &'s mut String) -> Result<Value<'s>, Error>
where 'a: 's,

The next value, after any whitespace, in one look at its first byte: a string, a number, a boolean or null, read; or the start of an array or an object, not consumed, to be read with first_item or first_key. A string is borrowed or decoded as in string.

Source

pub fn first_key<'s>( &mut self, buf: &'s mut String, ) -> Result<Option<&'s str>, Error>
where 'a: 's,

Enters the object at the cursor and reads its first key and colon, or None when the object is empty. Read the key’s value next. The key is borrowed from the input when it has no escapes, else decoded into buf, which it then borrows.

Source

pub fn next_key<'s>( &mut self, buf: &'s mut String, ) -> Result<Option<&'s str>, Error>
where 'a: 's,

After a value in an object: the next key and colon, or None at the object’s end, which is consumed. The key is borrowed as in first_key.

Source

pub fn first_item(&mut self) -> Result<bool, Error>

Enters the array at the cursor: true when it has a first item, which is read next, and false when it is empty.

Source

pub fn next_item(&mut self) -> Result<bool, Error>

After an item in an array: true when another follows, which is read next, and false at the array’s end, which is consumed.

Source

pub fn string<'s>(&mut self, buf: &'s mut String) -> Result<&'s str, Error>
where 'a: 's,

The string at the cursor: borrowed from the input when it has no escapes, else decoded into buf, which it then borrows.

Source

pub fn number(&mut self) -> Result<Number<'a>, Error>

The number at the cursor: its exact text, and its value as an i64 or an f64. Its grammar is checked; its size is not limited, since the text keeps every digit.

Source

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

The true or false at the cursor.

Source

pub fn null(&mut self) -> Result<(), Error>

The null at the cursor.

Source

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

Reads past the value at the cursor, of any kind, checking every byte of it: its grammar, its strings’ escapes, and its depth. Numbers are checked but not read.

Source

pub fn finish(&mut self) -> Result<(), Error>

Checks that only whitespace follows.

Trait Implementations§

Source§

impl<'a> Clone for Cursor<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Cursor<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for Cursor<'a>

§

impl<'a> Freeze for Cursor<'a>

§

impl<'a> RefUnwindSafe for Cursor<'a>

§

impl<'a> Send for Cursor<'a>

§

impl<'a> Sync for Cursor<'a>

§

impl<'a> Unpin for Cursor<'a>

§

impl<'a> UnsafeUnpin for Cursor<'a>

§

impl<'a> UnwindSafe for Cursor<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.