Struct lyon::svg::parser::Stream [] [src]

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

Streaming interface for &[u8] data.

Methods

impl<'a> Stream<'a>

Constructs a new Stream from data.

Constructs a new Stream from exists.

Used to properly detect a current position on the error.

Returns current position.

Sets current position.

Returns number of chars left.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"text");
s.advance_raw(4);
assert_eq!(s.at_end(), true);
assert_eq!(s.left(), 0);

Returns true if we are at the end of the stream.

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

Accessing stream after reaching end via safe methods will produce svgparser::Error.

Accessing stream after reaching end via unsafe/_raw methods will produce rust bound checking error.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"text");
s.advance_raw(2);
assert_eq!(s.curr_char_raw(), b'x');
assert_eq!(s.at_end(), false);
s.advance_raw(2);
assert_eq!(s.at_end(), true);

Returns a char from current stream position.

Errors

Returns Error::UnexpectedEndOfStream if we at the end of the stream.

Unsafe version of curr_char().

Compares selected char with char from current stream position.

Errors

Returns Error::UnexpectedEndOfStream if we at the end of the stream.

Unsafe version of is_char_eq().

Returns char at the position relative to current.

Errors

Returns Error::AdvanceError if we are out of the stream bounds.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"text");
s.advance_raw(2);
assert_eq!(s.char_at(-2).unwrap(), b't');
assert_eq!(s.char_at(-1).unwrap(), b'e');
assert_eq!(s.char_at(0).unwrap(),  b'x');
assert_eq!(s.char_at(1).unwrap(),  b't');

Moves back by n chars.

Advance by n chars.

Errors

Returns Error::AdvanceError if new position beyond stream end.

Examples

use svgparser::{Stream, Error, ErrorPos};

let mut s = Stream::new(b"text");
s.advance(2).unwrap(); // ok
assert_eq!(s.pos(), 2);
s.advance(2).unwrap(); // also ok, we at end now
assert_eq!(s.pos(), 4);
// fail
assert_eq!(s.advance(2).err().unwrap(), Error::AdvanceError{
    expected: 6,
    total: 4,
    pos: ErrorPos::new(1, 5),
});

Unsafe version of advance().

Examples

use svgparser::Stream;

let mut s = Stream::new(b"text");
s.advance_raw(2); // ok
s.advance_raw(20); // will cause panic via debug_assert!().

Checks that char at the current position is (white)space.

Accepted chars: ' ', '\n', '\r', '\t'.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"t e x t");
assert_eq!(s.is_space().unwrap(), false);
s.advance_raw(1);
assert_eq!(s.is_space().unwrap(), true);

Unsafe version of is_space().

Skips (white)space's.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"Some \t\n\rtext");
s.advance_raw(4);
s.skip_spaces();
assert_eq!(s.slice_tail(), b"text");

Calculates length to the selected char.

Errors

Returns Error::UnexpectedEndOfStream if no such char.

Examples

use svgparser::Stream;

let s = Stream::new(b"Some long text.");
assert_eq!(s.len_to(b'l').unwrap(), 5);

Calculates length to the selected char.

If char not found - returns length to the end of the stream.

Examples

use svgparser::Stream;

let s = Stream::new(b"Some long text.");
assert_eq!(s.len_to_char_or_end(b'l'), 5);
assert_eq!(s.len_to_char_or_end(b'q'), 15);

Calculates length to the 'space' char.

Checked according to is_space() method.

Examples

use svgparser::Stream;

let s = Stream::new(b"Some\ntext.");
assert_eq!(s.len_to_space_or_end(), 4);

Jumps to the selected char.

Errors

Returns Error::UnexpectedEndOfStream if no such char.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"Some text.");
s.jump_to(b't').unwrap();
assert_eq!(s.pos(), 5);

Jumps to the selected char or the end of the stream.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"Some text.");
s.jump_to_char_or_end(b'q');
assert_eq!(s.at_end(), true);

Jumps to the end of the stream.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"Some text.");
s.jump_to_end();
assert_eq!(s.at_end(), true);

Returns reference to data with length len and advance stream to the same length.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"Some text.");
assert_eq!(s.read_raw(4), b"Some");
assert_eq!(s.pos(), 4);

Returns reference to data until selected char and advance stream by the data length.

Shorthand for: len_to() + read_raw().

Errors

Returns Error::UnexpectedEndOfStream if no such char.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"Some text.");
assert_eq!(s.read_to(b'm').unwrap(), b"So");
assert_eq!(s.pos(), 2);

Returns next data of stream with selected length.

Examples

use svgparser::Stream;

let s = Stream::new(b"Text");
assert_eq!(s.slice_next_raw(3), b"Tex");

Returns data of stream within selected region.

Examples

use svgparser::Stream;

let s = Stream::new(b"Text");
assert_eq!(s.slice_region_raw(1, 3), b"ex");

Returns complete data of stream.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"Text");
s.advance(2).unwrap();
assert_eq!(s.slice(), b"Text");

Returns tail data of stream.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"Some text.");
s.advance(5).unwrap();
assert_eq!(s.slice_tail(), b"text.");

Returns true if stream data at current position starts with selected text.

Examples

use svgparser::Stream;

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

Consumes selected char.

Errors

If current char is not equal to selected - we will get Error::InvalidChar.

Examples

use svgparser::Stream;

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

Parses number from the stream.

We use own parser instead of Rust parser because we didn't know number length, to pass it to Rust parser and it will not return a number of consumed chars, which we need. And to detect length we need to actually parse the number, so getting only a length will be pointless.

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

Errors

Can return most of the Error errors.

Examples

use svgparser::Stream;

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

Parses number from the list of numbers.

Examples

use svgparser::Stream;

let mut s = Stream::new(b"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);

Parses integer number from the stream.

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

Parses integer from the list of numbers.

Parses length from the stream.

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

Examples

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

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

Notes

  • Suffix must be lowercase, otherwise it will be skipped.

Parses length from the list of lengths.

Calculates a current absolute position.

Generates a new UnexpectedEndOfStream error from the current position.

Trait Implementations

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

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

impl<'a> Debug for Stream<'a>

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