sokoban-elements 0.2.1

Parsing and writing Sokoban, Hexoban, Trioban and Octoban file formats.
Documentation
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use std::io::{BufRead, Read, Seek};
use std::mem;

use itertools::Itertools;
use regex::Regex;

use super::super::errors::IoReadError;
use super::super::file_format::FileFormatReader;
use super::helpers::{
    AUTHOR, BOXORDER, COLLECTION, CollectionData, DATE_CREATED, DATE_OF_LAST_CHANGE, GOALORDER,
    NumberedLine, PuzzleData, RAW_FILE_NOTES, SOLVER, SavedGameData, TAG_DELIMITERS, TITLE,
    VARIANT,
};
use crate::game_board::is_board_line;
use crate::saved_game::is_saved_game_line;
use crate::utilities::is_blank;
use crate::{Collection, Tessellation};

/// # Todo
///
/// - add url / email tags?
/// - when reading titles read "Title" but then check if it is the same as "title line"
pub struct SokReader {
    data:                CollectionData,
    notes_parser:        SokNotesTags,
    legacy_sokoban_plus: bool,
}

impl FileFormatReader for SokReader {
    fn read<R>(&mut self, src: &mut R) -> Result<Collection, IoReadError>
    where
        R: Read + BufRead + Seek,
    {
        self.read_and_split(src)?;
        self.parse_title_lines();
        self.parse_notes()?;
        self.data.create_collection(self.legacy_sokoban_plus)
    }
}

impl SokReader {
    // Uses `tessellation_hint` for any encountered [`Puzzle`] that doesn't have
    // associated tessellation info.
    //
    // If provided `tessellation_hint` is [`None`], these puzzles` will use
    // [`Sokoban`](Tessellation::Sokoban).
    pub fn new(tessellation_hint: Option<Tessellation>, legacy_sokoban_plus: bool) -> Self {
        Self {
            legacy_sokoban_plus,
            data: CollectionData::new(tessellation_hint),
            notes_parser: SokNotesTags::new(match tessellation_hint {
                Some(t) => t,
                None => Tessellation::Sokoban,
            }),
        }
    }

    fn read_and_split<R>(&mut self, src: &mut R) -> Result<(), IoReadError>
    where
        R: Read + BufRead + Seek,
    {
        let mut lines: Vec<NumberedLine> = Vec::new();

        for (idx, line) in src.lines().enumerate() {
            lines.push(NumberedLine { ln: line?, idx });
        }
        self.split_input(&mut lines);

        Ok(())
    }

    fn parse_notes(&mut self) -> Result<bool, IoReadError> {
        self.notes_parser
            .extract_collection_attributes(&mut self.data)?;
        Self::cleanup_whitespace(&mut self.data.notes);

        for puzzle in &mut self.data.puzzles {
            self.notes_parser
                .extract_puzzle_attributes(puzzle, self.data.header_tessellation_hint)?;

            for save_game in &mut puzzle.saved_games {
                self.notes_parser.extract_save_game_attributes(save_game);
                Self::cleanup_whitespace(&mut save_game.notes);
            }

            Self::cleanup_whitespace(&mut puzzle.notes);
        }
        Ok(true)
    }

    fn parse_title_lines(&mut self) {
        let puzzles_len = self.data.puzzles.len();
        for puzzle_idx in 0..puzzles_len {
            let title = Self::get_and_remove_title_line(Self::choose_ref_notes_before_puzzle(
                &mut self.data,
                puzzle_idx,
            ));
            self.data.puzzles[puzzle_idx].title = title;

            let saves_len = self.data.puzzles[puzzle_idx].saved_games.len();
            for save_idx in 0..saves_len {
                let title = Self::get_and_remove_title_line(
                    Self::choose_ref_notes_before_save_game(&mut self.data, puzzle_idx, save_idx),
                );
                self.data.puzzles[puzzle_idx].saved_games[save_idx].title = title;
            }
        }
    }

    fn split_input(&mut self, input_lines: &mut Vec<NumberedLine>) {
        let first_board_line_idx = input_lines
            .iter()
            .position(|num_ln| is_board_line(&num_ln.ln));

        match first_board_line_idx {
            Some(idx) => {
                Self::drain_into(input_lines, idx, &mut self.data.notes);
            }
            None => {
                mem::swap(&mut self.data.notes, input_lines);
            }
        }

        self.split_into_puzzles(input_lines);
        self.split_into_saved_games();
    }

