diff --git a/src/decode/lzma.rs b/src/decode/lzma.rs index fbe164bb..bc3c2c00 100644 --- a/src/decode/lzma.rs +++ b/src/decode/lzma.rs @@ -24,12 +24,9 @@ impl LZMAParams { R: io::BufRead, { // Properties - let props = input.read_u8().or_else(|e| { - Err(error::Error::LZMAError(format!( - "LZMA header too short: {}", - e - ))) - })?; + let props = input + .read_u8() + .map_err(|e| error::Error::LZMAError(format!("LZMA header too short: {}", e)))?; let mut pb = props as u32; if pb >= 225 { @@ -47,12 +44,9 @@ impl LZMAParams { lzma_info!("Properties {{ lc: {}, lp: {}, pb: {} }}", lc, lp, pb); // Dictionary - let dict_size_provided = input.read_u32::().or_else(|e| { - Err(error::Error::LZMAError(format!( - "LZMA header too short: {}", - e - ))) - })?; + let dict_size_provided = input + .read_u32::() + .map_err(|e| error::Error::LZMAError(format!("LZMA header too short: {}", e)))?; let dict_size = if dict_size_provided < 0x1000 { 0x1000 } else { @@ -64,11 +58,8 @@ impl LZMAParams { // Unpacked size let unpacked_size: Option = match options.unpacked_size { UnpackedSize::ReadFromHeader => { - let unpacked_size_provided = input.read_u64::().or_else(|e| { - Err(error::Error::LZMAError(format!( - "LZMA header too short: {}", - e - ))) + let unpacked_size_provided = input.read_u64::().map_err(|e| { + error::Error::LZMAError(format!("LZMA header too short: {}", e)) })?; let marker_mandatory: bool = unpacked_size_provided == 0xFFFF_FFFF_FFFF_FFFF; if marker_mandatory { diff --git a/src/decode/lzma2.rs b/src/decode/lzma2.rs index 725c93b7..7f399242 100644 --- a/src/decode/lzma2.rs +++ b/src/decode/lzma2.rs @@ -16,12 +16,9 @@ where let mut decoder = lzma::new_accum(accum, 0, 0, 0, None); loop { - let status = input.read_u8().or_else(|e| { - Err(error::Error::LZMAError(format!( - "LZMA2 expected new status: {}", - e - ))) - })?; + let status = input + .read_u8() + .map_err(|e| error::Error::LZMAError(format!("LZMA2 expected new status: {}", e)))?; lzma_info!("LZMA2 status: {}", status); @@ -81,20 +78,14 @@ where _ => unreachable!(), } - let unpacked_size = input.read_u16::().or_else(|e| { - Err(error::Error::LZMAError(format!( - "LZMA2 expected unpacked size: {}", - e - ))) - })?; + let unpacked_size = input + .read_u16::() + .map_err(|e| error::Error::LZMAError(format!("LZMA2 expected unpacked size: {}", e)))?; let unpacked_size = ((((status & 0x1F) as u64) << 16) | (unpacked_size as u64)) + 1; - let packed_size = input.read_u16::().or_else(|e| { - Err(error::Error::LZMAError(format!( - "LZMA2 expected packed size: {}", - e - ))) - })?; + let packed_size = input + .read_u16::() + .map_err(|e| error::Error::LZMAError(format!("LZMA2 expected packed size: {}", e)))?; let packed_size = (packed_size as u64) + 1; lzma_info!( @@ -116,11 +107,8 @@ where let mut pb: u32; if reset_props { - let props = input.read_u8().or_else(|e| { - Err(error::Error::LZMAError(format!( - "LZMA2 expected new properties: {}", - e - ))) + let props = input.read_u8().map_err(|e| { + error::Error::LZMAError(format!("LZMA2 expected new properties: {}", e)) })?; pb = props as u32; @@ -156,12 +144,8 @@ where decoder.set_unpacked_size(Some(unpacked_size)); let mut taken = input.take(packed_size); - let mut rangecoder = rangecoder::RangeDecoder::new(&mut taken).or_else(|e| { - Err(error::Error::LZMAError(format!( - "LZMA input too short: {}", - e - ))) - })?; + let mut rangecoder = rangecoder::RangeDecoder::new(&mut taken) + .map_err(|e| error::Error::LZMAError(format!("LZMA input too short: {}", e)))?; decoder.process(&mut rangecoder) } @@ -174,12 +158,9 @@ where R: io::BufRead, W: io::Write, { - let unpacked_size = input.read_u16::().or_else(|e| { - Err(error::Error::LZMAError(format!( - "LZMA2 expected unpacked size: {}", - e - ))) - })?; + let unpacked_size = input + .read_u16::() + .map_err(|e| error::Error::LZMAError(format!("LZMA2 expected unpacked size: {}", e)))?; let unpacked_size = (unpacked_size as usize) + 1; lzma_info!( @@ -193,11 +174,11 @@ where } let mut buf = vec![0; unpacked_size]; - input.read_exact(buf.as_mut_slice()).or_else(|e| { - Err(error::Error::LZMAError(format!( + input.read_exact(buf.as_mut_slice()).map_err(|e| { + error::Error::LZMAError(format!( "LZMA2 expected {} uncompressed bytes: {}", unpacked_size, e - ))) + )) })?; decoder.output.append_bytes(buf.as_slice()); diff --git a/src/decode/xz.rs b/src/decode/xz.rs index 8f9b0d54..dd32af36 100644 --- a/src/decode/xz.rs +++ b/src/decode/xz.rs @@ -415,11 +415,11 @@ where } let mut buf = vec![0; size_of_properties as usize]; - input.read_exact(buf.as_mut_slice()).or_else(|e| { - Err(error::Error::XZError(format!( + input.read_exact(buf.as_mut_slice()).map_err(|e| { + error::Error::XZError(format!( "Could not read filter properties of size {}: {}", size_of_properties, e - ))) + )) })?; lzma_info!("XZ filter properties: {:?}", buf); diff --git a/src/lib.rs b/src/lib.rs index b34e65ff..fca86e5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,12 +46,8 @@ pub fn lzma_decompress_with_options( decode::lzma::new_circular(output, params)? }; - let mut rangecoder = decode::rangecoder::RangeDecoder::new(input).or_else(|e| { - Err(error::Error::LZMAError(format!( - "LZMA stream too short: {}", - e - ))) - })?; + let mut rangecoder = decode::rangecoder::RangeDecoder::new(input) + .map_err(|e| error::Error::LZMAError(format!("LZMA stream too short: {}", e)))?; decoder.process(&mut rangecoder)?; decoder.output.finish()?; Ok(())