Struct Parser

Source
pub struct Parser<R>(/* private fields */);
Expand description

A struct that parses binary data from a bytearray

You can deserialize data using

Or you can deserialize data directly using

Implementations§

Source§

impl<R> Parser<R>
where R: Read,

Source

pub fn reader(self) -> R

Turn the parser into the underlying reader

Source

pub fn get_reader(&self) -> &R

Get the the underlying reader as a reference

Source

pub fn get_reader_mut(&mut self) -> &mut R

Get the the underlying reader as a mutable reference

Source

pub fn next<T>(&mut self) -> Result<T>

Parse the next item in the parser.

§Example
use mbon::parser::Parser;

let mut parser = Parser::from(b"i\x00\x00\x00\x42");
let i: u32 = parser.next().unwrap();

assert_eq!(i, 0x42);
Source

pub fn next_obj<T>(&mut self) -> Result<T>
where T: ObjectParse, <T as ObjectParse>::Error: Error + 'static,

Parse the next custom object in the parser.

This allows you to be able to parse custom binary data. A common usecase is to store a struct in a more compact form. You could also use object values to store a different format altogether.

Note: the next value in the parser must be an Object

§Example
use mbon::error::Error;
use mbon::parser::Parser;
use mbon::object::ObjectParse;

struct Foo {
    a: i32,
    b: String,
    c: f32,
}

impl ObjectParse for Foo {
    type Error = Error;

    fn parse_object(data: &[u8]) -> Result<Self, Self::Error> {
        let mut parser = Parser::from(data);
        let a = parser.next()?;
        let b = parser.next()?;
        let c = parser.next()?;
        Ok(Self { a, b, c })
    }
}

let mut parser =
Parser::from(
    b"o\x00\x00\x00\x14i\x00\x00\x00\x42s\x00\x00\x00\x05Hellof\x00\x00\x00\x00"
);

let foo: Foo = parser.next_obj().unwrap();
assert_eq!(foo.a, 0x42);
assert_eq!(foo.b, "Hello");
assert_eq!(foo.c, 0.0);
Source

pub fn skip_next(&mut self) -> Result<()>

Skip the next value in the parser.

This will ignore the next value without parsing more than what’s necessary.

If the reader supports seeking, then it is preffered to use seek_next() instead.

§Example
use mbon::parser::Parser;

let mut parser = Parser::from(
    b"s\x00\x00\x00\x1eI don't care about this stringi\x00\x00\x00\x42"
);

parser.skip_next().unwrap();

let v: i32 = parser.next().unwrap();
assert_eq!(v, 0x42);
Source

pub fn next_value(&mut self) -> Result<Value>

Parse the next value in the parser.

This will try to read whatever value is next and return it.

§Example
use mbon::parser::Parser;
use mbon::data::Value;

let mut parser = Parser::from(b"i\x00\x00\x00\x42");

assert_eq!(parser.next_value().unwrap(), Value::Int(0x42));
Source§

impl<R> Parser<R>
where R: Read + Seek,

Source

pub fn seek_next(&mut self) -> Result<()>

Seek to the next value in the parser.

This will efficiently skip the next value without reading more than what’s necessary.

§Example
use mbon::parser::Parser;
use std::io::Cursor;

let reader = Cursor::new(
    b"s\x00\x00\x00\x23This is a string I don't care abouti\x00\x00\x00\x20"
);

let mut parser = Parser::from(reader);

parser.seek_next().unwrap();
let val: u32 = parser.next().unwrap();

assert_eq!(val, 32);

Trait Implementations§

Source§

impl<R> AsMut<R> for Parser<R>

Source§

fn as_mut(&mut self) -> &mut R

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<R> AsRef<R> for Parser<R>

Source§

fn as_ref(&self) -> &R

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a, T> From<&'a T> for Parser<&'a [u8]>
where T: AsRef<[u8]>,

Source§

fn from(slice: &'a T) -> Self

Converts to this type from the input type.
Source§

impl<R> From<R> for Parser<R>
where R: Read,

Source§

fn from(reader: R) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<R> Freeze for Parser<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Parser<R>
where R: RefUnwindSafe,

§

impl<R> Send for Parser<R>
where R: Send,

§

impl<R> Sync for Parser<R>
where R: Sync,

§

impl<R> Unpin for Parser<R>
where R: Unpin,

§

impl<R> UnwindSafe for Parser<R>
where R: UnwindSafe,

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.