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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
use anyhow::Result;
use std::fmt;
use std::io::Read;
use std::str::FromStr;
use super::{RleHeader, RleParser, RleRunsTriple};
use crate::{Format, Position, Rule};
/// A representation for RLE file format.
///
/// The detail of this format is described in:
///
/// - [Run Length Encoded - LifeWiki](https://conwaylife.com/wiki/Run_Length_Encoded)
/// - [Golly Help: File Formats > Extended RLE format](https://golly.sourceforge.net/Help/formats.html#rle)
///
/// # Examples
///
/// Parses the given RLE file, and checks live cells included in it:
///
/// ```
/// use std::fs::File;
/// use life_backend::format::Rle;
/// use life_backend::Position;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let file = File::open("patterns/rpentomino.rle")?;
/// let parser = Rle::new(file)?;
/// assert!(parser.live_cells().eq([Position(1, 0), Position(2, 0), Position(0, 1), Position(1, 1), Position(1, 2)]));
/// # Ok(())
/// # }
/// ```
///
/// Parses the given string in RLE format:
///
/// ```
/// use life_backend::format::Rle;
/// use life_backend::Position;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let pattern = "\
/// #N R-pentomino\n\
/// x = 3, y = 3\n\
/// b2o$2o$bo!\n\
/// ";
/// let parser = pattern.parse::<Rle>()?;
/// assert!(parser.live_cells().eq([Position(1, 0), Position(2, 0), Position(0, 1), Position(1, 1), Position(1, 2)]));
/// # Ok(())
/// # }
/// ```
///
#[derive(Clone, Debug)]
pub struct Rle {
pub(super) header: RleHeader,
pub(super) comments: Vec<String>,
pub(super) contents: Vec<RleRunsTriple>,
}
// Inherent methods
impl Rle {
/// Creates from the specified implementor of [`Read`], such as [`File`] or `&[u8]`.
///
/// [`Read`]: std::io::Read
/// [`File`]: std::fs::File
///
/// # Examples
///
/// ```
/// use life_backend::format::Rle;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let pattern = "\
/// #N T-tetromino\n\
/// x = 3, y = 2\n\
/// 3o$bo!\n\
/// ";
/// let parser = Rle::new(pattern.as_bytes())?;
/// # Ok(())
/// # }
/// ```
///
#[inline]
pub fn new<R>(read: R) -> Result<Self>
where
R: Read,
{
RleParser::parse(read)
}
/// Returns the width written in the pattern.
///
/// # Examples
///
/// ```
/// use life_backend::format::Rle;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let pattern = "\
/// #N T-tetromino\n\
/// x = 3, y = 2\n\
/// 3o$bo!\n\
/// ";
/// let parser = Rle::new(pattern.as_bytes())?;
/// assert_eq!(parser.width(), 3);
/// # Ok(())
/// # }
/// ```
///
#[inline]
pub const fn width(&self) -> usize {
self.header.width
}
/// Returns the height written in the pattern.
///
/// # Examples
///
/// ```
/// use life_backend::format::Rle;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let pattern = "\
/// #N T-tetromino\n\
/// x = 3, y = 2\n\
/// 3o$bo!\n\
/// ";
/// let parser = Rle::new(pattern.as_bytes())?;
/// assert_eq!(parser.height(), 2);
/// # Ok(())
/// # }
/// ```
///
#[inline]
pub const fn height(&self) -> usize {
self.header.height
}
/// Returns the rule.
///
/// # Examples
///
/// ```
/// use life_backend::format::Rle;
/// use life_backend::Rule;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let pattern = "\
/// #N T-tetromino\n\
/// x = 3, y = 2, rule = B3/S23\n\
/// 3o$bo!\n\
/// ";
/// let parser = Rle::new(pattern.as_bytes())?;
/// assert_eq!(parser.rule(), &Rule::conways_life());
/// # Ok(())
/// # }
/// ```
///
#[inline]
pub const fn rule(&self) -> &Rule {
&self.header.rule
}
/// Returns comments of the pattern.
///
/// # Examples
///
/// ```
/// use life_backend::format::Rle;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let pattern = "\
/// #N T-tetromino\n\
/// x = 3, y = 2\n\
/// 3o$bo!\n\
/// ";
/// let parser = Rle::new(pattern.as_bytes())?;
/// assert_eq!(parser.comments().len(), 1);
/// assert_eq!(parser.comments()[0], "#N T-tetromino");
/// # Ok(())
/// # }
/// ```
///
#[inline]
pub const fn comments(&self) -> &Vec<String> {
&self.comments
}
/// Creates an owning iterator over the series of live cell positions in ascending order.
///
/// # Examples
///
/// ```
/// use life_backend::format::Rle;
/// use life_backend::Position;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let pattern = "\
/// #N T-tetromino\n\
/// x = 3, y = 2\n\
/// 3o$bo!\n\
/// ";
/// let parser = Rle::new(pattern.as_bytes())?;
/// assert!(parser.live_cells().eq([Position(0, 0), Position(1, 0), Position(2, 0), Position(1, 1)]));
/// # Ok(())
/// # }
/// ```
///
pub fn live_cells(&self) -> impl Iterator<Item = Position<usize>> + '_ {
self.contents
.iter()
.scan((0, 0), |(state_x, state_y), item| {
if item.pad_lines > 0 {
*state_y += item.pad_lines;
*state_x = 0;
}
if item.pad_dead_cells > 0 {
*state_x += item.pad_dead_cells;
}
let output = (*state_y, *state_x, item.live_cells);
*state_x += item.live_cells;
Some(output)
})
.flat_map(|(y, x, num)| (x..(x + num)).map(move |x| Position(x, y)))
}
}
// Trait implementations
impl Format for Rle {
fn rule(&self) -> Rule {
self.rule().clone()
}
fn live_cells(&self) -> Box<dyn Iterator<Item = Position<usize>> + '_> {
Box::new(self.live_cells())
}
}
impl fmt::Display for Rle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
const MAX_LINE_WIDTH: usize = 70;
fn convert_run_to_string(run_count: usize, tag_char: char) -> String {
if run_count > 1 {
let mut buf = run_count.to_string();
buf.push(tag_char);
buf
} else {
tag_char.to_string()
}
}
fn flush_buf(f: &mut fmt::Formatter, buf: &mut String) -> Result<(), fmt::Error> {
writeln!(f, "{buf}")?;
Ok(())
}
fn write_with_buf(f: &mut fmt::Formatter, buf: &mut String, s: &str) -> Result<(), fmt::Error> {
if buf.len() + s.len() > MAX_LINE_WIDTH {
flush_buf(f, buf)?;
buf.clear();
}
*buf += s;
Ok(())
}
for line in self.comments() {
writeln!(f, "{line}")?;
}
writeln!(f, "x = {}, y = {}, rule = {}", self.width(), self.height(), self.rule())?;
let mut buf = String::new();
for x in &self.contents {
for (run_count, tag_char) in [(x.pad_lines, '$'), (x.pad_dead_cells, 'b'), (x.live_cells, 'o')] {
if run_count > 0 {
let s = convert_run_to_string(run_count, tag_char);
write_with_buf(f, &mut buf, &s)?;
}
}
}
write_with_buf(f, &mut buf, "!")?;
flush_buf(f, &mut buf)?;
Ok(())
}
}
impl FromStr for Rle {
type Err = anyhow::Error;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::new(s.as_bytes())
}
}