Struct maybe_xml::Reader

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

Tokenizes XML input into a Token.

It does not allocate.

Examples

Using tokenize()

use maybe_xml::{Reader, token::{Characters, EndTag, StartTag, Ty}};

let input = "<id>123</id>";

let reader = Reader::from_str(input);
let mut pos = 0;

let token = reader.tokenize(&mut pos);
if let Some(Ty::StartTag(tag)) = token.map(|t| t.ty()) {
    assert_eq!("id", tag.name().local().as_str());
    assert_eq!(None, tag.name().namespace_prefix());
} else {
    panic!();
}
assert_eq!(4, pos);

let token = reader.tokenize(&mut pos);
if let Some(Ty::Characters(chars)) = token.map(|t| t.ty()) {
    assert_eq!("123", chars.content().as_str());
} else {
    panic!();
}
assert_eq!(7, pos);

let token = reader.tokenize(&mut pos);
if let Some(Ty::EndTag(tag)) = token.map(|t| t.ty()) {
    assert_eq!("</id>", tag.as_str());
    assert_eq!("id", tag.name().local().as_str());
} else {
    panic!();
}
assert_eq!(12, pos);

let token = reader.tokenize(&mut pos);
assert_eq!(None, token);

// Verify that `pos` is equal to `input.len()` to ensure all data was
// processed.

Using Iterator functionality

use maybe_xml::{Reader, token::Ty};

let input = "<id>123</id><name>Jane Doe</name>";

let reader = Reader::from_str(input);
let mut iter = reader.into_iter().filter_map(|token| {
    match token.ty() {
        Ty::StartTag(tag) => Some(tag.name().as_str()),
        _ => None,
    }
});

let name = iter.next();
assert_eq!(Some("id"), name);

let name = iter.next();
assert_eq!(Some("name"), name);

assert_eq!(None, iter.next());

Note that if the input is malformed or incomplete such as <tag, the Iterator will return None and will not return the invalid input. If you want to verify that all of the input was processed, then you should use the Reader::tokenize() method.

Implementations§

source§

impl<'a> Reader<'a>

source

pub const fn from_str(input: &'a str) -> Self

Creates a new instance with the given UTF-8 string input.

source

pub const fn new(input: &'a str) -> Self

Creates a new instance with the given UTF-8 string input.

source

pub fn tokenize(&self, pos: &mut usize) -> Option<Token<'a>>

Tokenizes the input starting at the given position.

If a token is found, the position argument is also updated to the byte index after the token.

Panics

Panics if the pos is greater than the input length or if pos is not at a character boundary.

Examples
use maybe_xml::{Reader, token::{StartTag, Ty}};

let input = "<id>123</id>";

let reader = Reader::from_str(input);
let mut pos = 0;

let token = reader.tokenize(&mut pos);
if let Some(Ty::StartTag(tag)) = token.map(|t| t.ty()) {
    assert_eq!("id", tag.name().local().as_str());
    assert_eq!(None, tag.name().namespace_prefix());
} else {
    panic!();
}

// Position was assigned to the index after the end of the token
assert_eq!(4, pos);

If tokenize() returns None, but the position is not equal to the input’s byte length, then there is unprocessed input such as malformed XML. For instance, if the input was <tag without the enclosing >, then tokenize() will return None.

use maybe_xml::{Reader, token::{StartTag, Ty}};

let input = "<tag";

let reader = Reader::from_str(input);
let mut pos = 0;

let token = reader.tokenize(&mut pos);
assert_eq!(None, token);

assert_eq!(0, pos);
assert_ne!(input.len(), pos);
source

pub const fn parse(&self, pos: usize) -> Option<Token<'a>>

Constant function which tokenizes the input starting at the given position.

Important

The pos is not updated and should be updated with the Token::len().

Panics

Panics if the pos is greater than the input length or if pos is not at a character boundary.

Examples
use maybe_xml::{Reader, token::{StartTag, Ty}};

let input = "<id>123</id>";

let reader = Reader::from_str(input);
let mut pos = 0;

let token = reader.parse(pos);
if let Some(Ty::StartTag(tag)) = token.map(|t| t.ty()) {
    assert_eq!("id", tag.name().local().as_str());
    assert_eq!(None, tag.name().namespace_prefix());
} else {
    panic!();
}

pos += token.map(|t| t.len()).unwrap_or_default();
assert_eq!(4, pos);

If parse() returns None, but the position is not equal to the input’s byte length, then there is unprocessed input such as malformed XML. For instance, if the input was <tag without the enclosing >, then tokenize() will return None.

use maybe_xml::{Reader, token::{StartTag, Ty}};

let input = "<tag";

let reader = Reader::from_str(input);
let mut pos = 0;

let token = reader.parse(pos);
assert_eq!(None, token);

assert_eq!(0, pos);
assert_ne!(input.len(), pos);
source

pub const fn iter(&self, pos: usize) -> Iter<'a>

Returns an iterator for tokens starting at the given position.

Panics

The iterator will panic if the initial pos is greater than the input length or if pos is not at a character boundary.

Examples
Using other Iterator functionality
use maybe_xml::{Reader, token::Ty};

let input = "<id>123</id><name>Jane Doe</name>";

let reader = Reader::from_str(input);
let mut iter = reader.iter(0).filter_map(|token| {
    match token.ty() {
        Ty::StartTag(tag) => Some(tag.name().as_str()),
        _ => None,
    }
});

let name = iter.next();
assert_eq!(Some("id"), name);

let name = iter.next();
assert_eq!(Some("name"), name);

assert_eq!(None, iter.next());
source

pub const fn into_inner(self) -> &'a str

Return the underlying bytes being tokenized.

Trait Implementations§

source§

impl<'a> Clone for Reader<'a>

source§

fn clone(&self) -> Reader<'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 Reader<'a>

source§

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

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

impl<'a> Hash for Reader<'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> IntoIterator for Reader<'a>

§

type Item = Token<'a>

The type of the elements being iterated over.
§

type IntoIter = IntoIter<'a>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a> PartialEq for Reader<'a>

source§

fn eq(&self, other: &Reader<'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 Reader<'a>

source§

impl<'a> Eq for Reader<'a>

source§

impl<'a> StructuralEq for Reader<'a>

source§

impl<'a> StructuralPartialEq for Reader<'a>

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnwindSafe for Reader<'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.