Skip to content

Commit 77ad5af

Browse files
committed
refactor: remove writer from main tokenizer
1 parent f6a0584 commit 77ad5af

File tree

3 files changed

+15
-51
lines changed

3 files changed

+15
-51
lines changed

src/parsers/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ pub enum Error {
2727
/// The main parser peeks one byte ahead to know what kind of bencoded value
2828
/// is being parsed. If the byte read after peeking does not match the
2929
/// peeked byte, it means the input is being consumed somewhere else.
30-
#[error("Read byte after peeking does match peeked byte; {0}; {1}")]
31-
ReadByteAfterPeekingDoesMatchPeekedByte(ReadContext, WriteContext),
30+
#[error("Read byte after peeking does match peeked byte; {0}")]
31+
ReadByteAfterPeekingDoesMatchPeekedByte(ReadContext),
3232

3333
/// Unrecognized first byte for new bencoded value.
3434
///
3535
/// The main parser peeks one byte ahead to know what kind of bencoded value
3636
/// is being parsed. This error is raised when the peeked byte is not a
3737
/// valid first byte for a bencoded value.
38-
#[error("Unrecognized first byte for new bencoded value; {0}; {1}")]
39-
UnrecognizedFirstBencodeValueByte(ReadContext, WriteContext),
38+
#[error("Unrecognized first byte for new bencoded value; {0}")]
39+
UnrecognizedFirstBencodeValueByte(ReadContext),
4040

4141
// Integers
4242
/// Unexpected byte parsing integer.

src/parsers/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ impl<R: Read> BencodeParser<R> {
104104
/// - It can't read from the input or write to the output.
105105
/// - The input is invalid Bencode.
106106
fn parse<W: Writer>(&mut self, writer: &mut W) -> Result<(), error::Error> {
107-
let capture_output = Vec::new();
108-
let mut null_writer = ByteWriter::new(capture_output);
109-
110-
while let Some(token) = self.tokenizer.next_token(&mut null_writer)? {
107+
while let Some(token) = self.tokenizer.next_token()? {
111108
match token {
112109
BencodeToken::Integer(integer_bytes) => {
113110
self.begin_bencoded_value(BencodeType::Integer, writer)?;

src/parsers/tokenizer/mod.rs

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ pub mod string;
44

55
use std::io::{self, Read};
66

7-
use super::error::{self, ReadContext, WriteContext};
7+
use super::error::{self, ReadContext};
88

9-
use crate::rw::{byte_reader::ByteReader, byte_writer::ByteWriter, writer::Writer};
9+
use crate::rw::{byte_reader::ByteReader, byte_writer::ByteWriter};
1010

1111
/* TODO:
1212
@@ -50,14 +50,11 @@ impl<R: Read> BencodeTokenizer<R> {
5050
/// Will return an error if:
5151
///
5252
/// - It can't read from the input.
53-
pub fn next_token<W: Writer>(
54-
&mut self,
55-
writer: &mut W,
56-
) -> Result<Option<BencodeToken>, error::Error> {
53+
pub fn next_token(&mut self) -> Result<Option<BencodeToken>, error::Error> {
5754
let capture_output = Vec::new();
5855
let mut null_writer = ByteWriter::new(capture_output);
5956

60-
let opt_peeked_byte = Self::peek_byte(&mut self.byte_reader, &null_writer)?;
57+
let opt_peeked_byte = Self::peek_byte(&mut self.byte_reader)?;
6158

6259
match opt_peeked_byte {
6360
Some(peeked_byte) => {
@@ -71,37 +68,21 @@ impl<R: Read> BencodeTokenizer<R> {
7168
Ok(Some(BencodeToken::String(value)))
7269
}
7370
BENCODE_BEGIN_LIST => {
74-
let _byte = Self::read_peeked_byte(
75-
peeked_byte,
76-
&mut self.byte_reader,
77-
&null_writer,
78-
)?;
71+
let _byte = Self::read_peeked_byte(peeked_byte, &mut self.byte_reader)?;
7972
Ok(Some(BencodeToken::BeginList))
8073
}
8174
BENCODE_BEGIN_DICT => {
82-
let _byte = Self::read_peeked_byte(
83-
peeked_byte,
84-
&mut self.byte_reader,
85-
&null_writer,
86-
)?;
75+
let _byte = Self::read_peeked_byte(peeked_byte, &mut self.byte_reader)?;
8776
Ok(Some(BencodeToken::BeginDict))
8877
}
8978
BENCODE_END_LIST_OR_DICT => {
90-
let _byte = Self::read_peeked_byte(
91-
peeked_byte,
92-
&mut self.byte_reader,
93-
&null_writer,
94-
)?;
79+
let _byte = Self::read_peeked_byte(peeked_byte, &mut self.byte_reader)?;
9580
Ok(Some(BencodeToken::EndListOrDict))
9681
}
9782
b'\n' => {
9883
// todo: we should not return any token and continue to the next token.
9984
// Ignore line breaks at the beginning, the end, or between values
100-
let _byte = Self::read_peeked_byte(
101-
peeked_byte,
102-
&mut self.byte_reader,
103-
&null_writer,
104-
)?;
85+
let _byte = Self::read_peeked_byte(peeked_byte, &mut self.byte_reader)?;
10586
Ok(Some(BencodeToken::LineBreak))
10687
}
10788
_ => Err(error::Error::UnrecognizedFirstBencodeValueByte(
@@ -110,11 +91,6 @@ impl<R: Read> BencodeTokenizer<R> {
11091
pos: self.byte_reader.input_byte_counter(),
11192
latest_bytes: self.byte_reader.captured_bytes(),
11293
},
113-
WriteContext {
114-
byte: Some(peeked_byte),
115-
pos: writer.output_byte_counter(),
116-
latest_bytes: writer.captured_bytes(),
117-
},
11894
)),
11995
}
12096
}
@@ -131,10 +107,9 @@ impl<R: Read> BencodeTokenizer<R> {
131107
///
132108
/// - It can't read from the input.
133109
/// - The byte read is not the expected one (the previously peeked byte).
134-
fn read_peeked_byte<W: Writer>(
110+
fn read_peeked_byte(
135111
peeked_byte: u8,
136112
reader: &mut ByteReader<R>,
137-
writer: &W,
138113
) -> Result<Option<u8>, error::Error> {
139114
match reader.read_byte() {
140115
Ok(byte) => {
@@ -147,11 +122,6 @@ impl<R: Read> BencodeTokenizer<R> {
147122
pos: reader.input_byte_counter(),
148123
latest_bytes: reader.captured_bytes(),
149124
},
150-
WriteContext {
151-
byte: Some(byte),
152-
pos: writer.output_byte_counter(),
153-
latest_bytes: writer.captured_bytes(),
154-
},
155125
))
156126
}
157127
Err(err) => {
@@ -169,10 +139,7 @@ impl<R: Read> BencodeTokenizer<R> {
169139
/// # Errors
170140
///
171141
/// Will return and errors if it can't read from the input.
172-
fn peek_byte<W: Writer>(
173-
reader: &mut ByteReader<R>,
174-
_writer: &W,
175-
) -> Result<Option<u8>, error::Error> {
142+
fn peek_byte(reader: &mut ByteReader<R>) -> Result<Option<u8>, error::Error> {
176143
match reader.peek_byte() {
177144
Ok(byte) => Ok(Some(byte)),
178145
Err(err) => {

0 commit comments

Comments
 (0)