<commit_before>use std::io::{Read, Result};

use super::event::{self, Event, Key};

pub struct Events<R> {
    source: R,
    leftover: Option<u8>
}

impl<R: Read> Events<R> {
    pub fn new(source: R) -> Events<R> {
        Events {
            source: source,
            leftover: None
        }
    }
}

impl<R: Read> Iterator for Events<R> {
    type Item = Result<Event>;

    fn next(&mut self) -> Option<Result<Event>> {
        let mut source = &mut self.source;

        let mut buf = [0u8; 2];

        let result = match source.read(&mut buf) {
            Ok(0) => return None,
            Ok(1) => {
                match buf[0] {
                    b'\x1b' => Ok(Event::Key(Key::Escape)),
                    item => parse_event(item, &mut source.bytes())
                }
            },
            Ok(2) => {
                let mut option_iter = &mut Some(buf[1]).into_iter();

                let result = {<commit_msg>Actually parse using leftover byte<commit_after>use std::io::{Read, Result};

use super::event::{self, Event, Key};

pub struct Events<R> {
    source: R,
    leftover: Option<u8>
}

impl<R: Read> Events<R> {
    pub fn new(source: R) -> Events<R> {
        Events {
            source: source,
            leftover: None
        }
    }
}

impl<R: Read> Iterator for Events<R> {
    type Item = Result<Event>;

    fn next(&mut self) -> Option<Result<Event>> {
        let mut source = &mut self.source;

        if let Some(c) = self.leftover {
            // we have a leftover byte, use it
            self.leftover = None;
            return Some(parse_event(c, &mut source.bytes()));
        }

        let mut buf = [0u8; 2];

        let result = match source.read(&mut buf) {
            Ok(0) => return None,
            Ok(1) => {
                match buf[0] {
                    b'\x1b' => Ok(Event::Key(Key::Escape)),
                    item => parse_event(item, &mut source.bytes())
                }
            },
            Ok(2) => {
                let mut option_iter = &mut Some(buf[1]).into_iter();

                let result = {
{'fraction_non_alphanumeric': 0.1450381679389313, 'fraction_numerical': 0.009541984732824428, 'mean_word_length': 1.6179775280898876, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 37, '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': '5193670', 'n_tokens_mistral': 685, 'n_tokens_neox': 648, 'n_words': 169}