read_with_file/
read-with-file.rs

1use puz_parse::parse;
2use std::{fs::File, io::ErrorKind};
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let path = "examples/data/rebus.puz";
6    let file = match File::open(path) {
7        Err(err) => match err.kind() {
8            ErrorKind::NotFound => panic!("File not found at path: {}", &path),
9            other_error => panic!("Problem opening the file: {other_error:?}"),
10        },
11        Ok(file) => file,
12    };
13
14    let result = parse(file)?;
15    let puzzle = result.result;
16
17    // Print any warnings
18    for warning in &result.warnings {
19        println!("Warning: {warning}");
20    }
21
22    // Pretty print the puzzle info
23    println!("Title: {}", puzzle.info.title);
24    println!("Author: {}", puzzle.info.author);
25    println!("Size: {}x{}", puzzle.info.width, puzzle.info.height);
26    println!("Version: {}", puzzle.info.version);
27    println!("Scrambled: {}", puzzle.info.is_scrambled);
28    println!("Across clues: {}", puzzle.clues.across.len());
29    println!("Down clues: {}", puzzle.clues.down.len());
30
31    // Print some sample clues
32    println!("\nSample across clues:");
33    for (num, clue) in puzzle.clues.across.iter().take(3) {
34        println!("  {num}: {clue}");
35    }
36
37    println!("\nSample down clues:");
38    for (num, clue) in puzzle.clues.down.iter().take(3) {
39        println!("  {num}: {clue}");
40    }
41
42    if let Some(rebus) = &puzzle.extensions.rebus {
43        println!("\nRebus entries found: {}", rebus.table.len());
44        for (key, value) in &rebus.table {
45            println!("  {key}: {value}");
46        }
47    }
48
49    println!("\nPuzzle parsed successfully!");
50
51    Ok(())
52}