puz-parse 0.1.2

A Rust library for parsing .puz crossword puzzle files
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use super::io::read_bytes;
use crate::{
    error::PuzError,
    types::{Grid, FREE_SQUARE, TAKEN_SQUARE},
};
use std::io::{BufReader, Read};

pub(crate) fn parse_grids<R: Read>(
    reader: &mut BufReader<R>,
    width: u8,
    height: u8,
) -> Result<Grid, PuzError> {
    // Grid data format (after header and before strings):
    // See: https://github.com/mwln/puz.rs/blob/main/PUZ.md
    //
    // The grids are stored as two consecutive byte arrays:
    // 1. Solution grid: width * height bytes (actual puzzle answers)
    // 2. Blank grid: width * height bytes (puzzle state for solver)
    //
    // Each byte represents one cell:
    // - '.' (0x2E) = black/blocked square
    // - '-' (0x2D) = empty square (in blank grid)
    // - A-Z, 0-9 = letter/number content

    let board_size = (width as usize) * (height as usize);

    // Read solution grid (width * height bytes)
    let solution_bytes = read_bytes(reader, board_size)?;
    let solution_chars: String = solution_bytes.iter().map(|&b| b as char).collect();

    // Read blank grid (width * height bytes)
    let blank_bytes = read_bytes(reader, board_size)?;
    let blank_chars: String = blank_bytes.iter().map(|&b| b as char).collect();

    // Convert flat strings to row-based grids
    let solution = string_to_grid(&solution_chars, width as usize);
    let blank = string_to_grid(&blank_chars, width as usize);

    // Ensure blocked squares match between grids
    validate_grid_consistency(&solution, &blank, width, height)?;

    Ok(Grid { blank, solution })
}

fn string_to_grid(s: &str, width: usize) -> Vec<String> {
    s.chars()
        .collect::<Vec<char>>()
        .chunks(width)
        .map(|chunk| chunk.iter().collect::<String>())
        .collect()
}

fn validate_grid_consistency(
    solution: &[String],
    blank: &[String],
    width: u8,
    height: u8,
) -> Result<(), PuzError> {
    if solution.len() != height as usize || blank.len() != height as usize {
        return Err(PuzError::InvalidGrid {
            reason: format!(
                "Grid height mismatch: expected {}, got solution: {}, blank: {}",
                height,
                solution.len(),
                blank.len()
            ),
        });
    }

    for (i, (sol_row, blank_row)) in solution.iter().zip(blank.iter()).enumerate() {
        if sol_row.len() != width as usize || blank_row.len() != width as usize {
            return Err(PuzError::InvalidGrid {
                reason: format!(
                    "Grid width mismatch at row {}: expected {}, got solution: {}, blank: {}",
                    i,
                    width,
                    sol_row.len(),
                    blank_row.len()
                ),
            });
        }

        for (j, (sol_char, blank_char)) in sol_row.chars().zip(blank_row.chars()).enumerate() {
            if (sol_char == TAKEN_SQUARE) != (blank_char == TAKEN_SQUARE) {
                return Err(PuzError::InvalidGrid {
                    reason: format!(
                        "Grid consistency error at ({i}, {j}): blocked squares don't match"
                    ),
                });
            }
        }
    }

    Ok(())
}

pub(crate) fn cell_needs_across_clue(grid: &[String], row: usize, col: usize) -> bool {
    if let Some(row_str) = grid.get(row) {
        if let Some(this_char) = row_str.chars().nth(col) {
            if this_char == FREE_SQUARE {
                if let Some(next_char) = row_str.chars().nth(col + 1) {
                    if next_char == FREE_SQUARE {
                        return col == 0 || row_str.chars().nth(col - 1) == Some(TAKEN_SQUARE);
                    }
                }
            }
        }
    }
    false
}

