From 3cae425a59fc4fb2ca2a53b2f5d0ba8f6380212c Mon Sep 17 00:00:00 2001 From: Quentin Santos Date: Wed, 19 Jun 2024 08:17:28 +0200 Subject: [PATCH] Bubble up I/O errors in decode_stream --- src/decode.rs | 14 ++++++++++---- src/main.rs | 2 +- tests/tests.rs | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/decode.rs b/src/decode.rs index e00c630..387f03d 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -212,12 +212,16 @@ pub fn decode_string(input: &[u8], char_decode: fn(u8) -> char) -> String { /// ripmors::decode_stream(&mut stdin, &mut stdout, ripmors::to_standard); /// } /// ``` -pub fn decode_stream(input: &mut impl Read, output: &mut impl Write, char_decode: fn(u8) -> char) { +pub fn decode_stream( + input: &mut impl Read, + output: &mut impl Write, + char_decode: fn(u8) -> char, +) -> Result<(), std::io::Error> { let mut input_buf = vec![0u8; 1 << 15]; let mut bytes_available = 0; let mut output_buf = Vec::with_capacity(1 << 15); loop { - let bytes_read = input.read(&mut input_buf[bytes_available..]).unwrap(); + let bytes_read = input.read(&mut input_buf[bytes_available..])?; if bytes_read == 0 { break; } @@ -228,7 +232,7 @@ pub fn decode_stream(input: &mut impl Read, output: &mut impl Write, char_decode // flush buffer if !output_buf.is_empty() { let decoded: String = output_buf.iter().collect(); - output.write_all(decoded.as_bytes()).unwrap(); + output.write_all(decoded.as_bytes())?; output_buf.clear(); } @@ -240,9 +244,11 @@ pub fn decode_stream(input: &mut impl Read, output: &mut impl Write, char_decode decode_buffer_end(&input_buf[..bytes_available], char_decode, &mut output_buf); if !output_buf.is_empty() { let decoded: String = output_buf.iter().collect(); - output.write_all(decoded.as_bytes()).unwrap(); + output.write_all(decoded.as_bytes())?; } } + + Ok(()) } #[test] diff --git a/src/main.rs b/src/main.rs index 4f11e7e..6eafa6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,7 +48,7 @@ fn main() { DecodeVariant::Hebrew => to_hebrew, DecodeVariant::Arabic => to_arabic, }; - decode_stream(&mut stdin, &mut stdout, char_decode); + decode_stream(&mut stdin, &mut stdout, char_decode).unwrap(); } else if args.encode == Some(EncodeVariant::Ascii) { encode_stream_ascii(&mut stdin, &mut stdout).unwrap(); } else { diff --git a/tests/tests.rs b/tests/tests.rs index 15efa42..3db763f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -46,6 +46,6 @@ fn test_encode_stream_unicode() { fn test_decode_stream() { let mut f = std::fs::File::open("2-encoded.txt").unwrap(); let mut writer = BufWriter::new(Vec::new()); - decode_stream(&mut f, &mut writer, to_standard); + decode_stream(&mut f, &mut writer, to_standard).unwrap(); compare_output_to_oracle(writer, "3-decoded.txt"); }