parse_bytes

Function parse_bytes 

Source
pub fn parse_bytes(data: &[u8]) -> Result<Puzzle, PuzError>
Expand description

Parse a .puz file from a byte slice.

Convenience function for parsing puzzle data already in memory.

§Arguments

  • data - Byte slice containing .puz file data

§Returns

Returns the parsed Puzzle or an error if parsing fails.

§Example

use puz_parse::parse_bytes;

let data = std::fs::read("puzzle.puz")?;
let puzzle = parse_bytes(&data)?;
Examples found in repository?
examples/basic_usage.rs (line 80)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    // Example 1: Simple file parsing
6    println!("=== Simple File Parsing ===");
7    let puzzle = parse_file("examples/data/standard1.puz")?;
8    println!("Title: {}", puzzle.info.title);
9    println!("Author: {}", puzzle.info.author);
10    println!("Size: {}x{}", puzzle.info.width, puzzle.info.height);
11    println!();
12
13    // Example 2: Parsing with error handling and warnings
14    println!("=== Advanced Parsing with Warnings ===");
15    let file = File::open("examples/data/rebus.puz")?;
16    let result = parse(file)?;
17    let puzzle = result.result;
18
19    println!("Title: {}", puzzle.info.title);
20
21    // Handle any warnings
22    for warning in &result.warnings {
23        println!("Warning: {warning}");
24    }
25    println!();
26
27    // Example 3: Working with clues
28    println!("=== Working with Clues ===");
29    println!("Across clues:");
30    for (num, clue) in puzzle.clues.across.iter().take(5) {
31        println!("  {num}: {clue}");
32    }
33
34    println!("Down clues:");
35    for (num, clue) in puzzle.clues.down.iter().take(5) {
36        println!("  {num}: {clue}");
37    }
38    println!();
39
40    // Example 4: Working with the grid
41    println!("=== Working with the Grid ===");
42    println!("First few rows of solution:");
43    for (i, row) in puzzle.grid.solution.iter().take(3).enumerate() {
44        println!("  Row {}: {}", i + 1, row);
45    }
46
47    println!("First few rows of blank grid:");
48    for (i, row) in puzzle.grid.blank.iter().take(3).enumerate() {
49        println!("  Row {}: {}", i + 1, row);
50    }
51    println!();
52
53    // Example 5: Working with extensions (rebus, circles, etc.)
54    println!("=== Working with Extensions ===");
55    if let Some(rebus) = &puzzle.extensions.rebus {
56        println!("Rebus found! Entries:");
57        for (key, value) in &rebus.table {
58            println!("  {key}: {value}");
59        }
60    } else {
61        println!("No rebus in this puzzle");
62    }
63
64    if let Some(_circles) = &puzzle.extensions.circles {
65        println!("This puzzle has circled squares");
66    } else {
67        println!("No circled squares in this puzzle");
68    }
69
70    if let Some(_given) = &puzzle.extensions.given {
71        println!("This puzzle has given squares");
72    } else {
73        println!("No given squares in this puzzle");
74    }
75    println!();
76
77    // Example 6: Parsing from bytes
78    println!("=== Parsing from Bytes ===");
79    let data = std::fs::read("examples/data/standard1.puz")?;
80    let puzzle_from_bytes = parse_bytes(&data)?;
81    println!("Parsed from bytes: {}", puzzle_from_bytes.info.title);
82
83    Ok(())
84}