[][src]Struct yaml_split::DocumentIterator

pub struct DocumentIterator<R> where
    R: Read
{ /* fields omitted */ }

DocumentIterator is an iterator over individual YAML documents in a file or stream.

For example, the following YAML file contains two separate documents:

hello: world
---
hello: rust

The first item in this iterator will be:

hello: world

The second item will be (the "header" / directives end marker "---" is considered part of the document):

---
hello: rust

Each item's output will be suitable for passing to serde-yaml, yaml-rust or similar libraries for parsing. Each item can also be an error, letting you opt for safe handling of errors when dealing with lots of files.

use std::fs::File;
use yaml_split::DocumentIterator;

let read_file = File::open("test.yaml").unwrap();
let doc_iter = DocumentIterator::new(read_file);

for doc in doc_iter {
    println!("{}", doc.unwrap());
}

This also correctly handles less common areas of the YAML spec including directives, comments and document end markers.

use yaml_split::DocumentIterator;

let input = r#"

# a header comment
%YAML 1.2
---
hello: world
...
---
hello: rust
---
# a body comment
hello: everyone
"#;

let mut doc_iter = DocumentIterator::new(input.as_bytes());

assert_eq!(r#"

# a header comment
%YAML 1.2
---
hello: world
"#, doc_iter.next().unwrap().unwrap());
assert_eq!(r#"---
hello: rust
"#, doc_iter.next().unwrap().unwrap());
assert_eq!(r#"---
# a body comment
hello: everyone
"#, doc_iter.next().unwrap().unwrap());

Implementations

impl<'a, R: Read + 'a> DocumentIterator<R>[src]

pub fn new(reader: R) -> DocumentIterator<R>

Notable traits for DocumentIterator<R>

impl<R: Read> Iterator for DocumentIterator<R> type Item = Result<String, YamlSplitError>;
[src]

new() creates a new DocumentIterator over a given reader's contents.

This reader can be a reader for a file:

use std::fs::File;
use yaml_split::DocumentIterator;

let read_file = File::open("test.yml").unwrap();
let doc_iter = DocumentIterator::new(read_file);

for doc in doc_iter {
    println!("{}", doc.unwrap());
}

Or the reader can be a simple byte array (useful for strings):

use yaml_split::DocumentIterator;
let yaml = r#"
hello: world
---
hello: rust
"#;

let mut doc_iter = DocumentIterator::new(yaml.as_bytes());

assert_eq!(r#"
hello: world
"#, doc_iter.next().unwrap().unwrap());
assert_eq!(r#"---
hello: rust
"#, doc_iter.next().unwrap().unwrap());
assert_eq!(true, doc_iter.next().is_none());

// or in a loop:
for doc in doc_iter {
    println!("{}", doc.unwrap());
}

Trait Implementations

impl<R: Read> Iterator for DocumentIterator<R>[src]

type Item = Result<String, YamlSplitError>

The type of the elements being iterated over.

Auto Trait Implementations

impl<R> RefUnwindSafe for DocumentIterator<R> where
    R: RefUnwindSafe
[src]

impl<R> Send for DocumentIterator<R> where
    R: Send
[src]

impl<R> Sync for DocumentIterator<R> where
    R: Sync
[src]

impl<R> Unpin for DocumentIterator<R> where
    R: Unpin
[src]

impl<R> UnwindSafe for DocumentIterator<R> where
    R: UnwindSafe
[src]

Blanket Implementations

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

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

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

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

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

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

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.