Skip to content

Commit

Permalink
refactor: rename
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 7, 2024
1 parent b658c26 commit 4830f24
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/io/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod byte_reader;
pub mod byte_writer;
pub mod char_writer;
pub mod string_writer;
pub mod writer;
14 changes: 7 additions & 7 deletions src/io/char_writer.rs → src/io/string_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ use super::writer::Writer;
/// A writer that writes chars to an output.
///
/// It's wrapper of a basic writer with extra functionality.
pub struct CharWriter<W: Write> {
pub struct StringWriter<W: Write> {
/// Number of bytes write to the output.
pub output_char_counter: u64,
pub output_string_length: u64,

/// It optionally captures the output.
pub opt_captured_output: Option<String>,

writer: W,
}

impl<W: Write> CharWriter<W> {
impl<W: Write> StringWriter<W> {
pub fn new(writer: W) -> Self {
Self {
output_char_counter: 0,
output_string_length: 0,
opt_captured_output: Some(String::new()),
writer,
}
}
}

impl<W: Write> Writer for CharWriter<W> {
impl<W: Write> Writer for StringWriter<W> {
fn write_byte(&mut self, byte: u8) -> io::Result<()> {
let c = byte as char;

self.writer.write_char(c).expect("error writing str");

self.output_char_counter += 1;
self.output_string_length += 1;

if let Some(ref mut captured_output) = self.opt_captured_output {
captured_output.push(c);
Expand All @@ -44,7 +44,7 @@ impl<W: Write> Writer for CharWriter<W> {
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;
self.output_string_length += value.len() as u64;

if let Some(ref mut captured_output) = self.opt_captured_output {
captured_output.push_str(value);
Expand Down
12 changes: 6 additions & 6 deletions src/io/writer.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use std::io;

pub trait Writer {
/// It writes one byte to the output (stdout or file).
/// It writes one byte to the output.
///
/// # Errors
///
/// Will return an error if it can't write the byte to the output.
/// Will return an error if it can't write the byte.
fn write_byte(&mut self, byte: u8) -> io::Result<()>;

/// It writes a string to the output (stdout or file).
/// It writes a string to the output.
///
/// # Errors
///
/// Will return an error if it can't write the string (as bytes) to the output.
/// Will return an error if it can't write the string.
fn write_str(&mut self, value: &str) -> io::Result<()>;

/// It gets the captured output is enabled.
/// It gets the captured output if enabled.
fn get_captured_output(&mut self) -> Option<String>;

/// It prints the captured output is enabled.
/// It prints the captured output if enabled.
fn print_captured_output(&self);
}
4 changes: 2 additions & 2 deletions src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
use stack::{Stack, State};

use crate::io::{
byte_reader::ByteReader, byte_writer::ByteWriter, char_writer::CharWriter, writer::Writer,
byte_reader::ByteReader, byte_writer::ByteWriter, string_writer::StringWriter, writer::Writer,
};

pub struct BencodeParser<R: Read> {
Expand Down Expand Up @@ -69,7 +69,7 @@ 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_str<W: FmtWrite>(&mut self, writer: W) -> io::Result<()> {
let mut writer = CharWriter::new(writer);
let mut writer = StringWriter::new(writer);
self.parse(&mut writer)
}

Expand Down

0 comments on commit 4830f24

Please sign in to comment.