    fn split_into_puzzles(&mut self, input_lines: &mut Vec<NumberedLine>) {
        while !input_lines.is_empty() {
            let mut puzzle = PuzzleData::new(input_lines[0].idx);

            // first take the board lines if there are some
            let first_note_line_idx = input_lines
                .iter()
                .position(|num_ln| !is_board_line(&num_ln.ln));
            if let Some(idx) = first_note_line_idx {
                puzzle.board = input_lines[..idx]
                    .iter()
                    .map(|num_ln| &num_ln.ln)
                    .join("\n");
                let mut dropped: Vec<NumberedLine> = Vec::new();
                Self::drain_into(input_lines, idx, &mut dropped);
            } else {
                puzzle.board = input_lines.iter().map(|num_ln| &num_ln.ln).join("\n");
                input_lines.clear();
            }

            // and take any remaining lines after board but before next board and put them
            // into puzzle notes
            if !input_lines.is_empty() {
                let first_board_line_idx = input_lines
                    .iter()
                    .position(|num_ln| is_board_line(&num_ln.ln));

                match first_board_line_idx {
                    Some(idx) => {
                        Self::drain_into(input_lines, idx, &mut puzzle.notes);
                    }
                    None => {
                        mem::swap(&mut puzzle.notes, input_lines);
                    }
                }
            }

            self.data.puzzles.push(puzzle);
        }
    }

    fn split_into_saved_games(&mut self) {
        for puzzle in &mut self.data.puzzles {
            let mut input_lines = mem::take(&mut puzzle.notes);

            // take any lines between board and first saved game
            let first_moves_line_idx = input_lines
                .iter()
                .position(|num_ln| is_saved_game_line(&num_ln.ln));
            match first_moves_line_idx {
                Some(idx) => {
                    Self::drain_into(&mut input_lines, idx, &mut puzzle.notes);
                }
                None => {
                    puzzle.notes = mem::take(&mut input_lines);
                }
            }

            while !input_lines.is_empty() {
                let mut saved_game = SavedGameData::new(input_lines[0].idx);

                let first_note_line_idx = input_lines
                    .iter()
                    .position(|num_ln| !is_saved_game_line(&num_ln.ln));
                if let Some(idx) = first_note_line_idx {
                    saved_game.moves = input_lines[..idx]
                        .iter()
                        .map(|num_ln| num_ln.ln.trim())
                        .join("");
                    let mut dropped: Vec<NumberedLine> = Vec::new();
                    Self::drain_into(&mut input_lines, idx, &mut dropped);
                } else {
                    saved_game.moves = input_lines.iter().map(|num_ln| num_ln.ln.trim()).join("");
                    input_lines.clear();
                }

                if !input_lines.is_empty() {
                    let first_moves_line_idx = input_lines
                        .iter()
                        .position(|num_ln| is_saved_game_line(&num_ln.ln));

                    match first_moves_line_idx {
                        Some(idx) => {
                            Self::drain_into(&mut input_lines, idx, &mut saved_game.notes);
                        }
                        None => {
                            mem::swap(&mut input_lines, &mut saved_game.notes);
                        }
                    }
                }

                puzzle.saved_games.push(saved_game);
            }
        }
    }

    fn choose_ref_notes_before_puzzle(
        data: &mut CollectionData, puzzle_idx: usize,
    ) -> &mut Vec<NumberedLine> {
        if puzzle_idx == 0 {
            &mut data.notes
        } else {
            let previous_puzzle = &mut data.puzzles[puzzle_idx - 1];
            if previous_puzzle.saved_games.is_empty() {
                &mut previous_puzzle.notes
            } else {
                let last_idx = previous_puzzle.saved_games.len() - 1;
                &mut previous_puzzle.saved_games[last_idx].notes
            }
        }
    }

    fn choose_ref_notes_before_save_game(
        data: &mut CollectionData, puzzle_idx: usize, saved_game_idx: usize,
    ) -> &mut Vec<NumberedLine> {
        let puzzle = &mut data.puzzles[puzzle_idx];
        if saved_game_idx == 0 {
            &mut puzzle.notes
        } else {
            &mut puzzle.saved_games[saved_game_idx - 1].notes
        }
    }

