From 4830f24a05e4ed25899a43ea1dd870d373fd339d Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 7 Oct 2024 13:42:49 +0100 Subject: [PATCH] refactor: rename --- src/io/mod.rs | 2 +- src/io/{char_writer.rs => string_writer.rs} | 14 +++++++------- src/io/writer.rs | 12 ++++++------ src/parsers/mod.rs | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) rename src/io/{char_writer.rs => string_writer.rs} (83%) diff --git a/src/io/mod.rs b/src/io/mod.rs index 1cb2849..9ce7ba8 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -1,4 +1,4 @@ pub mod byte_reader; pub mod byte_writer; -pub mod char_writer; +pub mod string_writer; pub mod writer; diff --git a/src/io/char_writer.rs b/src/io/string_writer.rs similarity index 83% rename from src/io/char_writer.rs rename to src/io/string_writer.rs index 9ccd391..812ac1d 100644 --- a/src/io/char_writer.rs +++ b/src/io/string_writer.rs @@ -6,9 +6,9 @@ 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 { +pub struct StringWriter { /// 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, @@ -16,23 +16,23 @@ pub struct CharWriter { writer: W, } -impl CharWriter { +impl StringWriter { pub fn new(writer: W) -> Self { Self { - output_char_counter: 0, + output_string_length: 0, opt_captured_output: Some(String::new()), writer, } } } -impl Writer for CharWriter { +impl Writer for StringWriter { 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); @@ -44,7 +44,7 @@ impl Writer for CharWriter { 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); diff --git a/src/io/writer.rs b/src/io/writer.rs index e91c4d3..dcca445 100644 --- a/src/io/writer.rs +++ b/src/io/writer.rs @@ -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; - /// It prints the captured output is enabled. + /// It prints the captured output if enabled. fn print_captured_output(&self); } diff --git a/src/parsers/mod.rs b/src/parsers/mod.rs index a883fe6..dca1b45 100644 --- a/src/parsers/mod.rs +++ b/src/parsers/mod.rs @@ -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 { @@ -69,7 +69,7 @@ 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_str(&mut self, writer: W) -> io::Result<()> { - let mut writer = CharWriter::new(writer); + let mut writer = StringWriter::new(writer); self.parse(&mut writer) }