Skip to content

Commit 25a67f9

Browse files
committed
complete working mvp
1 parent 2d732fc commit 25a67f9

14 files changed

+493
-328
lines changed

.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# will have compiled files and executables
33
/target/
44

5-
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
6-
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
7-
Cargo.lock
5+
# wasm default directory
6+
/pkg/
7+
*.json
88

99
# These are backup files generated by rustfmt
1010
**/*.rs.bk

Cargo.lock

+247
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ version = "0.1.0"
44
edition = "2021"
55

66
[lib]
7-
crate-type = ["cdylib"]
7+
crate-type = ["cdylib", "rlib"]
88

99
[dependencies]
10-
nom = "7.1.3"
1110
wasm-bindgen-futures = "0.4.37"
1211
wasm-bindgen = "0.2.87"
13-
gloo-file = "0.3.0"
14-
gloo-console = "0.3.0"
12+
gloo-file = {version = "0.3.0", features = ['futures'] }
1513
js-sys = "0.3.64"
1614
web-sys = { version = "0.3.64", features = ['File', 'FileReader', 'Blob', 'console'] }
1715
byteorder = "1.4.3"
16+
serde_json = "1.0.107"

examples/data/circled.puz

2.45 KB
Binary file not shown.

examples/data/circled2.puz

2.59 KB
Binary file not shown.

examples/data/circled3.puz

2.71 KB
Binary file not shown.

examples/data/rebus.puz

2.68 KB
Binary file not shown.

examples/data/rebus1.puz

2.7 KB
Binary file not shown.
File renamed without changes.

examples/data/standard2.puz

2.44 KB
Binary file not shown.

examples/read-with-file.rs

+8-51
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,22 @@
1+
use puz::parse_puz;
12
use std::{
23
fs::File,
3-
io::{BufReader, ErrorKind, Read},
4+
io::{ErrorKind, Write},
45
};
5-
6-
use byteorder::{ByteOrder, LittleEndian};
7-
8-
enum PieceKind {
9-
None,
10-
String,
11-
Number,
12-
Natural,
13-
}
14-
156
fn main() -> std::io::Result<()> {
16-
let path = "example_data/Nov2493.puz";
7+
let path = "examples/data/rebus.puz";
178
let file = match File::open(&path) {
189
Err(err) => match err.kind() {
1910
ErrorKind::NotFound => panic!("File not found at path: {}", &path),
2011
other_error => panic!("Problem opening the file: {:?}", other_error),
2112
},
2213
Ok(file) => file,
2314
};
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-
}
5915

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())?;
6220

63-
fn print_puz_piece<T: std::fmt::Debug>(label: &str, value: T) {
64-
println!("{}: {:?}", label, value);
21+
Ok(())
6522
}

0 commit comments

Comments
 (0)