    fn get_and_remove_title_line(notes: &mut Vec<NumberedLine>) -> String {
        // :: Titles                                                 ::
        // ::   A title line is the last non-blank text line before  ::
        // ::   a board, a saved game, or a solution, provided the   ::
        // ::   line is preceded by a blank line or it is the only   ::
        // ::   text line at this position in the file.              ::
        // ::                                                        ::
        // ::   Title lines are optional unless a single or a last   ::
        // ::   text line from a preceding puzzle, saved game,       ::
        // ::   solution, or file header can be mistaken for a title ::
        // ::   line.                                                ::
        let candidate_index = notes.iter().rposition(|num_ln| !is_blank(&num_ln.ln));

        match candidate_index {
            Some(idx) => {
                let mut preceding_index: Option<usize> = None;
                if idx > 0 {
                    preceding_index = Some(idx - 1);
                }

                let mut following_index: Option<usize> = None;
                if idx < notes.len() - 1 {
                    following_index = Some(idx + 1);
                }

                let preceding_ok = if let Some(pi) = preceding_index {
                    is_blank(&notes[pi].ln)
                } else {
                    true
                };
                let following_ok = if let Some(fi) = following_index {
                    is_blank(&notes[fi].ln)
                } else {
                    true
                };

                if preceding_ok && following_ok {
                    let title_line = notes[idx].ln.trim().to_string();
                    notes.remove(idx);
                    title_line
                } else {
                    String::new()
                }
            }
            None => String::new(),
        }
    }

    fn drain_into(from: &mut Vec<NumberedLine>, at: usize, into: &mut Vec<NumberedLine>) {
        for num_ln in &mut from[..at] {
            into.push(NumberedLine {
                idx: num_ln.idx,
                ln:  mem::take(&mut num_ln.ln),
            });
        }

        let mut remainder: Vec<NumberedLine> = Vec::with_capacity(from[at..].len());
        for num_ln in &mut from[at..] {
            remainder.push(NumberedLine {
                idx: num_ln.idx,
                ln:  mem::take(&mut num_ln.ln),
            });
        }
        mem::swap(from, &mut remainder);
    }

    fn cleanup_whitespace(notes: &mut Vec<NumberedLine>) {
        let mut cleaned: Vec<NumberedLine> = Vec::new();

        for num_ln in notes
            .iter_mut()
            .dedup_by(|l, r| is_blank(&l.ln) && is_blank(&r.ln))
        {
            cleaned.push(NumberedLine {
                idx: num_ln.idx,
                ln:  mem::take(&mut num_ln.ln),
            });
        }

        if !cleaned.is_empty() && is_blank(&cleaned[0].ln) {
            cleaned.remove(0);
        }

        if !cleaned.is_empty() && is_blank(cleaned.last().map_or("", |l| &l.ln)) {
            cleaned.pop();
        }

        mem::swap(&mut cleaned, notes);
    }
}

struct SokNotesTags {
    tag_splitter:               Regex,
    supplied_tessellation_hint: Tessellation,
}

impl SokNotesTags {
    #[expect(clippy::expect_used)]
    pub fn new(tessellation_hint: Tessellation) -> Self {
        Self {
            tag_splitter:               Regex::new(&format!(
                "(?<tag>.*?)[{TAG_DELIMITERS}](?<value>.*)"
            ))
            .expect("Bug: failed to construct Regex!"),
            supplied_tessellation_hint: tessellation_hint,
        }
    }

    pub fn is_raw_file_notes_line(line: &str) -> bool { line.starts_with(RAW_FILE_NOTES) }

    pub fn get_tag_data(&self, tag: &str, line: &str) -> Option<String> {
        let caps = self.tag_splitter.captures(line)?;
        (tag.to_lowercase().trim() == caps["tag"].to_lowercase().trim())
            .then(|| caps["value"].trim().to_string())
    }

