Skip to content

Commit 03fcf1a

Browse files
authored
Add load_meta for faster metadata, improve DMI field handling, parsing optimizations (#23)
1 parent 1f0ee79 commit 03fcf1a

File tree

13 files changed

+632
-265
lines changed

13 files changed

+632
-265
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
22
Cargo.lock
3+
tests/resources/save_test.dmi

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ crc32fast = "1"
1616
deflate = "1"
1717
image = { version = "0.25", default-features = false, features = ["png"] }
1818
inflate = "0.4"
19+
png = "0.17.16"
1920
thiserror = "2"

src/chunk.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl RawGenericChunk {
2323
let chunk_length = chunk_bytes.len();
2424

2525
if chunk_length < 12 {
26-
return Err(error::DmiError::Generic(format!("Failed to load Chunk. Supplied reader contained size of {} bytes, lower than the required 12.", chunk_length)));
26+
return Err(error::DmiError::Generic(format!("Failed to load Chunk. Supplied reader contained size of {chunk_length} bytes, lower than the required 12.")));
2727
};
2828

2929
let data_length = [
@@ -46,8 +46,7 @@ impl RawGenericChunk {
4646
.all(|c| (b'A' <= *c && *c <= b'Z') || (b'a' <= *c && *c <= b'z'))
4747
{
4848
return Err(error::DmiError::Generic(format!(
49-
"Failed to load Chunk. Type contained unlawful characters: {:#?}",
50-
chunk_type
49+
"Failed to load Chunk. Type contained unlawful characters: {chunk_type:#?}",
5150
)));
5251
};
5352

@@ -61,9 +60,10 @@ impl RawGenericChunk {
6160
];
6261

6362
let recalculated_crc = crc::calculate_chunk_data_crc(chunk_type, &data);
64-
if u32::from_be_bytes(crc) != recalculated_crc {
63+
let crc_le = u32::from_be_bytes(crc);
64+
if crc_le != recalculated_crc {
6565
let chunk_name = String::from_utf8(chunk_type.to_vec())?;
66-
return Err(error::DmiError::Generic(format!("Failed to load Chunk of type {}. Supplied CRC invalid: {:#?}. Its value ({}) does not match the recalculated one ({}).", chunk_name, crc, u32::from_be_bytes(crc), recalculated_crc)));
66+
return Err(error::DmiError::Generic(format!("Failed to load Chunk of type {chunk_name}. Supplied CRC invalid: {crc:#?}. Its value ({crc_le}) does not match the recalculated one ({recalculated_crc}).")));
6767
}
6868

6969
Ok(RawGenericChunk {
@@ -74,40 +74,36 @@ impl RawGenericChunk {
7474
})
7575
}
7676

77-
pub fn save<W: Write>(&self, writter: &mut W) -> Result<usize, error::DmiError> {
78-
let bytes_written = writter.write(&self.data_length)?;
77+
pub fn save<W: Write>(&self, writer: &mut W) -> Result<usize, error::DmiError> {
78+
let bytes_written = writer.write(&self.data_length)?;
7979
let mut total_bytes_written = bytes_written;
8080
if bytes_written < self.data_length.len() {
8181
return Err(error::DmiError::Generic(format!(
82-
"Failed to save Chunk. Buffer unable to hold the data, only {} bytes written.",
83-
total_bytes_written
82+
"Failed to save Chunk. Buffer unable to hold the data, only {total_bytes_written} bytes written."
8483
)));
8584
};
8685

87-
let bytes_written = writter.write(&self.chunk_type)?;
86+
let bytes_written = writer.write(&self.chunk_type)?;
8887
total_bytes_written += bytes_written;
8988
if bytes_written < self.chunk_type.len() {
9089
return Err(error::DmiError::Generic(format!(
91-
"Failed to save Chunk. Buffer unable to hold the data, only {} bytes written.",
92-
total_bytes_written
90+
"Failed to save Chunk. Buffer unable to hold the data, only {total_bytes_written} bytes written."
9391
)));
9492
};
9593

96-
let bytes_written = writter.write(&self.data)?;
94+
let bytes_written = writer.write(&self.data)?;
9795
total_bytes_written += bytes_written;
9896
if bytes_written < self.data.len() {
9997
return Err(error::DmiError::Generic(format!(
100-
"Failed to save Chunk. Buffer unable to hold the data, only {} bytes written.",
101-
total_bytes_written
98+
"Failed to save Chunk. Buffer unable to hold the data, only {total_bytes_written} bytes written."
10299
)));
103100
};
104101

105-
let bytes_written = writter.write(&self.crc)?;
102+
let bytes_written = writer.write(&self.crc)?;
106103
total_bytes_written += bytes_written;
107104
if bytes_written < self.crc.len() {
108105
return Err(error::DmiError::Generic(format!(
109-
"Failed to save Chunk. Buffer unable to hold the data, only {} bytes written.",
110-
total_bytes_written
106+
"Failed to save Chunk. Buffer unable to hold the data, only {total_bytes_written} bytes written."
111107
)));
112108
};
113109

src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use png::DecodingError;
12
use std::io;
23
use thiserror::Error;
34

45
#[derive(Error, Debug)]
56
pub enum DmiError {
67
#[error("IO error: {0}")]
78
Io(#[from] io::Error),
9+
#[error("PNG decoding error: {0}")]
10+
PngDecoding(#[from] DecodingError),
811
#[error("Image-processing error: {0}")]
912
Image(#[from] image::error::ImageError),
1013
#[error("FromUtf8 error: {0}")]
@@ -19,6 +22,8 @@ pub enum DmiError {
1922
CrcMismatch { stated: u32, calculated: u32 },
2023
#[error("Dmi error: {0}")]
2124
Generic(String),
25+
#[error("Dmi block entry error: {0}")]
26+
BlockEntry(String),
2227
#[error("Dmi IconState error: {0}")]
2328
IconState(String),
2429
#[error("Encoding error: {0}")]

0 commit comments

Comments
 (0)