pub struct BufferedReader<R> { /* private fields */ }
Expand description

A buffered PGN reader.

Implementations§

Create a new reader by wrapping a byte slice in a Cursor.

use pgn_reader::BufferedReader;

let pgn = b"1. e4 e5 *";
let reader = BufferedReader::new_cursor(&pgn[..]);

Create a new buffered PGN reader.

use std::fs::File;
use pgn_reader::BufferedReader;

let file = File::open("example.pgn")?;
let reader = BufferedReader::new(file);
Examples found in repository?
src/reader.rs (line 534)
533
534
535
    pub fn new_cursor(inner: T) -> BufferedReader<Cursor<T>> {
        BufferedReader::new(Cursor::new(inner))
    }

Read a single game, if any, and returns the result produced by the visitor. Returns Ok(None) if the underlying reader is empty.

Errors
  • I/O error from the underlying reader.
  • Irrecoverable parser errors.
Examples found in repository?
src/reader.rs (line 587)
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
    pub fn read_all<V: Visitor>(&mut self, visitor: &mut V) -> io::Result<()> {
        while self.read_game(visitor)?.is_some() {}
        Ok(())
    }

    /// Create an iterator over all games.
    ///
    /// # Errors
    ///
    /// * I/O error from the underlying reader.
    /// * Irrecoverable parser errors.
    pub fn into_iter<V: Visitor>(self, visitor: &mut V) -> IntoIter<'_, V, R> {
        IntoIter {
            reader: self,
            visitor,
        }
    }

    /// Gets the remaining bytes in the buffer and the underlying reader.
    pub fn into_inner(self) -> Chain<Cursor<Buffer>, R> {
        Cursor::new(self.buffer).chain(self.inner)
    }

    /// Returns whether the reader has another game to parse, but does not
    /// actually parse it.
    ///
    /// # Errors
    ///
    /// * I/O error from the underlying reader.
    pub fn has_more(&mut self) -> io::Result<bool> {
        self.skip_bom()?;
        self.skip_whitespace()?;
        Ok(self.fill_buffer_and_peek()?.is_some())
    }
}

impl<R: Read> ReadPgn for BufferedReader<R> {
    type Err = io::Error;

    fn fill_buffer_and_peek(&mut self) -> io::Result<Option<u8>> {
        while self.buffer.inner.available_data() < MIN_BUFFER_SIZE {
            let remainder = self.buffer.inner.space();
            let size = self.inner.read(remainder)?;

            if size == 0 {
                break;
            }

            self.buffer.inner.fill(size);
        }

        Ok(self.buffer.inner.data().first().cloned())
    }

    fn invalid_data() -> io::Error {
        io::Error::from(io::ErrorKind::InvalidData)
    }

    fn buffer(&self) -> &[u8] {
        self.buffer.inner.data()
    }

    fn consume(&mut self, bytes: usize) {
        self.buffer.inner.consume(bytes);
    }

    fn peek(&self) -> Option<u8> {
        self.buffer.inner.data().first().cloned()
    }
}

/// Iterator returned by
/// [`BufferedReader::into_iter()`](struct.BufferedReader.html#method.into_iter).
#[derive(Debug)]
#[must_use]
pub struct IntoIter<'a, V: 'a, R> {
    visitor: &'a mut V,
    reader: BufferedReader<R>,
}

impl<'a, V: Visitor, R: Read> Iterator for IntoIter<'a, V, R> {
    type Item = Result<V::Result, io::Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.reader.read_game(self.visitor) {
            Ok(Some(result)) => Some(Ok(result)),
            Ok(None) => None,
            Err(err) => Some(Err(err)),
        }
    }

Skip a single game, if any.

Errors
  • I/O error from the underlying reader.
  • Irrecoverable parser errors.

Read all games.

Errors
  • I/O error from the underlying reader.
  • Irrecoverable parser errors.

Create an iterator over all games.

Errors
  • I/O error from the underlying reader.
  • Irrecoverable parser errors.

Gets the remaining bytes in the buffer and the underlying reader.

Returns whether the reader has another game to parse, but does not actually parse it.

Errors
  • I/O error from the underlying reader.

Trait Implementations§

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.