Skip to content

Commit 15f66a6

Browse files
josecelanoda2ce7
authored andcommitted
docs: improve crate docs
1 parent eabe360 commit 15f66a6

File tree

4 files changed

+105
-13
lines changed

4 files changed

+105
-13
lines changed

src/lib.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
//! This lib contains functions to convert bencoded bytes into a JSON string.
22
//!
3-
//! There are high-level functions for common purposes that call the lower level
4-
//! parser. You can use the low-lever parser if the high-level wrappers are not
5-
//! suitable for your needs.
3+
//! Bencode is a simple encoding format that is used to encode arbitrary
4+
//! data structures. It is commonly used in the context of torrent files,
5+
//! where the data structures are used to describe the contents of the torrent
6+
//! file.
7+
//!
8+
//! To learn more about bencode, you can refer to the following resources:
9+
//!
10+
//! - <https://en.wikipedia.org/wiki/Bencode>
11+
//! - <https://www.bittorrent.org/beps/bep_0003.html>
12+
//!
13+
//! Thi lib has high-level functions for common purposes that call the lower
14+
//! level parser. You can use the low-lever parser if the high-level wrappers
15+
//! are not suitable for your needs.
16+
//!
17+
//! The most straightforward way to use this lib is to use the `try_bencode_to_json`
18+
//! function:
19+
//!
20+
//! ```rust
21+
//! use torrust_bencode2json::{try_bencode_to_json};
22+
//!
23+
//! let result = try_bencode_to_json(b"d4:spam4:eggse").unwrap();
24+
//!
25+
//! assert_eq!(result, r#"{"spam":"eggs"}"#);
26+
//! ```
27+
//!
28+
//! The primary goal of this lib is to provide a simple and easy-to-use API for
29+
//! converting bencoded data into JSON. It's also designed to be flexible and
30+
//! efficient, making it suitable for a wide range of use cases.
31+
//!
32+
//! A design requirement is to be able to parse bencoded data without building
33+
//! an in-memory representation of the whole bencoded data structure.
34+
//!
35+
//! > __NOTICE__: In the context of this lib, parser is a function that takes an input
36+
//! > containing bencoded data and produces a JSON output (raw bytes or UTF-8 string).
637
use parsers::{error::Error, BencodeParser};
738

839
pub mod parsers;

src/parsers/error.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,70 +11,110 @@ use crate::rw;
1111

1212
use super::BencodeType;
1313

14+
/// Errors that can occur while parsing a bencoded value.
1415
#[derive(Debug, Error)]
1516
pub enum Error {
17+
/// I/O error.
1618
#[error("I/O error: {0}")]
1719
Io(#[from] io::Error),
1820

21+
/// R/W error.
1922
#[error("R/W error: {0}")]
2023
Rw(#[from] rw::error::Error),
2124

25+
/// Read byte after peeking does match peeked byte.
26+
///
27+
/// The main parser peeks one byte ahead to know what kind of bencoded value
28+
/// is being parsed. If the byte read after peeking does not match the
29+
/// peeked byte, it means the input is being consumed somewhere else.
2230
#[error("Read byte after peeking does match peeked byte; {0}; {1}")]
2331
ReadByteAfterPeekingDoesMatchPeekedByte(ReadContext, WriteContext),
2432

33+
/// Unrecognized first byte for new bencoded value.
34+
///
35+
/// The main parser peeks one byte ahead to know what kind of bencoded value
36+
/// is being parsed. This error is raised when the peeked byte is not a
37+
/// valid first byte for a bencoded value.
2538
#[error("Unrecognized first byte for new bencoded value; {0}; {1}")]
2639
UnrecognizedFirstBencodeValueByte(ReadContext, WriteContext),
2740

2841
// Integers
42+
/// Unexpected byte parsing integer.
43+
///
44+
/// The main parser parses integers by reading bytes until it finds the
45+
/// end of the integer. This error is raised when the byte read is not a
46+
/// valid byte for an integer bencoded value.
2947
#[error("Unexpected byte parsing integer; {0}; {1}")]
3048
UnexpectedByteParsingInteger(ReadContext, WriteContext),
3149

50+
/// Unexpected end of input parsing integer.
51+
///
52+
/// The input ends before the integer ends.
3253
#[error("Unexpected end of input parsing integer; {0}; {1}")]
3354
UnexpectedEndOfInputParsingInteger(ReadContext, WriteContext),
3455

56+
/// Leading zeros in integers are not allowed, for example b'i00e'.
3557
#[error("Leading zeros in integers are not allowed, for example b'i00e'; {0}; {1}")]
3658
LeadingZerosInIntegersNotAllowed(ReadContext, WriteContext),
3759

3860
// Strings
61+
/// Invalid string length byte, expected a digit.
62+
///
63+
/// The string parser found an invalid byte for the string length. The
64+
/// length can only be made of digits (0-9).
3965
#[error("Invalid string length byte, expected a digit; {0}; {1}")]
4066
InvalidStringLengthByte(ReadContext, WriteContext),
4167

68+
/// Unexpected end of input parsing string length.
69+
///
70+
/// The input ends before the string length ends.
4271
#[error("Unexpected end of input parsing string length; {0}; {1}")]
4372
UnexpectedEndOfInputParsingStringLength(ReadContext, WriteContext),
4473

74+
/// Unexpected end of input parsing string value.
75+
///
76+
/// The input ends before the string value ends.
4577
#[error("Unexpected end of input parsing string value; {0}; {1}")]
4678
UnexpectedEndOfInputParsingStringValue(ReadContext, WriteContext),
4779

4880
// Lists
81+
/// Unexpected end of input parsing list. Expecting first list item or list end.
4982
#[error(
5083
"Unexpected end of input parsing list. Expecting first list item or list end; {0}; {1}"
5184
)]
5285
UnexpectedEndOfInputExpectingFirstListItemOrEnd(ReadContext, WriteContext),
5386

87+
/// Unexpected end of input parsing list. Expecting next list item.
5488
#[error("Unexpected end of input parsing list. Expecting next list item; {0}; {1}")]
5589
UnexpectedEndOfInputExpectingNextListItem(ReadContext, WriteContext),
5690

5791
// Dictionaries
92+
/// Unexpected end of input parsing dictionary. Expecting first dictionary field or dictionary end.
5893
#[error("Unexpected end of input parsing dictionary. Expecting first dictionary field or dictionary end; {0}; {1}")]
5994
UnexpectedEndOfInputExpectingFirstDictFieldOrEnd(ReadContext, WriteContext),
6095

96+
/// Unexpected end of input parsing dictionary. Expecting dictionary field value.
6197
#[error(
6298
"Unexpected end of input parsing dictionary. Expecting dictionary field value; {0}; {1}"
6399
)]
64100
UnexpectedEndOfInputExpectingDictFieldValue(ReadContext, WriteContext),
65101

