Skip to content

Commit 68d9915

Browse files
committed
refactor: rename json::BencodeParser to json::Generator
1 parent a3c7c4b commit 68d9915

File tree

13 files changed

+35
-43
lines changed

13 files changed

+35
-43
lines changed

examples/parser_file_in_file_out.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
io::{Read, Write},
1111
};
1212

13-
use bencode2json::generators::json::BencodeParser;
13+
use bencode2json::generators::json::Generator;
1414
use clap::{Arg, Command};
1515

1616
fn main() {
@@ -61,7 +61,7 @@ fn main() {
6161
std::process::exit(1);
6262
};
6363

64-
if let Err(e) = BencodeParser::new(input).write_bytes(&mut output) {
64+
if let Err(e) = Generator::new(input).write_bytes(&mut output) {
6565
eprintln!("Error: {e}");
6666
std::process::exit(1);
6767
}

examples/parser_stdin_stdout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
//! It prints "spam".
88
use std::io;
99

10-
use bencode2json::generators::json::BencodeParser;
10+
use bencode2json::generators::json::Generator;
1111

1212
fn main() {
1313
let input = Box::new(io::stdin());
1414
let mut output = Box::new(io::stdout());
1515

16-
if let Err(e) = BencodeParser::new(input).write_bytes(&mut output) {
16+
if let Err(e) = Generator::new(input).write_bytes(&mut output) {
1717
eprintln!("Error: {e}");
1818
std::process::exit(1);
1919
}

examples/parser_string_in_string_out.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
//! ```
66
//!
77
//! It prints "spam".
8-
use bencode2json::generators::json::BencodeParser;
8+
use bencode2json::generators::json::Generator;
99

1010
fn main() {
1111
let input = "4:spam".to_string();
1212
let mut output = String::new();
1313

14-
if let Err(e) = BencodeParser::new(input.as_bytes()).write_str(&mut output) {
14+
if let Err(e) = Generator::new(input.as_bytes()).write_str(&mut output) {
1515
eprintln!("Error: {e}");
1616
std::process::exit(1);
1717
}

examples/parser_string_in_vec_out.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
//! ```
66
//!
77
//! It prints "spam".
8-
use bencode2json::generators::json::BencodeParser;
8+
use bencode2json::generators::json::Generator;
99

1010
fn main() {
1111
let input = "4:spam".to_string();
1212
let mut output = Vec::new();
1313

14-
if let Err(e) = BencodeParser::new(input.as_bytes()).write_bytes(&mut output) {
14+
if let Err(e) = Generator::new(input.as_bytes()).write_bytes(&mut output) {
1515
eprintln!("Error: {e}");
1616
std::process::exit(1);
1717
}

examples/parser_vec_in_string_out.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
//! ```
66
//!
77
//! It prints "spam".
8-
use bencode2json::generators::json::BencodeParser;
8+
use bencode2json::generators::json::Generator;
99

1010
fn main() {
1111
let input = b"4:spam".to_vec();
1212
let mut output = String::new();
1313

14-
if let Err(e) = BencodeParser::new(&input[..]).write_str(&mut output) {
14+
if let Err(e) = Generator::new(&input[..]).write_str(&mut output) {
1515
eprintln!("Error: {e}");
1616
std::process::exit(1);
1717
}

examples/parser_vec_in_vec_out.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
//! ```
66
//!
77
//! It prints "spam".
8-
use bencode2json::generators::json::BencodeParser;
8+
use bencode2json::generators::json::Generator;
99

1010
fn main() {
1111
let input = b"4:spam".to_vec();
1212
let mut output = Vec::new();
1313

14-
if let Err(e) = BencodeParser::new(&input[..]).write_bytes(&mut output) {
14+
if let Err(e) = Generator::new(&input[..]).write_bytes(&mut output) {
1515
eprintln!("Error: {e}");
1616
std::process::exit(1);
1717
}

src/generators/json.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
/* TODO:
2-
3-
- Rename this parser to generator.
4-
5-
*/
6-
1+
//! Json generator for bencoded data.
72
use core::str;
83
use std::{
94
fmt::Write as FmtWrite,
@@ -22,13 +17,13 @@ use crate::{
2217
tokenizer,
2318
};
2419

25-
pub struct BencodeParser<R: Read> {
20+
pub struct Generator<R: Read> {
2621
tokenizer: Tokenizer<R>,
2722
num_processed_tokens: u64,
2823
stack: Stack,
2924
}
3025

31-
impl<R: Read> BencodeParser<R> {
26+
impl<R: Read> Generator<R> {
3227
const JSON_ARRAY_BEGIN: u8 = b'[';
3328
const JSON_ARRAY_ITEMS_SEPARATOR: u8 = b',';
3429
const JSON_ARRAY_END: u8 = b']';
@@ -39,7 +34,7 @@ impl<R: Read> BencodeParser<R> {
3934
const JSON_OBJ_END: u8 = b'}';
4035

4136
pub fn new(reader: R) -> Self {
42-
BencodeParser {
37+
Generator {
4338
tokenizer: Tokenizer::new(reader),
4439
num_processed_tokens: 1,
4540
stack: Stack::default(),
@@ -353,16 +348,16 @@ mod tests {
353348

354349
use std::io::{self, Read};
355350

356-
use crate::generators::json::BencodeParser;
351+
use crate::generators::json::Generator;
357352

358353
mod it_should_allow_writing {
359-
use crate::generators::json::BencodeParser;
354+
use crate::generators::json::Generator;
360355

361356
#[test]
362357
fn to_any_type_implementing_io_write_trait() {
363358
let mut output = Vec::new();
364359

365-
let mut parser = BencodeParser::new(&b"i0e"[..]);
360+
let mut parser = Generator::new(&b"i0e"[..]);
366361

367362
parser
368363
.write_bytes(&mut output)
@@ -375,7 +370,7 @@ mod tests {
375370
fn writing_to_any_type_implementing_fmt_write_trait() {
376371
let mut output = String::new();
377372

378-
let mut parser = BencodeParser::new(&b"i0e"[..]);
373+
let mut parser = Generator::new(&b"i0e"[..]);
379374

380375
parser
381376
.write_str(&mut output)
@@ -400,7 +395,7 @@ mod tests {
400395

401396
let mut output = String::new();
402397

403-
let mut parser = BencodeParser::new(EmptyReader);
398+
let mut parser = Generator::new(EmptyReader);
404399

405400
parser.write_str(&mut output).unwrap();
406401

@@ -409,13 +404,13 @@ mod tests {
409404

410405
mod it_should_allow_special_bencode_cases {
411406

412-
use crate::{generators::json::BencodeParser, test::bencode_to_json_unchecked};
407+
use crate::{generators::json::Generator, test::bencode_to_json_unchecked};
413408

414409
#[test]
415410
fn an_empty_input() {
416411
let mut output = String::new();
417412

418-
let mut parser = BencodeParser::new(&b""[..]);
413+
let mut parser = Generator::new(&b""[..]);
419414

420415
parser
421416
.write_str(&mut output)
@@ -446,7 +441,7 @@ mod tests {
446441
mod it_should_fail {
447442
use std::io::{self, Read};
448443

449-
use crate::{error::Error, generators::json::BencodeParser, try_bencode_to_json};
444+
use crate::{error::Error, generators::json::Generator, try_bencode_to_json};
450445

451446
#[test]
452447
fn when_there_is_a_problem_reading_from_input() {
@@ -463,7 +458,7 @@ mod tests {
463458

464459
let mut output = String::new();
465460

466-
let mut parser = BencodeParser::new(FaultyReader);
461+
let mut parser = Generator::new(FaultyReader);
467462

468463
let result = parser.write_str(&mut output);
469464

src/generators/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub mod json;
22
pub mod stack;
33

4+
// todo: extract trait for generators when we implement a new one.
5+
46
use derive_more::derive::Display;
57

68
#[derive(Debug, PartialEq, Display)]

src/generators/stack.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//! The stack used by the Bencoded to JSON converter to keep track of the
2-
//! current parsing state.
1+
//! The stack used by the generators to keep track of the current parsing state.
32
use std::fmt::Display;
43

54
/// Stack containing states for nested Bencoded values.

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub mod rw;
4040
pub mod tokenizer;
4141

4242
use error::Error;
43-
use generators::json::BencodeParser;
43+
use generators::json::Generator;
4444
mod test;
4545

4646
/// It converts bencoded bytes into a JSON string.
@@ -51,7 +51,7 @@ mod test;
5151
pub fn try_bencode_to_json(input_buffer: &[u8]) -> Result<String, Error> {
5252
let mut output = String::new();
5353

54-
let mut parser = BencodeParser::new(input_buffer);
54+
let mut parser = Generator::new(input_buffer);
5555

5656
match parser.write_str(&mut output) {
5757
Ok(()) => Ok(output),

0 commit comments

Comments
 (0)