ca_formats/lib.rs
1/*!
2Parsing pattern files for Conway's Game of Life.
3
4The parsers read a string and return an iterator of coordinates of living cells.
5
6## Supported formats
7
8- [RLE](https://www.conwaylife.com/wiki/Run_Length_Encoded)
9- [Plaintext](https://www.conwaylife.com/wiki/Plaintext)
10- [apgcode](https://www.conwaylife.com/wiki/Apgcode)
11- [Macrocell](https://www.conwaylife.com/wiki/Macrocell)
12
13## Example
14
15### Reading from a string:
16
17```rust
18use ca_formats::rle::Rle;
19
20const GLIDER: &str = r"#N Glider
21#O Richard K. Guy
22#C The smallest, most common, and first discovered spaceship. Diagonal, has period 4 and speed c/4.
23#C www.conwaylife.com/wiki/index.php?title=Glider
24x = 3, y = 3, rule = B3/S23
25bob$2bo$3o!";
26
27let glider = Rle::new(GLIDER).unwrap();
28assert_eq!(glider.header_data().unwrap().x, 3);
29assert_eq!(glider.header_data().unwrap().y, 3);
30assert_eq!(glider.header_data().unwrap().rule, Some(String::from("B3/S23")));
31
32let cells = glider.map(|cell| cell.unwrap().position).collect::<Vec<_>>();
33assert_eq!(cells, vec![(1, 0), (2, 1), (0, 2), (1, 2), (2, 2)]);
34```
35
36### Reading from a file:
37
38```rust
39use std::fs::File;
40use ca_formats::rle::Rle;
41
42let file = File::open("tests/sirrobin.rle").unwrap();
43let sirrobin = Rle::new_from_file(file).unwrap();
44
45assert_eq!(sirrobin.count(), 282);
46```
47
48## See also
49
50- [ca-rules](https://crates.io/crates/ca-rules) - A parser for rule strings.
51- [game-of-life-parsers](https://crates.io/crates/game-of-life-parsers)
52 by René Perschon - Parsers for [Life 1.05](https://www.conwaylife.com/wiki/Life_1.05)
53 and [Life 1.06](https://www.conwaylife.com/wiki/Life_1.06) format.
54
55*/
56
57#![cfg_attr(docs_rs, feature(doc_cfg))]
58
59pub mod apgcode;
60mod input;
61pub mod macrocell;
62pub mod plaintext;
63pub mod rle;
64
65pub use input::Input;
66
67pub type Coordinates = (i64, i64);
68
69/// Position and state of a cell.
70///
71/// Rules with more than 256 states are not supported.
72#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Default, Hash)]
73pub struct CellData {
74 /// Coordinates of the cell.
75 pub position: Coordinates,
76 /// State of the cell.
77 ///
78 /// For rules with only 2 states, `0` means dead and `1` means alive.
79 pub state: u8,
80}
81
82/// Convert the coordinates into a [`CellData`] with state `1`.
83impl From<Coordinates> for CellData {
84 fn from(position: Coordinates) -> Self {
85 Self { position, state: 1 }
86 }
87}