|
| 1 | +use puz::parse_puz; |
1 | 2 | use std::{
|
2 | 3 | fs::File,
|
3 |
| - io::{BufReader, ErrorKind, Read}, |
| 4 | + io::{ErrorKind, Write}, |
4 | 5 | };
|
5 |
| - |
6 |
| -use byteorder::{ByteOrder, LittleEndian}; |
7 |
| - |
8 |
| -enum PieceKind { |
9 |
| - None, |
10 |
| - String, |
11 |
| - Number, |
12 |
| - Natural, |
13 |
| -} |
14 |
| - |
15 | 6 | fn main() -> std::io::Result<()> {
|
16 |
| - let path = "example_data/Nov2493.puz"; |
| 7 | + let path = "examples/data/rebus.puz"; |
17 | 8 | let file = match File::open(&path) {
|
18 | 9 | Err(err) => match err.kind() {
|
19 | 10 | ErrorKind::NotFound => panic!("File not found at path: {}", &path),
|
20 | 11 | other_error => panic!("Problem opening the file: {:?}", other_error),
|
21 | 12 | },
|
22 | 13 | Ok(file) => file,
|
23 | 14 | };
|
24 |
| - let mut reader = BufReader::new(file); |
25 |
| - let header_offsets: Vec<(usize, Option<PieceKind>, &str)> = vec![ |
26 |
| - (0x02, None, "checksum"), |
27 |
| - (0x0C, None, "file_magic"), |
28 |
| - (0x02, None, "cib_checksum"), |
29 |
| - (0x04, None, "masked_low_checksum"), |
30 |
| - (0x04, None, "masked_high_checksum"), |
31 |
| - (0x04, Some(PieceKind::String), "version"), |
32 |
| - (0x02, None, "reserved_1c"), |
33 |
| - (0x02, None, "scrambled_checksum"), |
34 |
| - (0x0C, None, "reserved_20"), |
35 |
| - (0x01, Some(PieceKind::Natural), "width"), |
36 |
| - (0x01, Some(PieceKind::Natural), "height"), |
37 |
| - (0x02, Some(PieceKind::Number), "num_clues"), |
38 |
| - (0x02, None, "unknown_bitmask"), |
39 |
| - (0x02, None, "scrambled_tag"), |
40 |
| - ]; |
41 |
| - for (offset, conversion, label) in header_offsets.iter() { |
42 |
| - let mut buffer = vec![0; *offset]; |
43 |
| - reader.read_exact(&mut buffer)?; |
44 |
| - if let Some(c_type) = conversion { |
45 |
| - match c_type { |
46 |
| - PieceKind::Natural => print_puz_piece(label, &buffer), |
47 |
| - PieceKind::Number => print_puz_piece(label, &LittleEndian::read_u16(&buffer)), |
48 |
| - PieceKind::String => { |
49 |
| - if let Ok(s) = std::str::from_utf8(&buffer) { |
50 |
| - print_puz_piece(label, s); |
51 |
| - } else { |
52 |
| - println!("Puz::String listed in header but cannot convert to String. Check offsets or validity of file."); |
53 |
| - } |
54 |
| - } |
55 |
| - PieceKind::None => {} |
56 |
| - } |
57 |
| - } |
58 |
| - } |
59 | 15 |
|
60 |
| - Ok(()) |
61 |
| -} |
| 16 | + let puzzle = parse_puz(&file).expect("Error while parsing the file."); |
| 17 | + let file_path = "examples/output.json"; |
| 18 | + let mut file = File::create(file_path)?; |
| 19 | + file.write_all(puzzle.to_string().as_bytes())?; |
62 | 20 |
|
63 |
| -fn print_puz_piece<T: std::fmt::Debug>(label: &str, value: T) { |
64 |
| - println!("{}: {:?}", label, value); |
| 21 | + Ok(()) |
65 | 22 | }
|
0 commit comments