octseq::parse

Struct Parser

source
pub struct Parser<'a, Octs: ?Sized> { /* private fields */ }
Expand description

A parser for sequentially extracting data from an octets sequence.

The parser wraps an Octets reference and remembers the read position on the referenced sequence. Methods allow reading out data and progressing the position beyond processed data.

Implementations§

source§

impl<'a, Octs: ?Sized> Parser<'a, Octs>

source

pub fn from_ref(octets: &'a Octs) -> Self
where Octs: AsRef<[u8]>,

Creates a new parser atop a reference to an octet sequence.

source

pub fn with_range<R>(octets: &'a Octs, range: R) -> Self
where Octs: AsRef<[u8]>, R: RangeBounds<usize>,

Creates a new parser only using a range of the given octets.

§Panics

Panics if range is decreasing or out of bounds.

source

pub fn try_with_range<R>(octets: &'a Octs, range: R) -> Option<Self>
where Octs: AsRef<[u8]>, R: RangeBounds<usize>,

Creates a new parser only using a range if possible.

If range is decreasing or out of bounds, returns an Error.

source

pub fn octets_ref(&self) -> &'a Octs

Returns the wrapped reference to the underlying octets sequence.

source

pub fn pos(&self) -> usize

Returns the current parse position as an index into the octets.

source

pub fn len(&self) -> usize

Returns the length of the underlying octet sequence.

This is not the number of octets left for parsing. Use Parser::remaining for that.

source

pub fn is_empty(&self) -> bool

Returns whether the underlying octets sequence is empty.

This does not return whether there are no more octets left to parse.

source§

impl Parser<'static, [u8]>

source

pub fn from_static(slice: &'static [u8]) -> Self

Creates a new parser atop a static byte slice.

This function is most useful for testing.

source§

impl<'a, Octs: AsRef<[u8]> + ?Sized> Parser<'a, Octs>

source

pub fn as_slice(&self) -> &[u8]

Returns an octets slice of the underlying sequence.

The slice covers the entire sequence, not just the remaining data. You can use Parser::peek for that.

source

pub fn remaining(&self) -> usize

Returns the number of remaining octets to parse.

source

pub fn peek(&self, len: usize) -> Result<&[u8], ShortInput>

Returns a slice for the next len octets.

If less than len octets are left, returns an error.

source

pub fn peek_all(&self) -> &[u8]

Returns a slice of the data left to parse.

source

pub fn seek(&mut self, pos: usize) -> Result<(), ShortInput>

Repositions the parser to the given index.

It is okay to reposition anywhere within the sequence. However, if pos is larger than the length of the sequence, an error is returned.

source

pub fn advance(&mut self, len: usize) -> Result<(), ShortInput>

Advances the parser‘s position by len octets.

If this would take the parser beyond its end, an error is returned.

source

pub fn advance_to_end(&mut self)

Advances to the end of the parser.

source

pub fn check_len(&self, len: usize) -> Result<(), ShortInput>

Checks that there are len octets left to parse.

If there aren’t, returns an error.

source§

impl<'a, Octs: AsRef<[u8]> + ?Sized> Parser<'a, Octs>

source

pub fn parse_octets( &mut self, len: usize, ) -> Result<Octs::Range<'a>, ShortInput>
where Octs: Octets,

Takes and returns the next len octets.

Advances the parser by len octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_buf(&mut self, buf: &mut [u8]) -> Result<(), ShortInput>

Fills the provided buffer by taking octets from the parser.

Copies as many octets as the buffer is long from the parser into the buffer and advances the parser by that many octets.

If there aren’t enough octets left in the parser to fill the buffer completely, returns an error and leaves the parser untouched.

source

pub fn parse_parser(&mut self, len: usize) -> Result<Self, ShortInput>

Takes as many octets as requested and returns a parser for them.

If enough octets are remaining, the method clones self, limits its length to the requested number of octets, and returns it. The returned parser will be positioned at wherever self was positioned. The self parser will be advanced by the requested amount of octets.

If there aren’t enough octets left in the parser to fill the buffer completely, returns an error and leaves the parser untouched.

source

pub fn parse_i8(&mut self) -> Result<i8, ShortInput>

Takes an i8 from the beginning of the parser.

Advances the parser by one octet. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u8(&mut self) -> Result<u8, ShortInput>

Takes a u8 from the beginning of the parser.

Advances the parser by one octet. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source§

impl<'a, Octs: AsRef<[u8]> + ?Sized> Parser<'a, Octs>

source

pub fn parse_i16_be(&mut self) -> Result<i16, ShortInput>

Takes a big-endian i16 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by two octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_i16_le(&mut self) -> Result<i16, ShortInput>

Takes a little-endian i16 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by two octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u16_be(&mut self) -> Result<u16, ShortInput>

Takes a big-endian u16 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by two octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u16_le(&mut self) -> Result<u16, ShortInput>

Takes a little-endian u16 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by two octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_i32_be(&mut self) -> Result<i32, ShortInput>

Takes a big-endian i32 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by four octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_i32_le(&mut self) -> Result<i32, ShortInput>

Takes a little-endian i32 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by four octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u32_be(&mut self) -> Result<u32, ShortInput>

Takes a big-endian u32 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by four octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u32_le(&mut self) -> Result<u32, ShortInput>

Takes a little-endian u32 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by four octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_i64_be(&mut self) -> Result<i64, ShortInput>

Takes a big-endian i64 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by eight octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_i64_le(&mut self) -> Result<i64, ShortInput>

Takes a little-endian i64 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by eight octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u64_be(&mut self) -> Result<u64, ShortInput>

Takes a big-endian u64 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by eight octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u64_le(&mut self) -> Result<u64, ShortInput>

Takes a little-endian u64 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by eight octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_i128_be(&mut self) -> Result<i128, ShortInput>

Takes a big-endian i128 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by 16 octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_i128_le(&mut self) -> Result<i128, ShortInput>

Takes a little-endian i128 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by 16 octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u128_be(&mut self) -> Result<u128, ShortInput>

Takes a big-endian u128 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by 16 octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u128_le(&mut self) -> Result<u128, ShortInput>

Takes a little-endian u128 from the beginning of the parser.

The value is converted into the system’s own byte order if necessary. The parser is advanced by 16 octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

Trait Implementations§

source§

impl<'a, Octs: ?Sized> Clone for Parser<'a, Octs>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<'a, Octs: Debug + ?Sized> Debug for Parser<'a, Octs>

source§

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

Formats the value using the given formatter. Read more
source§

impl<'a, Octs: ?Sized> Copy for Parser<'a, Octs>

Auto Trait Implementations§

§

impl<'a, Octs> Freeze for Parser<'a, Octs>
where Octs: ?Sized,

§

impl<'a, Octs> RefUnwindSafe for Parser<'a, Octs>
where Octs: RefUnwindSafe + ?Sized,

§

impl<'a, Octs> Send for Parser<'a, Octs>
where Octs: Sync + ?Sized,

§

impl<'a, Octs> Sync for Parser<'a, Octs>
where Octs: Sync + ?Sized,

§

impl<'a, Octs> Unpin for Parser<'a, Octs>
where Octs: ?Sized,

§

impl<'a, Octs> UnwindSafe for Parser<'a, Octs>
where Octs: RefUnwindSafe + ?Sized,

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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<Source, Target> OctetsInto<Target> for Source
where Target: OctetsFrom<Source>,

source§

type Error = <Target as OctetsFrom<Source>>::Error

source§

fn try_octets_into( self, ) -> Result<Target, <Source as OctetsInto<Target>>::Error>

Performs the conversion.
source§

fn octets_into(self) -> Target
where Self::Error: Into<Infallible>,

Performs an infallible conversion.
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 = 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.