pub struct Parser<Ref> { /* 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<Ref> Parser<Ref>
impl<Ref> Parser<Ref>
sourcepub fn from_ref(octets: Ref) -> Selfwhere
Ref: AsRef<[u8]>,
pub fn from_ref(octets: Ref) -> Selfwhere Ref: AsRef<[u8]>,
Creates a new parser atop a reference to an octet sequence.
sourcepub fn octets_ref(&self) -> Refwhere
Ref: Copy,
pub fn octets_ref(&self) -> Refwhere Ref: Copy,
Returns the wrapped reference to the underlying octets sequence.
source§impl Parser<&'static [u8]>
impl Parser<&'static [u8]>
sourcepub fn from_static(slice: &'static [u8]) -> Self
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<Ref: AsRef<[u8]>> Parser<Ref>
impl<Ref: AsRef<[u8]>> Parser<Ref>
sourcepub fn as_slice(&self) -> &[u8] ⓘ
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 peek
for that.
sourcepub fn as_slice_mut(&mut self) -> &mut [u8] ⓘwhere
Ref: AsMut<[u8]>,
pub fn as_slice_mut(&mut self) -> &mut [u8] ⓘwhere Ref: AsMut<[u8]>,
Returns a mutable octets slice of the underlying sequence.
The slice covers the entire sequence, not just the remaining data.
sourcepub fn peek(&self, len: usize) -> Result<&[u8], ParseError>
pub fn peek(&self, len: usize) -> Result<&[u8], ParseError>
Returns a slice for the next len
octets.
If less than len
octets are left, returns an error.
sourcepub fn seek(&mut self, pos: usize) -> Result<(), ParseError>
pub fn seek(&mut self, pos: usize) -> Result<(), ParseError>
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.
sourcepub fn advance(&mut self, len: usize) -> Result<(), ParseError>
pub fn advance(&mut self, len: usize) -> Result<(), ParseError>
Advances the parser‘s position by len
octets.
If this would take the parser beyond its end, an error is returned.
sourcepub fn advance_to_end(&mut self)
pub fn advance_to_end(&mut self)
Advances to the end of the parser.
source§impl<Ref: AsRef<[u8]>> Parser<Ref>
impl<Ref: AsRef<[u8]>> Parser<Ref>
sourcepub fn parse_octets(&mut self, len: usize) -> Result<Ref::Range, ParseError>where
Ref: OctetsRef,
pub fn parse_octets(&mut self, len: usize) -> Result<Ref::Range, ParseError>where Ref: OctetsRef,
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.
sourcepub fn parse_buf(&mut self, buf: &mut [u8]) -> Result<(), ParseError>
pub fn parse_buf(&mut self, buf: &mut [u8]) -> Result<(), ParseError>
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.
sourcepub fn parse_i8(&mut self) -> Result<i8, ParseError>
pub fn parse_i8(&mut self) -> Result<i8, ParseError>
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.
sourcepub fn parse_u8(&mut self) -> Result<u8, ParseError>
pub fn parse_u8(&mut self) -> Result<u8, ParseError>
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.
sourcepub fn parse_i16(&mut self) -> Result<i16, ParseError>
pub fn parse_i16(&mut self) -> Result<i16, ParseError>
Takes an i16
from the beginning of the parser.
The value is converted from network byte order 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.
sourcepub fn parse_u16(&mut self) -> Result<u16, ParseError>
pub fn parse_u16(&mut self) -> Result<u16, ParseError>
Takes a u16
from the beginning of the parser.
The value is converted from network byte order into the system’s own byte order if necessary. The parser is advanced by two ocetets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.
sourcepub fn parse_i32(&mut self) -> Result<i32, ParseError>
pub fn parse_i32(&mut self) -> Result<i32, ParseError>
Takes an i32
from the beginning of the parser.
The value is converted from network byte order 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.
sourcepub fn parse_u32(&mut self) -> Result<u32, ParseError>
pub fn parse_u32(&mut self) -> Result<u32, ParseError>
Takes a u32
from the beginning of the parser.
The value is converted from network byte order 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.
sourcepub fn parse_block<F, U>(
&mut self,
limit: usize,
op: F
) -> Result<U, ParseError>where
F: FnOnce(&mut Self) -> Result<U, ParseError>,
pub fn parse_block<F, U>( &mut self, limit: usize, op: F ) -> Result<U, ParseError>where F: FnOnce(&mut Self) -> Result<U, ParseError>,
Parses a given amount of octets through a closure.
Parses a block of limit
octets and moves the parser to the end of
that block or, if less than limit
octets are still available, to
the end of the parser.
The closure op
will be allowed to parse up to limit
octets. If it
does so successfully or returns with a form error, the method returns
its return value. If it returns with a short buffer error, the method
returns a form error. If it returns successfully with less than
limit
octets parsed, returns a form error indicating trailing data.
If the limit is larger than the remaining number of octets, returns a
ParseError::ShortInput
.