102+
/// Unexpected end of input parsing dictionary. Expecting dictionary field key or end.
66103
#[error(
67104
"Unexpected end of input parsing dictionary. Expecting dictionary field key or end; {0}; {1}"
68105
)]
69106
UnexpectedEndOfInputExpectingDictFieldKeyOrEnd(ReadContext, WriteContext),
70107

108+
/// Unexpected end of dictionary. Premature end of dictionary.
71109
#[error("Unexpected end of dictionary. Premature end of dictionary; {0}; {1}")]
72110
PrematureEndOfDict(ReadContext, WriteContext),
73111

112+
/// Expected string for dictionary field key.
74113
#[error("Expected string for dictionary field key, but got: {0}, {1}")]
75114
ExpectedStringForDictKeyGot(BencodeType, ReadContext, WriteContext),
76115

77116
// List and dictionaries
117+
/// Unexpected end of list or dict. No matching start for the list or dict end.
78118
#[error(
79119
"Unexpected end of list or dict. No matching start for the list or dict end: {0}, {1}"
80120
)]

src/parsers/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Parsers, including the main parser and the parsers for the basic types
2-
//! (integer and string)
2+
//! (integer and string).
3+
//!
4+
//! ``BencodeParser`` is the main parser. It is generic over the type of the
5+
//! input buffer.
36
pub mod error;
47
pub mod integer;
58
pub mod stack;

src/parsers/stack.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
//! current parsing state.
33
use std::fmt::Display;
44

5-
/// Stack with containing states for nested Bencoded values.
5+
/// Stack containing states for nested Bencoded values.
66
///
7-
/// The stat has an immutable initial state.
7+
/// The stack has an immutable initial state.
8+
///
9+
/// > NOTICE!: It's not allowed to pop or change the initial state.
810
#[derive(Debug)]
911
pub struct Stack {
12+
/// The stack of states.
1013
states: Vec<State>,
1114
}
1215

@@ -17,21 +20,36 @@ pub struct Stack {
1720
///
1821
/// States are displayed with a short name using only one letter:
1922
///
20-
/// I, L, M, D, E, F
23+
/// `I`, `L`, `M`, `D`, `E`, `F`
2124
///
22-
/// This comes from the original implementation.
25+
/// This comes from the original implementation in C.
2326
#[derive(Debug, PartialEq, Clone)]
2427
pub enum State {
28+
/// The initial state.
29+
/// /// The sort display name for the state is L.
2530
Initial, // I
2631

2732
// States while parsing lists
28-
ExpectingFirstListItemOrEnd, // L
29-
ExpectingNextListItem, // M
33+
/// Expecting the first list item or the end of the list.
34+
/// The sort display name for the state is L.
35+
ExpectingFirstListItemOrEnd,
36+
37+
/// Expecting the next list item. List contains at least one item.
38+
/// The sort display name for the state is M.
39+
ExpectingNextListItem,
3040

3141
// States while parsing dictionaries
32-
ExpectingFirstDictFieldOrEnd, // D
33-
ExpectingDictFieldValue, // E
34-
ExpectingDictFieldKeyOrEnd, // F
42+
/// Expecting the first dict field or the end of the dict.
43+
/// The sort display name for the state is D.
44+
ExpectingFirstDictFieldOrEnd,
45+
46+
/// Expecting the dict field value.
47+
/// The sort display name for the state is E.
48+
ExpectingDictFieldValue,
49+
50+
/// Expecting the dict field key or the end of the dict.
51+
/// The sort display name for the state is F.
52+
ExpectingDictFieldKeyOrEnd,
3553
}
3654

3755
impl Display for State {

0 commit comments

Comments
 (0)