[][src]Struct svgtypes::Stream

pub struct Stream<'a> { /* fields omitted */ }

A streaming text parsing interface.

Methods

impl<'a> Stream<'a>[src]

pub fn pos(&self) -> usize[src]

Returns the current position in bytes.

pub fn calc_char_pos(&self) -> usize[src]

Calculates the current position in chars.

pub fn calc_char_pos_at(&self, byte_pos: usize) -> usize[src]

Calculates the current position in chars.

pub fn jump_to_end(&mut self)[src]

Sets current position equal to the end.

Used to indicate end of parsing on error.

pub fn at_end(&self) -> bool[src]

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.

pub fn curr_byte(&self) -> Result<u8, Error>[src]

Returns a byte from a current stream position.

Errors

  • UnexpectedEndOfStream

pub fn curr_byte_unchecked(&self) -> u8[src]

Returns a byte from a current stream position.

Panics

  • if the current position is after the end of the data

pub fn is_curr_byte_eq(&self, c: u8) -> bool[src]

Checks that current byte is equal to provided.

Returns false if no bytes left.

pub fn get_curr_byte(&self) -> Option<u8>[src]

Returns a byte from a current stream position if there is one.

pub fn next_byte(&self) -> Result<u8, Error>[src]

Returns a next byte from a current stream position.

Errors

  • UnexpectedEndOfStream

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

Advances by n bytes.

Examples

use svgtypes::Stream;

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

pub fn skip_spaces(&mut self)[src]

Skips whitespaces.

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

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

Checks that the stream starts with a selected text.

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

Examples

use svgtypes::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);

pub fn starts_with_space(&self) -> bool[src]

Checks if the stream is starts with a space.

Uses skip_spaces() internally.

pub fn consume_byte(&mut self, c: u8) -> Result<(), Error>[src]

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

Errors

  • InvalidChar
  • UnexpectedEndOfStream

Examples

use svgtypes::Stream;

let mut s = Stream::from("Some text.");
s.consume_byte(b'S').unwrap();
s.consume_byte(b'o').unwrap();
s.consume_byte(b'm').unwrap();
// s.consume_byte(b'q').unwrap(); // will produce an error

pub fn skip_string(&mut self, text: &[u8]) -> Result<(), Error>[src]

Consumes selected string.

Errors

  • InvalidChar
  • UnexpectedEndOfStream

pub fn consume_bytes<F>(&mut self, f: F) -> &'a str where
    F: Fn(&Stream, u8) -> bool
[src]

Consumes bytes by the predicate and returns them.

The result can be empty.

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

Consumes bytes by the predicate.

pub fn consume_ident(&mut self) -> &'a str[src]

Consumes bytes by the predicate and returns them.

pub fn slice_back(&self, pos: usize) -> &'a str[src]

Slices data from pos to the current position.

pub fn slice_tail(&self) -> &'a str[src]

Slices data from the current position to the end.

pub fn parse_number(&mut self) -> Result<f64, Error>[src]

Parses number from the stream.

This method will detect a number length and then will pass a substring to the f64::from_str method.

https://www.w3.org/TR/SVG11/types.html#DataTypeNumber

Errors

Returns only InvalidNumber.

Examples

use svgtypes::Stream;

let mut s = Stream::from("3.14");
assert_eq!(s.parse_number().unwrap(), 3.14);
assert_eq!(s.at_end(), true);

pub fn parse_list_number(&mut self) -> Result<f64, Error>[src]

Parses number from the list of numbers.

Examples

use svgtypes::Stream;

let mut s = Stream::from("3.14, 12,5 , 20-4");
assert_eq!(s.parse_list_number().unwrap(), 3.14);
assert_eq!(s.parse_list_number().unwrap(), 12.0);
assert_eq!(s.parse_list_number().unwrap(), 5.0);
assert_eq!(s.parse_list_number().unwrap(), 20.0);
assert_eq!(s.parse_list_number().unwrap(), -4.0);

pub fn parse_integer(&mut self) -> Result<i32, Error>[src]

Parses integer number from the stream.

Same as parse_number(), but only for integer. Does not refer to any SVG type.

pub fn parse_list_integer(&mut self) -> Result<i32, Error>[src]

Parses integer from the list of numbers.

pub fn parse_length(&mut self) -> Result<Length, Error>[src]

Parses length from the stream.

https://www.w3.org/TR/SVG11/types.html#DataTypeLength

Examples

use svgtypes::{Stream, Length, LengthUnit};

let mut s = Stream::from("30%");
assert_eq!(s.parse_length().unwrap(), Length::new(30.0, LengthUnit::Percent));

Notes

  • Suffix must be lowercase, otherwise it will be an error.

pub fn parse_list_length(&mut self) -> Result<Length, Error>[src]

Parses length from the list of lengths.

pub fn parse_angle(&mut self) -> Result<Angle, Error>[src]

Parses angle from the stream.

https://www.w3.org/TR/SVG11/types.html#DataTypeAngle

Notes

  • Suffix must be lowercase, otherwise it will be an error.

pub fn skip_digits(&mut self)[src]

Skips digits.

pub fn parse_iri(&mut self) -> Result<&'a str, Error>[src]

Parses a IRI.

By the SVG spec, the ID must contain only Name characters, but since no one fallows this it will parse any characters.

pub fn parse_func_iri(&mut self) -> Result<&'a str, Error>[src]

Parses a FuncIRI.

By the SVG spec, the ID must contain only Name characters, but since no one fallows this it will parse any characters.

Trait Implementations

impl<'a> Clone for Stream<'a>[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<'a> From<&'a str> for Stream<'a>[src]

impl<'a> PartialEq<Stream<'a>> for Stream<'a>[src]

impl<'a> Copy for Stream<'a>[src]

impl<'a> Debug for Stream<'a>[src]

Auto Trait Implementations

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

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

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

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

impl<'a> UnwindSafe for Stream<'a>

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]