Struct xmlparser::Stream

source ·
pub struct Stream<'a> { /* private fields */ }
Expand description

A streaming XML parsing interface.

Implementations§

source§

impl<'a> Stream<'a>

source

pub fn from_substr(text: &'a str, fragment: Range<usize>) -> Self

Creates a new stream from a specified text substring.

source

pub fn span(&self) -> StrSpan<'a>

Returns an underling string span.

source

pub fn pos(&self) -> usize

Returns current position.

source

pub fn jump_to_end(&mut self)

Sets current position equal to the end.

Used to indicate end of parsing on error.

source

pub fn at_end(&self) -> bool

Checks if the stream is reached the end.

Any pos() value larger than original text length indicates stream end.

Accessing stream after reaching end via safe methods will produce an UnexpectedEndOfStream error.

Accessing stream after reaching end via *_unchecked methods will produce a Rust’s bound checking error.

source

pub fn curr_byte(&self) -> Result<u8, StreamError>

Returns a byte from a current stream position.

§Errors
  • UnexpectedEndOfStream
source

pub fn curr_byte_unchecked(&self) -> u8

Returns a byte from a current stream position.

§Panics
  • if the current position is after the end of the data
source

pub fn next_byte(&self) -> Result<u8, StreamError>

Returns a next byte from a current stream position.

§Errors
  • UnexpectedEndOfStream
source

pub fn advance(&mut self, n: usize)

Advances by n bytes.

§Examples
use xmlparser::Stream;

let mut s = Stream::from("text");
s.advance(2); // ok
s.advance(20); // will cause a panic via debug_assert!().
source

pub fn starts_with(&self, text: &[u8]) -> bool

Checks that the stream starts with a selected text.

We are using &[u8] instead of &str for performance reasons.

§Examples
use xmlparser::Stream;

let mut s = Stream::from("Some text.");
s.advance(5);
assert_eq!(s.starts_with(b"text"), true);
assert_eq!(s.starts_with(b"long"), false);
source

pub fn consume_byte(&mut self, c: u8) -> Result<(), StreamError>

Consumes the current byte if it’s equal to the provided byte.

§Errors
  • InvalidChar
  • UnexpectedEndOfStream
§Examples
use xmlparser::Stream;

let mut s = Stream::from("Some text.");
assert!(s.consume_byte(b'S').is_ok());
assert!(s.consume_byte(b'o').is_ok());
assert!(s.consume_byte(b'm').is_ok());
assert!(s.consume_byte(b'q').is_err());
source

pub fn try_consume_byte(&mut self, c: u8) -> bool

Tries to consume the current byte if it’s equal to the provided byte.

Unlike consume_byte() will not return any errors.

source

pub fn skip_string(&mut self, text: &'static [u8]) -> Result<(), StreamError>

Skips selected string.

§Errors
  • InvalidString
source

pub fn consume_bytes<F>(&mut self, f: F) -> StrSpan<'a>
where F: Fn(&Stream<'_>, u8) -> bool,

Consumes bytes by the predicate and returns them.

The result can be empty.

source

pub fn skip_bytes<F>(&mut self, f: F)
where F: Fn(&Stream<'_>, u8) -> bool,

Skips bytes by the predicate.

source

pub fn consume_chars<F>(&mut self, f: F) -> Result<StrSpan<'a>, StreamError>
where F: Fn(&Stream<'_>, char) -> bool,

Consumes chars by the predicate and returns them.

The result can be empty.

source

pub fn skip_chars<F>(&mut self, f: F) -> Result<(), StreamError>
where F: Fn(&Stream<'_>, char) -> bool,

Skips chars by the predicate.

source

pub fn slice_back(&self, pos: usize) -> StrSpan<'a>

Slices data from pos to the current position.

source

pub fn slice_tail(&self) -> StrSpan<'a>

Slices data from the current position to the end.

source

pub fn skip_spaces(&mut self)

Skips whitespaces.

Accepted values: ' ' \n \r \t.

source

pub fn starts_with_space(&self) -> bool

Checks if the stream is starts with a space.

source

pub fn consume_spaces(&mut self) -> Result<(), StreamError>

Consumes whitespaces.

Like skip_spaces(), but checks that first char is actually a space.

§Errors
  • InvalidSpace
source

pub fn try_consume_reference(&mut self) -> Option<Reference<'a>>

Consumes an XML character reference if there is one.

On error will reset the position to the original.

source

pub fn consume_reference(&mut self) -> Result<Reference<'a>, StreamError>

Consumes an XML reference.

Consumes according to: https://www.w3.org/TR/xml/#NT-Reference

§Errors
  • InvalidReference
source

pub fn consume_name(&mut self) -> Result<StrSpan<'a>, StreamError>

Consumes an XML name and returns it.

Consumes according to: https://www.w3.org/TR/xml/#NT-Name

§Errors
  • InvalidName - if name is empty or starts with an invalid char
  • UnexpectedEndOfStream
source

pub fn skip_name(&mut self) -> Result<(), StreamError>

Skips an XML name.

The same as consume_name(), but does not return a consumed name.

§Errors
  • InvalidName - if name is empty or starts with an invalid char
source

pub fn consume_qname( &mut self, ) -> Result<(StrSpan<'a>, StrSpan<'a>), StreamError>

Consumes a qualified XML name and returns it.

Consumes according to: https://www.w3.org/TR/xml-names/#ns-qualnames

§Errors
  • InvalidName - if name is empty or starts with an invalid char
source

pub fn consume_eq(&mut self) -> Result<(), StreamError>

Consumes =.

Consumes according to: https://www.w3.org/TR/xml/#NT-Eq

§Errors
  • InvalidChar
  • UnexpectedEndOfStream
source

pub fn consume_quote(&mut self) -> Result<u8, StreamError>

Consumes quote.

Consumes ' or " and returns it.

§Errors
  • InvalidQuote
  • UnexpectedEndOfStream
source

pub fn gen_text_pos(&self) -> TextPos

Calculates a current absolute position.

This operation is very expensive. Use only for errors.

source

pub fn gen_text_pos_from(&self, pos: usize) -> TextPos

Calculates an absolute position at pos.

This operation is very expensive. Use only for errors.

§Examples
let s = xmlparser::Stream::from("text");

assert_eq!(s.gen_text_pos_from(2), xmlparser::TextPos::new(1, 3));
assert_eq!(s.gen_text_pos_from(9999), xmlparser::TextPos::new(1, 5));

Trait Implementations§

source§

impl<'a> Clone for Stream<'a>

source§

fn clone(&self) -> Stream<'a>

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> Debug for Stream<'a>

source§

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

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

impl<'a> From<&'a str> for Stream<'a>

source§

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

Converts to this type from the input type.
source§

impl<'a> From<StrSpan<'a>> for Stream<'a>

source§

fn from(span: StrSpan<'a>) -> Self

Converts to this type from the input type.
source§

impl<'a> Hash for Stream<'a>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a> PartialEq for Stream<'a>

source§

fn eq(&self, other: &Stream<'a>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> Copy for Stream<'a>

source§

impl<'a> Eq for Stream<'a>

source§

impl<'a> StructuralPartialEq for Stream<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Stream<'a>

§

impl<'a> RefUnwindSafe for Stream<'a>

§

impl<'a> Send for Stream<'a>

§

impl<'a> Sync for Stream<'a>

§

impl<'a> Unpin for Stream<'a>

§

impl<'a> UnwindSafe for Stream<'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> 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,

§

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>,

§

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>,

§

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.