From d0e6dbf05f74d6181c418b38a9ec55514a188b63 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 7 Oct 2024 10:10:03 +0100 Subject: [PATCH] refactor: pass mutable vec --- src/main.rs | 5 +++-- src/parsers/mod.rs | 13 ++++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index eba76fd..f9c1504 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,13 +32,14 @@ fn main() -> io::Result<()> { }; // Handle output stream (file or stdout) - let output: Box = if let Some(output_path) = matches.get_one::("output") { + let mut output: Box = if let Some(output_path) = matches.get_one::("output") + { Box::new(File::create(output_path)?) } else { Box::new(io::stdout()) }; - BencodeParser::new(input, output).parse()?; + BencodeParser::new(input, &mut output).parse()?; Ok(()) } diff --git a/src/parsers/mod.rs b/src/parsers/mod.rs index f468d2d..751d7cf 100644 --- a/src/parsers/mod.rs +++ b/src/parsers/mod.rs @@ -192,18 +192,13 @@ mod tests { use super::BencodeParser; fn to_json(input_buffer: &[u8]) -> String { - let output_buffer = Vec::new(); + let mut output_buffer = Vec::new(); - let mut parser = BencodeParser::new(input_buffer, output_buffer); + let mut parser = BencodeParser::new(input_buffer, &mut output_buffer); - parser.parse().expect("bencoded to JSON conversion failed"); + parser.parse().expect("Bencode to JSON conversion failed"); - match parser.opt_captured_output() { - Some(captured_output) => captured_output, - None => panic!( - "capturing output is not enabled in parser, please enable it to run the tests" - ), - } + String::from_utf8_lossy(&output_buffer).to_string() } mod integers {