Skip to content

Commit

Permalink
feat: implemented writer to string
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 7, 2024
1 parent e12c584 commit b658c26
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
36 changes: 19 additions & 17 deletions src/io/char_writer.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand All @@ -22,14 +24,13 @@ impl<W: Write> CharWriter<W> {
writer,
}
}
}

impl<W: Write> Writer for CharWriter<W> {
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;

Expand All @@ -40,13 +41,8 @@ impl<W: Write> CharWriter<W> {
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;

Expand All @@ -57,8 +53,14 @@ impl<W: Write> CharWriter<W> {
Ok(())
}

/// It prints the captured output is enabled.
pub fn print_captured_output(&self) {
fn get_captured_output(&mut self) -> Option<String> {
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}");
}
Expand Down
60 changes: 54 additions & 6 deletions src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: Read> {
pub debug: bool,
Expand Down Expand Up @@ -46,11 +51,28 @@ impl<R: Read> BencodeParser<R> {
///
/// 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<W: Write>(&mut self, writer: W) -> io::Result<()> {
pub fn write_bytes<W: IoWrite>(&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<W: FmtWrite>(&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.
///
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b658c26

Please sign in to comment.