Skip to main content

from_slice_at

Function from_slice_at 

Source
pub fn from_slice_at<'de, T>(input: &'de [u8], pointer: &str) -> Result<T>
where T: Read<'de> + Default,
Expand description

Parse one value out of a BEVE document, identified by a JSON Pointer.

Everything not on the path is stepped over rather than parsed, so the cost is a walk over the headers in front of the value and then one read of the value itself. Reaching one field of a large document does not decode the rest of it, and does not allocate for the parts it passes.

The empty pointer names the whole document, and is from_slice with an extra argument. Anything else is / followed by one token per level: /servers/0/port is the port of the first element of servers. A key containing / or ~ is spelled with ~1 and ~0, and an object with integer keys takes an integer token.

let bytes = structio::to_beve(&config);

let port: u16 = structio::from_beve_at(&bytes, "/servers/1/port").unwrap();
assert_eq!(port, 443);

Unlike from_slice this does not require the document to end where the value does, the rest of it being what surrounds the value asked for. The bytes after it are therefore never looked at, so a document that is malformed past the value it names is still read from successfully. Use validate first where that matters. The one exception is an element of a packed-boolean array: the array’s last byte holds its padding, which is checked, so the whole array has to be present.

The depth limit is the document’s, not the value’s: the containers the pointer passes through are counted as reading the whole document would count them, so a value too deep to read that way is too deep to read here. What lies past the value is neither looked at nor measured.

A well-formed pointer naming something the document does not hold is NoSuchValue; a pointer that is not well formed is InvalidPointer.