From e44764a59421e434cead615d867157cdcc881887 Mon Sep 17 00:00:00 2001 From: Quentin Santos Date: Wed, 19 Jun 2024 08:17:13 +0200 Subject: [PATCH] Bubble up I/O errors in encode_stream_ascii --- benches/bench.rs | 2 +- src/encode_ascii.rs | 12 ++++++++---- src/main.rs | 2 +- tests/tests.rs | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/benches/bench.rs b/benches/bench.rs index 45e27b2..cafa757 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -19,7 +19,7 @@ fn ascii_benchmark(c: &mut Criterion) { group.bench_function("stream", |b| { b.iter(|| { f.rewind().unwrap(); - encode_stream_ascii(&mut f, &mut devnull); + encode_stream_ascii(&mut f, &mut devnull).unwrap(); }) }); diff --git a/src/encode_ascii.rs b/src/encode_ascii.rs index 71ad512..803a64a 100644 --- a/src/encode_ascii.rs +++ b/src/encode_ascii.rs @@ -87,11 +87,14 @@ pub fn encode_string_ascii(input: &[u8]) -> String { /// ripmors::encode_stream_ascii(&mut stdin, &mut stdout); /// } /// ``` -pub fn encode_stream_ascii(input: &mut impl Read, output: &mut impl Write) { +pub fn encode_stream_ascii( + input: &mut impl Read, + output: &mut impl Write, +) -> Result<(), std::io::Error> { let mut input_buf = vec![0u8; 1 << 15]; let mut output_buf = Vec::new(); loop { - let bytes_read = input.read(&mut input_buf).unwrap(); + let bytes_read = input.read(&mut input_buf)?; if bytes_read == 0 { break; } @@ -99,14 +102,15 @@ pub fn encode_stream_ascii(input: &mut impl Read, output: &mut impl Write) { if output_buf.is_empty() { } else if output_buf.last() == Some(&b' ') { output_buf.pop(); - output.write_all(&output_buf).unwrap(); + output.write_all(&output_buf)?; output_buf.clear(); output_buf.push(b' '); } else { - output.write_all(&output_buf).unwrap(); + output.write_all(&output_buf)?; output_buf.clear(); } } + Ok(()) } #[test] diff --git a/src/main.rs b/src/main.rs index de068b8..4f11e7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,7 +50,7 @@ fn main() { }; decode_stream(&mut stdin, &mut stdout, char_decode); } else if args.encode == Some(EncodeVariant::Ascii) { - encode_stream_ascii(&mut stdin, &mut stdout); + encode_stream_ascii(&mut stdin, &mut stdout).unwrap(); } else { encode_stream(&mut stdin, &mut stdout); } diff --git a/tests/tests.rs b/tests/tests.rs index aab6e48..15efa42 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -28,7 +28,7 @@ fn compare_output_to_oracle(writer: BufWriter>, expected_filename: &str) fn test_encode_stream_ascii() { let mut f = std::fs::File::open("1-original.txt").unwrap(); let mut writer = BufWriter::new(Vec::new()); - encode_stream_ascii(&mut f, &mut writer); + encode_stream_ascii(&mut f, &mut writer).unwrap(); compare_output_to_oracle(writer, "2-encoded.txt"); }