mirror of
https://github.com/ducaale/xh.git
synced 2025-05-05 15:32:50 +00:00
Use a proper error type for binary data detection
We used to use `ErrorKind::InvalidData` to communicate binary data that should not be shown in the terminal but that one can actually happen in other cases as well. brotli decoding uses that ErrorKind, and we now use it for all decompressors. So an invalid brotli response would under certain circumstances render as "NOTE: binary data not shown in terminal". We can use our own error type to track this properly.
This commit is contained in:
parent
674be17a36
commit
6bc87b3974
@ -44,6 +44,17 @@ struct BinaryGuard<'a, T: Read> {
|
||||
checked: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FoundBinaryData;
|
||||
|
||||
impl std::fmt::Display for FoundBinaryData {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str("binary data not shown in terminal")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for FoundBinaryData {}
|
||||
|
||||
impl<'a, T: Read> BinaryGuard<'a, T> {
|
||||
fn new(reader: &'a mut T, checked: bool) -> Self {
|
||||
Self {
|
||||
@ -78,10 +89,7 @@ impl<'a, T: Read> BinaryGuard<'a, T> {
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
if self.checked && buf.contains(&b'\0') {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"Found binary data",
|
||||
));
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, FoundBinaryData));
|
||||
} else if buf.is_empty() {
|
||||
if self.buffer.is_empty() {
|
||||
return Ok(None);
|
||||
@ -454,7 +462,7 @@ impl Printer {
|
||||
Ok(_) => {
|
||||
self.buffer.print("\n")?;
|
||||
}
|
||||
Err(err) if err.kind() == io::ErrorKind::InvalidData => {
|
||||
Err(err) if err.get_ref().is_some_and(|err| err.is::<FoundBinaryData>()) => {
|
||||
self.buffer.print(BINARY_SUPPRESSOR)?;
|
||||
}
|
||||
Err(err) => return Err(err.into()),
|
||||
|
Loading…
x
Reference in New Issue
Block a user