    pub fn extract_collection_attributes(
        &self, dest: &mut CollectionData,
    ) -> Result<bool, IoReadError> {
        let mut remaining_lines: Vec<NumberedLine> = Vec::new();

        for line in &mut dest.notes {
            if let Some(s) = self.get_tag_data(TITLE, &line.ln) {
                dest.title = s;
            } else if let Some(s) = self.get_tag_data(AUTHOR, &line.ln) {
                dest.author = s;
            } else if let Some(s) = self.get_tag_data(COLLECTION, &line.ln) {
                dest.title = s;
            } else if let Some(s) = self.get_tag_data(DATE_CREATED, &line.ln) {
                dest.created_at = s;
            } else if let Some(s) = self.get_tag_data(DATE_OF_LAST_CHANGE, &line.ln) {
                dest.updated_at = s;
            } else if let Some(s) = self.get_tag_data(VARIANT, &line.ln) {
                let tessellation = Self::parse_tessellation_str(line.idx, s.as_str())?;
                dest.was_tessellation_in_header = true;
                dest.header_tessellation_hint = Some(tessellation);
            } else if !Self::is_raw_file_notes_line(&line.ln) && !is_blank(&line.ln) {
                remaining_lines.push(NumberedLine {
                    idx: line.idx,
                    ln:  mem::take(&mut line.ln),
                });
            }
        }

        mem::swap(&mut dest.notes, &mut remaining_lines);
        Ok(true)
    }

    pub fn extract_puzzle_attributes(
        &self, dest: &mut PuzzleData, collection_header_tessellation_hint: Option<Tessellation>,
    ) -> Result<bool, IoReadError> {
        let mut remaining_lines: Vec<NumberedLine> = Vec::new();
        let mut tessellation: Option<Tessellation> = None;

        for line in &mut dest.notes {
            if let Some(s) = self.get_tag_data(TITLE, &line.ln) {
                dest.title = s;
            } else if let Some(s) = self.get_tag_data(AUTHOR, &line.ln) {
                dest.author = s;
            } else if let Some(s) = self.get_tag_data(BOXORDER, &line.ln) {
                dest.boxorder = s;
                dest.sokplus_ln_idx = line.idx;
            } else if let Some(s) = self.get_tag_data(GOALORDER, &line.ln) {
                dest.goalorder = s;
                dest.sokplus_ln_idx = line.idx;
            } else if let Some(s) = self.get_tag_data(VARIANT, &line.ln) {
                let t = Self::parse_tessellation_str(line.idx, s.as_str())?;
                tessellation = Some(t);
            } else {
                remaining_lines.push(NumberedLine {
                    idx: line.idx,
                    ln:  mem::take(&mut line.ln),
                });
            }
        }

        if let Some(t) = tessellation {
            dest.tessellation = t;
        } else if let Some(t) = collection_header_tessellation_hint {
            dest.tessellation = t;
        } else {
            dest.tessellation = self.supplied_tessellation_hint;
        }

        mem::swap(&mut dest.notes, &mut remaining_lines);
        Ok(true)
    }

    pub fn extract_save_game_attributes(&self, dest: &mut SavedGameData) {
        let mut remaining_lines: Vec<NumberedLine> = Vec::new();

        for line in &mut dest.notes {
            if let Some(s) = self.get_tag_data(TITLE, &line.ln) {
                dest.title = s;
            } else if let Some(s) = self.get_tag_data(AUTHOR, &line.ln) {
                dest.solver = s;
            } else if let Some(s) = self.get_tag_data(SOLVER, &line.ln) {
                dest.solver = s;
            } else {
                remaining_lines.push(NumberedLine {
                    idx: line.idx,
                    ln:  mem::take(&mut line.ln),
                });
            }
        }

        mem::swap(&mut dest.notes, &mut remaining_lines);
    }

    // The one place a new `Tessellation` variant slips through the compiler. Its counterpart,
    // `SokWriter::variant_name()`, matches on the variant itself and so has to gain an arm - this
    // matches on text and keeps compiling, which means the crate would write a tessellation name it
    // then refuses to read back. Nothing fails until a round trip.
    fn parse_tessellation_str(ln_pos: usize, value: &str) -> Result<Tessellation, IoReadError> {
        match value.trim().to_lowercase().as_str() {
            "hexoban" => Ok(Tessellation::Hexoban),
            "octoban" => Ok(Tessellation::Octoban),
            "trioban" => Ok(Tessellation::Trioban),
            "sokoban" | "" => Ok(Tessellation::Sokoban),
            _ => Err(IoReadError::new_unknown_tessellation_error(ln_pos, value)),
        }
    }
}