pub(crate) fn cell_needs_down_clue(grid: &[String], row: usize, col: usize) -> bool {
    if let Some(row_str) = grid.get(row) {
        if let Some(this_char) = row_str.chars().nth(col) {
            if this_char == FREE_SQUARE {
                if let Some(next_row) = grid.get(row + 1) {
                    if let Some(next_char) = next_row.chars().nth(col) {
                        if next_char == FREE_SQUARE {
                            return row == 0
                                || grid.get(row - 1).and_then(|r| r.chars().nth(col))
                                    == Some(TAKEN_SQUARE);
                        }
                    }
                }
            }
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;

    /// Test parsing valid grids with standard layout
    /// This covers the most common case of rectangular crossword grids
    #[test]
    fn test_parse_grids_valid() {
        let width = 3u8;
        let height = 3u8;

        // Create solution grid: simple 3x3 with some black squares (9 bytes)
        let solution_data = b"ABC.DEFGH";
        // Create blank grid: same pattern but with dashes for empty squares (9 bytes, blacks must match)
        let blank_data = b"---.-----";

        let mut data = Vec::new();
        data.extend_from_slice(solution_data);
        data.extend_from_slice(blank_data);

        let mut reader = BufReader::new(Cursor::new(data));
        let grid = parse_grids(&mut reader, width, height).unwrap();

        assert_eq!(grid.solution.len(), 3);
        assert_eq!(grid.blank.len(), 3);
        assert_eq!(grid.solution[0], "ABC");
        assert_eq!(grid.solution[1], ".DE");
        assert_eq!(grid.solution[2], "FGH");
        assert_eq!(grid.blank[0], "---");
        assert_eq!(grid.blank[1], ".--");
        assert_eq!(grid.blank[2], "---");
    }

    /// Test parsing grids with all black squares
    /// Edge case where entire puzzle is blocked
    #[test]
    fn test_parse_grids_all_black() {
        let width = 2u8;
        let height = 2u8;

        let solution_data = b"....";
        let blank_data = b"....";

        let mut data = Vec::new();
        data.extend_from_slice(solution_data);
        data.extend_from_slice(blank_data);

        let mut reader = BufReader::new(Cursor::new(data));
        let grid = parse_grids(&mut reader, width, height).unwrap();

        assert_eq!(grid.solution, vec!["..".to_string(), "..".to_string()]);
        assert_eq!(grid.blank, vec!["..".to_string(), "..".to_string()]);
    }

    /// Test parsing grids with all free squares
    /// Edge case where no squares are blocked
    #[test]
    fn test_parse_grids_all_free() {
        let width = 2u8;
        let height = 2u8;

        let solution_data = b"ABCD";
        let blank_data = b"----";

        let mut data = Vec::new();
        data.extend_from_slice(solution_data);
        data.extend_from_slice(blank_data);

        let mut reader = BufReader::new(Cursor::new(data));
        let grid = parse_grids(&mut reader, width, height).unwrap();

        assert_eq!(grid.solution, vec!["AB".to_string(), "CD".to_string()]);
        assert_eq!(grid.blank, vec!["--".to_string(), "--".to_string()]);
    }

    /// Test parsing 1x1 grid (minimal valid case)
    /// Ensures single-cell puzzles work
    #[test]
    fn test_parse_grids_single_cell() {
        let width = 1u8;
        let height = 1u8;

        let solution_data = b"A";
        let blank_data = b"-";

        let mut data = Vec::new();
        data.extend_from_slice(solution_data);
        data.extend_from_slice(blank_data);

        let mut reader = BufReader::new(Cursor::new(data));
        let grid = parse_grids(&mut reader, width, height).unwrap();

        assert_eq!(grid.solution, vec!["A".to_string()]);
        assert_eq!(grid.blank, vec!["-".to_string()]);
    }

    /// Test parsing large grid
    /// Ensures performance with typical newspaper puzzle sizes
    #[test]
    fn test_parse_grids_large() {
        let width = 15u8;
        let height = 15u8;
        let board_size = (width as usize) * (height as usize);

        // Create alternating pattern
        let mut solution_data = Vec::new();
        let mut blank_data = Vec::new();

        for i in 0..board_size {
            if i % 2 == 0 {
                solution_data.push(b'A');
                blank_data.push(b'-');
            } else {
                solution_data.push(b'.');
                blank_data.push(b'.');
            }
        }

        let mut data = Vec::new();
        data.extend(solution_data);
        data.extend(blank_data);

        let mut reader = BufReader::new(Cursor::new(data));
        let grid = parse_grids(&mut reader, width, height).unwrap();

        assert_eq!(grid.solution.len(), 15);
        assert_eq!(grid.blank.len(), 15);
        assert_eq!(grid.solution[0].len(), 15);
        assert_eq!(grid.blank[0].len(), 15);
    }

    /// Test grid parsing with insufficient data
    /// Should handle truncated grid data gracefully
    #[test]
    fn test_parse_grids_insufficient_data() {
        let width = 3u8;
        let height = 3u8;

        // Only provide solution data, missing blank data
        let solution_data = b"ABC.DEFGH";

        let mut reader = BufReader::new(Cursor::new(solution_data));
        let result = parse_grids(&mut reader, width, height);

        assert!(result.is_err());
        matches!(result.unwrap_err(), PuzError::IoError { .. });
    }

    /// Test grid parsing with consistency validation failure
    /// Blank and solution grids must have matching blocked squares
    #[test]
    fn test_parse_grids_consistency_failure() {
        let width = 2u8;
        let height = 2u8;

        // Solution has black square at (0,1), blank doesn't
        let solution_data = b"A.BC";
        let blank_data = b"--B-"; // Inconsistent: should be "-.B-"

        let mut data = Vec::new();
        data.extend_from_slice(solution_data);
        data.extend_from_slice(blank_data);

        let mut reader = BufReader::new(Cursor::new(data));
        let result = parse_grids(&mut reader, width, height);

        assert!(result.is_err());
        if let Err(PuzError::InvalidGrid { reason }) = result {
            assert!(reason.contains("consistency error"));
        } else {
            panic!("Expected InvalidGrid error with consistency message");
        }
    }

    /// Test string_to_grid function with various inputs
    /// This is the core grid transformation logic
    #[test]
    fn test_string_to_grid() {
        // Test normal case
        let result = string_to_grid("ABCDEF", 3);
        assert_eq!(result, vec!["ABC".to_string(), "DEF".to_string()]);

        // Test single row
        let result = string_to_grid("ABC", 3);
        assert_eq!(result, vec!["ABC".to_string()]);

        // Test single column
        let result = string_to_grid("ABC", 1);
        assert_eq!(
            result,
            vec!["A".to_string(), "B".to_string(), "C".to_string()]
        );

        // Test empty string
        let result = string_to_grid("", 1);
        assert_eq!(result, Vec::<String>::new());
    }

    /// Test cell_needs_across_clue function
    /// This determines which cells start across words
    #[test]
    fn test_cell_needs_across_clue() {
        let grid = vec![
            "---".to_string(), // Row 0: across clue at (0,0)
            "...".to_string(), // Row 1: all blocked, no across clues
            "--.".to_string(), // Row 2: across clue at (2,0), not at (2,2)
        ];

        // Test start of across word - needs two consecutive free squares
        assert!(cell_needs_across_clue(&grid, 0, 0)); // --, starts word
        assert!(!cell_needs_across_clue(&grid, 0, 1)); // continues word
        assert!(!cell_needs_across_clue(&grid, 0, 2)); // continues word

        // Test blocked squares
        assert!(!cell_needs_across_clue(&grid, 1, 0)); // blocked square
        assert!(!cell_needs_across_clue(&grid, 1, 1)); // blocked square
        assert!(!cell_needs_across_clue(&grid, 1, 2)); // blocked square

        // Test second row
        assert!(cell_needs_across_clue(&grid, 2, 0)); // --, starts word
        assert!(!cell_needs_across_clue(&grid, 2, 1)); // continues word
        assert!(!cell_needs_across_clue(&grid, 2, 2)); // blocked (isolated)
    }

    /// Test cell_needs_down_clue function
    /// This determines which cells start down words
    #[test]
    fn test_cell_needs_down_clue() {
        let grid = vec![
            "-.-".to_string(), // Row 0
            "-.-".to_string(), // Row 1
            "...".to_string(), // Row 2: all blocked
        ];

        // Test start of down word - needs two consecutive free squares vertically
        assert!(cell_needs_down_clue(&grid, 0, 0)); // -/-, starts down word
        assert!(!cell_needs_down_clue(&grid, 1, 0)); // continues down word
        assert!(!cell_needs_down_clue(&grid, 2, 0)); // blocked square

        // Test blocked column
        assert!(!cell_needs_down_clue(&grid, 0, 1)); // blocked square
        assert!(!cell_needs_down_clue(&grid, 1, 1)); // blocked square
        assert!(!cell_needs_down_clue(&grid, 2, 1)); // blocked square

        // Test column with down word
        assert!(cell_needs_down_clue(&grid, 0, 2)); // -/-, starts down word
        assert!(!cell_needs_down_clue(&grid, 1, 2)); // continues down word
        assert!(!cell_needs_down_clue(&grid, 2, 2)); // blocked square
    }

    /// Test across clue detection with edge cases
    /// Boundary conditions and single-letter words
    #[test]
    fn test_across_clue_edge_cases() {
        // Single column grid
        let grid = vec!["-".to_string(), "-".to_string(), ".".to_string()];
        assert!(!cell_needs_across_clue(&grid, 0, 0)); // Can't have across word with width 1
        assert!(!cell_needs_across_clue(&grid, 1, 0)); // Can't have across word with width 1
        assert!(!cell_needs_across_clue(&grid, 2, 0)); // Blocked square

        // Grid with gaps
        let grid = vec!["-.--.".to_string()];
        assert!(!cell_needs_across_clue(&grid, 0, 0)); // - (isolated, no next free square)
        assert!(!cell_needs_across_clue(&grid, 0, 1)); // blocked
        assert!(cell_needs_across_clue(&grid, 0, 2)); // -- (two free squares, starts word)
        assert!(!cell_needs_across_clue(&grid, 0, 3)); // continues word
        assert!(!cell_needs_across_clue(&grid, 0, 4)); // blocked
    }

    /// Test down clue detection with edge cases
    /// Boundary conditions and single-letter words
    #[test]
    fn test_down_clue_edge_cases() {
        // Single row grid
        let grid = vec!["---".to_string()];
        assert!(!cell_needs_down_clue(&grid, 0, 0)); // Can't have down word with height 1
        assert!(!cell_needs_down_clue(&grid, 0, 1)); // Can't have down word with height 1
        assert!(!cell_needs_down_clue(&grid, 0, 2)); // Can't have down word with height 1

        // Grid with gaps
        let grid = vec![
            "-".to_string(),
            ".".to_string(),
            "-".to_string(),
            "-".to_string(),
            "-".to_string(),
        ];
        assert!(!cell_needs_down_clue(&grid, 0, 0)); // - (isolated, no next free square)
        assert!(!cell_needs_down_clue(&grid, 1, 0)); // blocked
        assert!(cell_needs_down_clue(&grid, 2, 0)); // -- (two free squares, starts down word)
        assert!(!cell_needs_down_clue(&grid, 3, 0)); // continues down word
        assert!(!cell_needs_down_clue(&grid, 4, 0)); // continues down word
    }

    /// Test grid validation with mismatched dimensions
    /// Ensures proper error handling for corrupted data
    #[test]
    fn test_validate_grid_consistency_dimension_mismatch() {
        let solution = vec!["ABC".to_string(), "DEF".to_string()]; // 2 rows
        let blank = vec!["---".to_string()]; // 1 row

        let result = validate_grid_consistency(&solution, &blank, 3, 2);
        assert!(result.is_err());
        if let Err(PuzError::InvalidGrid { reason }) = result {
            assert!(reason.contains("height mismatch"));
        } else {
            panic!("Expected InvalidGrid error with height mismatch");
        }
    }

    /// Test grid validation with mismatched row widths
    /// Ensures proper error handling for malformed rows
    #[test]
    fn test_validate_grid_consistency_width_mismatch() {
        let solution = vec!["ABC".to_string(), "DE".to_string()]; // Second row too short
        let blank = vec!["---".to_string(), "--".to_string()];

        let result = validate_grid_consistency(&solution, &blank, 3, 2);
        assert!(result.is_err());
        if let Err(PuzError::InvalidGrid { reason }) = result {
            assert!(reason.contains("width mismatch"));
        } else {
            panic!("Expected InvalidGrid error with width mismatch");
        }
    }

    /// Test clue detection with real crossword patterns
    /// Simulates actual crossword grid layouts
    #[test]
    fn test_clue_detection_realistic_grid() {
        let grid = vec![
            "---".to_string(), // Row 0: all free squares
            "-.-".to_string(), // Row 1: free, blocked, free
            "---".to_string(), // Row 2: all free squares
        ];

        // Across clues: should be at start of each word
        assert!(cell_needs_across_clue(&grid, 0, 0)); // 3-letter word at row 0
        assert!(!cell_needs_across_clue(&grid, 0, 1)); // continues word
        assert!(!cell_needs_across_clue(&grid, 0, 2)); // continues word

        assert!(!cell_needs_across_clue(&grid, 1, 0)); // only 1 free square before block
        assert!(!cell_needs_across_clue(&grid, 1, 1)); // blocked square
        assert!(!cell_needs_across_clue(&grid, 1, 2)); // only 1 free square (isolated)

        assert!(cell_needs_across_clue(&grid, 2, 0)); // 3-letter word at row 2
        assert!(!cell_needs_across_clue(&grid, 2, 1)); // continues word
        assert!(!cell_needs_across_clue(&grid, 2, 2)); // continues word

        // Down clues: should be at start of each word
        assert!(cell_needs_down_clue(&grid, 0, 0)); // 3-letter word at col 0
        assert!(!cell_needs_down_clue(&grid, 1, 0)); // continues word
        assert!(!cell_needs_down_clue(&grid, 2, 0)); // continues word

        assert!(!cell_needs_down_clue(&grid, 0, 1)); // only free at top, blocked below
        assert!(!cell_needs_down_clue(&grid, 1, 1)); // blocked square
        assert!(!cell_needs_down_clue(&grid, 2, 1)); // isolated - no cell below

        assert!(cell_needs_down_clue(&grid, 0, 2)); // 3-letter word at col 2
        assert!(!cell_needs_down_clue(&grid, 1, 2)); // continues word
        assert!(!cell_needs_down_clue(&grid, 2, 2)); // continues word

        // Middle of row 1 is blocked
        assert!(!cell_needs_across_clue(&grid, 1, 1)); // blocked
        assert!(!cell_needs_down_clue(&grid, 1, 1)); // blocked
    }
}