pub fn parse<R: Read>(reader: R) -> Result<ParseResult<Puzzle>, PuzError>Expand description
Parse a .puz file from any source that implements Read.
This is the core parsing function that provides full control over error handling
and warnings. Use parse_file for a simpler API when parsing from files.
§Arguments
reader- Any type that implementsRead, such as aFileor&[u8]
§Returns
Returns a Result<ParseResult<Puzzle>, PuzError> containing the parsed puzzle data
along with any warnings, or an error if parsing fails.
§Example
use std::fs::File;
use puz_parse::parse;
let file = File::open("puzzle.puz")?;
let result = parse(file)?;
let puzzle = result.result;
for warning in &result.warnings {
eprintln!("Warning: {}", warning);
}Examples found in repository?
examples/read-with-file.rs (line 14)
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}More examples
examples/basic_usage.rs (line 16)
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}