Skip to content

Commit d59a94d

Browse files
authored
Merge pull request #84 from sile/fix-issue-82
Fix `invalid_data_error!` macro to properly format error messages with arguments
2 parents cad002d + ddbcb3c commit d59a94d

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@ extern crate alloc;
99

1010
macro_rules! invalid_data_error {
1111
($fmt:expr) => {
12-
invalid_data_error!($fmt, "")
12+
::core2::io::Error::new(::core2::io::ErrorKind::InvalidData, $fmt)
1313
};
1414
($fmt:expr, $($arg:tt)*) => {
15-
::core2::io::Error::new(::core2::io::ErrorKind::InvalidData, $fmt)
15+
{
16+
#[cfg(feature = "std")]
17+
{
18+
::core2::io::Error::new(
19+
::core2::io::ErrorKind::InvalidData,
20+
::alloc::format!($fmt, $($arg)*),
21+
)
22+
}
23+
#[cfg(not(feature = "std"))]
24+
{
25+
::core2::io::Error::new(::core2::io::ErrorKind::InvalidData, $fmt)
26+
}
27+
}
1628
};
1729
}
1830

src/zlib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,4 +932,13 @@ mod tests {
932932
];
933933
assert_eq!(buf, decoded_data);
934934
}
935+
936+
#[test]
937+
#[cfg(feature = "std")]
938+
fn issue_82() {
939+
let encoded_data = [0x00, 0x00];
940+
let error = Header::read_from(&encoded_data[..]).unwrap_err();
941+
assert_eq!(error.kind(), io::ErrorKind::InvalidData);
942+
assert!(error.to_string().contains("method=0"));
943+
}
935944
}

0 commit comments

Comments
 (0)