Skip to content

Commit

Permalink
refactor: pass mutable vec
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 7, 2024
1 parent 1398881 commit d0e6dbf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ fn main() -> io::Result<()> {
};

// Handle output stream (file or stdout)
let output: Box<dyn Write> = if let Some(output_path) = matches.get_one::<String>("output") {
let mut output: Box<dyn Write> = if let Some(output_path) = matches.get_one::<String>("output")
{
Box::new(File::create(output_path)?)
} else {
Box::new(io::stdout())
};

BencodeParser::new(input, output).parse()?;
BencodeParser::new(input, &mut output).parse()?;

Ok(())
}
13 changes: 4 additions & 9 deletions src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d0e6dbf

Please sign in to comment.