1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! A parser for [PlainText](https://www.conwaylife.com/wiki/Plaintext) format.
//!
//! # Example
//!
//! ```rust
//! use ca_formats::plaintext::Plaintext;
//!
//! const GLIDER: &str = r"!Name: Glider
//! !Author: Richard K. Guy
//! !The smallest, most common, and first discovered spaceship.
//! !www.conwaylife.com/wiki/index.php?title=Glider
//! .O
//! ..O
//! OOO";
//! let mut glider = Plaintext::new(GLIDER).collect::<Result<Vec<_>, _>>().unwrap();
//! assert_eq!(glider, vec![(0, 1), (1, 2), (2, 0), (2, 1), (2, 2)]);
//! ```
//!

use crate::Error;
use std::str::{Bytes, Lines};

/// An iterator of coordinates of living cells. Returns by parsing a
/// [PlainText](https://www.conwaylife.com/wiki/Plaintext) file.
pub struct Plaintext<'a> {
    lines: Lines<'a>,
    current_line: Bytes<'a>,
    x: i32,
    y: i32,
}

impl Plaintext<'_> {
    /// Creates a new iterator from a string.
    pub fn new(text: &str) -> Plaintext {
        Plaintext {
            lines: text.lines(),
            current_line: "".bytes(),
            x: -1,
            y: -1,
        }
    }
}

impl<'a> Iterator for Plaintext<'a> {
    /// Coordinates of a living cell, or an error in the pattern string.
    type Item = Result<(i32, i32), Error>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if let Some(c) = self.current_line.next() {
                match c {
                    b'O' => {
                        self.x += 1;
                        return Some(Ok((self.y, self.x)));
                    }
                    b'.' => self.x += 1,
                    _ => return Some(Err(Error::UnexpectedByte(c))),
                }
            } else if let Some(l) = self.lines.next() {
                if l.starts_with('!') {
                    continue;
                } else {
                    self.x = -1;
                    self.y += 1;
                    self.current_line = l.bytes();
                }
            } else {
                return None;
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn glider_plaintext() -> Result<(), Error> {
        const GLIDER: &str = r"!Name: Glider
!Author: Richard K. Guy
!The smallest, most common, and first discovered spaceship.
!www.conwaylife.com/wiki/index.php?title=Glider
.O
..O
OOO";
        let mut glider = Plaintext::new(GLIDER).collect::<Result<Vec<_>, _>>()?;
        glider.sort();
        assert_eq!(glider, vec![(0, 1), (1, 2), (2, 0), (2, 1), (2, 2)]);
        Ok(())
    }

    #[test]
    fn twin_bees_shuttle_plaintext() -> Result<(), Error> {
        const TWIN_BEE_SHUTTLE: &str = r"!Name: Twin bees shuttle
!Author: Bill Gosper
!Twin bees shuttle was found in 1971 and is the basis of all known period 46 oscillators.
!www.conwaylife.com/wiki/index.php?title=Twin_bees_shuttle
.................OO
OO...............O.O.......OO
OO.................O.......OO
.................OOO



.................OOO
OO.................O
OO...............O.O
.................OO";
        let mut twin_bees_shuttle =
            Plaintext::new(TWIN_BEE_SHUTTLE).collect::<Result<Vec<_>, _>>()?;
        twin_bees_shuttle.sort();
        assert_eq!(
            twin_bees_shuttle,
            vec![
                (0, 17),
                (0, 18),
                (1, 0),
                (1, 1),
                (1, 17),
                (1, 19),
                (1, 27),
                (1, 28),
                (2, 0),
                (2, 1),
                (2, 19),
                (2, 27),
                (2, 28),
                (3, 17),
                (3, 18),
                (3, 19),
                (7, 17),
                (7, 18),
                (7, 19),
                (8, 0),
                (8, 1),
                (8, 19),
                (9, 0),
                (9, 1),
                (9, 17),
                (9, 19),
                (10, 17),
                (10, 18)
            ]
        );
        Ok(())
    }
}