diff --git a/src/io/char_writer.rs b/src/io/char_writer.rs index a60ba1e..9ccd391 100644 --- a/src/io/char_writer.rs +++ b/src/io/char_writer.rs @@ -1,5 +1,7 @@ use core::str; -use std::fmt::{self, Write}; +use std::{fmt::Write, io}; + +use super::writer::Writer; /// A writer that writes chars to an output. /// @@ -22,14 +24,13 @@ impl CharWriter { writer, } } +} + +impl Writer for CharWriter { + fn write_byte(&mut self, byte: u8) -> io::Result<()> { + let c = byte as char; - /// It writes one byte to the output (stdout or file). - /// - /// # Errors - /// - /// Will return an error if it can't write the byte to the output. - pub fn write_char(&mut self, c: char) -> fmt::Result { - self.writer.write_char(c)?; + self.writer.write_char(c).expect("error writing str"); self.output_char_counter += 1; @@ -40,13 +41,8 @@ impl CharWriter { Ok(()) } - /// It writes a string to the output (stdout or file). - /// - /// # Errors - /// - /// Will return an error if it can't write the string (as bytes) to the output. - pub fn write_str(&mut self, value: &str) -> fmt::Result { - self.writer.write_str(value)?; + fn write_str(&mut self, value: &str) -> io::Result<()> { + self.writer.write_str(value).expect("error writing str"); self.output_char_counter += value.len() as u64; @@ -57,8 +53,14 @@ impl CharWriter { Ok(()) } - /// It prints the captured output is enabled. - pub fn print_captured_output(&self) { + fn get_captured_output(&mut self) -> Option { + match &self.opt_captured_output { + Some(output) => Some(output.to_string()), + None => todo!(), + } + } + + fn print_captured_output(&self) { if let Some(output) = &self.opt_captured_output { println!("output: {output}"); } diff --git a/src/parsers/mod.rs b/src/parsers/mod.rs index b53e164..a883fe6 100644 --- a/src/parsers/mod.rs +++ b/src/parsers/mod.rs @@ -2,11 +2,16 @@ pub mod integer; pub mod stack; pub mod string; -use std::io::{self, Read, Write}; +use std::{ + fmt::Write as FmtWrite, + io::{self, Read, Write as IoWrite}, +}; use stack::{Stack, State}; -use crate::io::{byte_reader::ByteReader, byte_writer::ByteWriter, writer::Writer}; +use crate::io::{ + byte_reader::ByteReader, byte_writer::ByteWriter, char_writer::CharWriter, writer::Writer, +}; pub struct BencodeParser { pub debug: bool, @@ -46,11 +51,28 @@ impl BencodeParser { /// /// Will panic if receives a byte that isn't a valid begin or end of a /// bencoded type: integer, string, list or dictionary. - pub fn write_bytes(&mut self, writer: W) -> io::Result<()> { + pub fn write_bytes(&mut self, writer: W) -> io::Result<()> { let mut writer = ByteWriter::new(writer); self.parse(&mut writer) } + /// It parses a bencoded value read from input and writes the corresponding + /// JSON value to the output. + /// + /// # Errors + /// + /// Will return an error if it can't read from the input or write to the + /// output. + /// + /// # Panics + /// + /// Will panic if receives a byte that isn't a valid begin or end of a + /// bencoded type: integer, string, list or dictionary. + pub fn write_str(&mut self, writer: W) -> io::Result<()> { + let mut writer = CharWriter::new(writer); + self.parse(&mut writer) + } + /// It parses a bencoded value read from input and writes the corresponding /// JSON value to the output. /// @@ -203,16 +225,42 @@ mod tests { use super::BencodeParser; + #[test] + fn it_should_allow_writing_to_a_byte_vector() { + let mut output = Vec::new(); + + let mut parser = BencodeParser::new(&b"i0e"[..]); + + parser + .write_bytes(&mut output) + .expect("Bencode to JSON conversion failed"); + + assert_eq!(output, vec!(48)); + } + + #[test] + fn it_should_allow_writing_to_a_string() { + let mut output = String::new(); + + let mut parser = BencodeParser::new(&b"i0e"[..]); + + parser + .write_str(&mut output) + .expect("Bencode to JSON conversion failed"); + + assert_eq!(output, "0".to_string()); + } + fn to_json(input_buffer: &[u8]) -> String { - let mut output_buffer = Vec::new(); + let mut output = String::new(); let mut parser = BencodeParser::new(input_buffer); parser - .write_bytes(&mut output_buffer) + .write_str(&mut output) .expect("Bencode to JSON conversion failed"); - String::from_utf8_lossy(&output_buffer).to_string() + output } mod integers {