<commit_before>//! Contains high-level interface for a pull-based XML parser.
//!
//! The most important type in this module is `EventReader`, which provides an iterator
//! view for events in XML document.

use std::io::{self, Read};
use std::borrow::Cow;
use std::result;
use std::str;

use common::{Position, TextPosition};

pub use self::config::ParserConfig;
pub use self::events::XmlEvent;

use self::parser::PullParser;

mod lexer;
mod parser;
mod config;
mod events;

mod error_impl;

#[derive(Debug)]
pub enum ErrorKind {
    Syntax( Cow<'static, str> ),
    Io( io::Error ),
    Utf8( str::Utf8Error ),
    UnexpectedEof,
}

/// An XML parsing error.
///
/// Consists of a 2D position in a document and a textual message describing the error.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Error {
    pos: TextPosition,
    kind: ErrorKind,
}

/// A result type yielded by `XmlReader`.
pub type Result<T> = result::Result<T, Error>;

/// A wrapper around an `std::io::Read` instance which provides pull-based XML parsing.
pub struct EventReader<R: Read> {
    source: R,
    parser: PullParser
}

impl<R: Read> EventReader<R> {
    /// Creates a new reader, consuming the given stream.
    #[inline]
    pub fn new(source: R) -> EventReader<R> {
        EventReader::new_with_config(source, ParserConfig::new())
    }

    /// Creates a new reader with the provded configuration, consuming the given stream.
    #[inline]
    pub fn new_with_config(source: R, config: ParserConfig) -> EventReader<R> {
        EventReader { source: source, parser: PullParser::new(config) }
    }

    /// Pulls and returns next XML event from the stream.
    ///
    /// If returned event is `XmlEvent::Error` or `XmlEvent::EndDocument`, then
    /// further calls to this method will return this event again.
    #[inline]
    pub fn next(&mut self) -> Result<XmlEvent> {
        self.parser.next(&mut self.source)
    }
}

impl<B: Read> Position for EventReader<B> {
    /// Returns the position of the last event produced by the reader.
    #[inline]<commit_msg>Expose `source` via `EventReader::source(..)` and `EventReader::source_mut()`<commit_after>//! Contains high-level interface for a pull-based XML parser.
//!
//! The most important type in this module is `EventReader`, which provides an iterator
//! view for events in XML document.

use std::io::{self, Read};
use std::borrow::Cow;
use std::result;
use std::str;

use common::{Position, TextPosition};

pub use self::config::ParserConfig;
pub use self::events::XmlEvent;

use self::parser::PullParser;

mod lexer;
mod parser;
mod config;
mod events;

mod error_impl;

#[derive(Debug)]
pub enum ErrorKind {
    Syntax( Cow<'static, str> ),
    Io( io::Error ),
    Utf8( str::Utf8Error ),
    UnexpectedEof,
}

/// An XML parsing error.
///
/// Consists of a 2D position in a document and a textual message describing the error.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Error {
    pos: TextPosition,
    kind: ErrorKind,
}

/// A result type yielded by `XmlReader`.
pub type Result<T> = result::Result<T, Error>;

/// A wrapper around an `std::io::Read` instance which provides pull-based XML parsing.
pub struct EventReader<R: Read> {
    source: R,
    parser: PullParser
}

impl<R: Read> EventReader<R> {
    /// Creates a new reader, consuming the given stream.
    #[inline]
    pub fn new(source: R) -> EventReader<R> {
        EventReader::new_with_config(source, ParserConfig::new())
    }

    /// Creates a new reader with the provded configuration, consuming the given stream.
    #[inline]
    pub fn new_with_config(source: R, config: ParserConfig) -> EventReader<R> {
        EventReader { source: source, parser: PullParser::new(config) }
    }

    /// Pulls and returns next XML event from the stream.
    ///
    /// If returned event is `XmlEvent::Error` or `XmlEvent::EndDocument`, then
    /// further calls to this method will return this event again.
    #[inline]
    pub fn next(&mut self) -> Result<XmlEvent> {
        self.parser.next(&mut self.source)
    }

    pub fn source( &self ) -> &R { &self.source }
    pub fn source_mut( &mut self ) -> &mut R { &mut self.source }
}

impl<B: Read> Position for EventReader<B> {
    /// Returns the position of the last event produced by the reader.
    #[inline]
{'fraction_non_alphanumeric': 0.1318783375899698, 'fraction_numerical': 0.0013930810308799629, 'mean_word_length': 3.4139344262295084, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6768963', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1317, 'n_words': 471}