Skip to content

Commit

Permalink
Print nicer transform errors
Browse files Browse the repository at this point in the history
Create and use a Display for Error, mostly since clippy started
complaining about dead code in Error -- the derived Debug instance does
not count as 'use' even though I was using the Debug instance before

Since I was tweaking the error printing anyway, also made the printed
error messages for Error slightly nicer
  • Loading branch information
rayrobdod committed Apr 12, 2024
1 parent de5fe29 commit 09f8635
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]
* Adjusted some error messages to be slightly more informative
and print in natural-language instead of the programatic debug print style

## [v2023.6.10] 2023-06-10
Refactors, process improvements, dependency updating
Expand Down
39 changes: 38 additions & 1 deletion src/inflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main() {
}
},
Result::Err(x) => {
eprintln!("Could not transform: {:?}", x);
eprintln!("Could not transform: {}", x);
::std::process::exit(1);
},
}
Expand All @@ -82,6 +82,43 @@ impl From<zlib::InflateError> for Error {
}
}

impl ::std::fmt::Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match self {
Error::Zlib(zlib::InflateError::UnexpectedEof) => {
write!(f, "Unexpected End of File")
},
Error::CannotCopySafely => {
write!(f, "Found non-safe-to-copy chunk")
},
Error::UnsupportedCompressionMethod => {
write!(f, "Unsupported PNG Compression Method")
},
Error::Zlib(zlib::InflateError::ChecksumMismatchHeader) => {
write!(f, "ZLib Header Checksum Mismatch")
},
Error::Zlib(zlib::InflateError::UnknownCompressionMethod(method)) => {
write!(f, "Unsupported Zlib Compression Method: {method:X}")
},
Error::Zlib(zlib::InflateError::ChecksumMismatch { given, calculated }) => {
write!(
f,
"ZLib Checksum Mismatch: given `{given:x}`, calculated `{calculated:x}`"
)
},
Error::Zlib(zlib::InflateError::HasPresetDictionary) => {
write!(f, "ZLib Segment has preset dictionary")
},
Error::Zlib(zlib::InflateError::DeflateNonCompressedLengthInvalid) => {
write!(f, "Malformed deflate block: LEN and NLEN mismatch")
},
Error::Zlib(zlib::InflateError::DeflateInvalidBtype) => {
write!(f, "Malformed deflate block: invalid BTYPE")
},
}
}
}

fn deflate_chunks(indata: png::Chunk, ignore_unsafe_to_copy: bool) -> Result<png::Chunk, Error> {
match indata.typ.as_ref() {
// Not compressed, but contains data that can be validated
Expand Down
22 changes: 14 additions & 8 deletions src/zlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ impl Header {
_ => panic!(""),
};

if method != u4::_8 || dict {
Err(InflateError::UnsupportedHeader)
if method != u4::_8 {
Err(InflateError::UnknownCompressionMethod(method))
} else if dict {
Err(InflateError::HasPresetDictionary)
} else {
Ok(Header::new(info, level))
}
Expand All @@ -96,8 +98,9 @@ impl Header {
pub enum InflateError {
UnexpectedEof,
ChecksumMismatchHeader,
UnsupportedHeader,
ChecksumMismatch,
UnknownCompressionMethod(u4),
ChecksumMismatch { given: u32, calculated: u32 },
HasPresetDictionary,

DeflateNonCompressedLengthInvalid,
DeflateInvalidBtype,
Expand Down Expand Up @@ -139,8 +142,10 @@ pub fn inflate(r: &[u8]) -> Result<Vec<u8>, InflateError> {
let calculated_chksum = adler32(&result);

if given_chksum != calculated_chksum {
eprintln!("{:x} {:x}", given_chksum, calculated_chksum);
Err(InflateError::ChecksumMismatch)
Err(InflateError::ChecksumMismatch {
given: given_chksum,
calculated: calculated_chksum,
})
} else {
Ok(result)
}
Expand Down Expand Up @@ -227,14 +232,15 @@ mod tests {
}
#[test]
fn has_dict_fails() {
let exp: Result<Header, InflateError> = Err(InflateError::UnsupportedHeader);
let exp: Result<Header, InflateError> = Err(InflateError::HasPresetDictionary);
let dut: u16 = 0x68A0;
let res = Header::read(dut);
assert!(exp == res, "{:?} != {:?}", exp, res);
}
#[test]
fn method_not_deflate_fails() {
let exp: Result<Header, InflateError> = Err(InflateError::UnsupportedHeader);
let exp: Result<Header, InflateError> =
Err(InflateError::UnknownCompressionMethod(u4::_5));
let dut: u16 = 0x6599;
let res = Header::read(dut);
assert!(exp == res, "{:?} != {:?}", exp, res);
Expand Down
6 changes: 6 additions & 0 deletions src/zlib/u4mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ impl ::std::ops::Sub for u4 {
}
}

impl ::std::fmt::UpperHex for u4 {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:X}", u8::from(*self))
}
}

/// An iterator over u4s that begins at 0, and ends (exclusive) of a given value
pub struct ZeroToRangeIter {
current: u4,
Expand Down

0 comments on commit 09f8635

Please sign in to comment.