Skip to content

Commit e51d7c2

Browse files
add
1 parent 46c4aae commit e51d7c2

6 files changed

Lines changed: 33 additions & 20 deletions

File tree

crates/bencode/src/dispatcher/bdecode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::parser::parse_bencode;
21
use crate::enums::bencode::BencodeValue;
2+
use crate::parser::parse_bencode;
33

44
/// Decode bencode data using the cursor-based iterative parser.
55
pub fn decode_bencode(data: &[u8]) -> Result<(BencodeValue, &[u8]), &'static str> {

crates/bencode/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
pub mod parser;
21
pub mod dispatcher;
32
pub mod encoders;
43
pub mod enums;
4+
pub mod parser;
55

66
// Re-export top-level API for convenience
77
pub use dispatcher::bdecode::decode_bencode;

crates/bencode/src/parser/cursor.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ impl<'a> Cursor<'a> {
5757
}
5858
let digit = (b - b'0') as usize;
5959
if digit < 10 {
60-
len = len.checked_mul(10)
60+
len = len
61+
.checked_mul(10)
6162
.ok_or("String length overflow")?
6263
.checked_add(digit)
6364
.ok_or("String length overflow")?;
@@ -89,7 +90,9 @@ impl<'a> Cursor<'a> {
8990
}
9091

9192
// Leading-zero check: if next byte is '0' and followed by another digit, reject
92-
if let Some(&b) = self.input.get(self.position) && b == b'0' {
93+
if let Some(&b) = self.input.get(self.position)
94+
&& b == b'0'
95+
{
9396
if let Some(&b2) = self.input.get(self.position + 1)
9497
&& b2.is_ascii_digit()
9598
{
@@ -110,7 +113,8 @@ impl<'a> Cursor<'a> {
110113
}
111114
if b.is_ascii_digit() {
112115
let digit = (b - b'0') as i128;
113-
value = value.checked_mul(10)
116+
value = value
117+
.checked_mul(10)
114118
.ok_or("Integer overflow")?
115119
.checked_add(digit)
116120
.ok_or("Integer overflow")?;

crates/bencode/src/parser/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ pub fn parse_bencode(input: &[u8]) -> Result<(BencodeValue, &[u8]), &'static str
4040
}
4141
b'l' => {
4242
cursor.advance(1);
43-
stack.push(ParseFrame::List {
44-
items: Vec::new(),
45-
});
43+
stack.push(ParseFrame::List { items: Vec::new() });
4644
}
4745
b'd' => {
4846
cursor.advance(1);
@@ -121,7 +119,10 @@ fn push_value(stack: &mut ParseStack, value: BencodeValue) -> Result<(), &'stati
121119
items.push(value);
122120
Ok(())
123121
}
124-
ParseFrame::Dict { entries, pending_key } => {
122+
ParseFrame::Dict {
123+
entries,
124+
pending_key,
125+
} => {
125126
if let Some(key) = pending_key.take() {
126127
entries.insert(key, value);
127128
Ok(())
@@ -143,7 +144,10 @@ fn push_value(stack: &mut ParseStack, value: BencodeValue) -> Result<(), &'stati
143144
fn complete_frame(frame: ParseFrame) -> Result<BencodeValue, &'static str> {
144145
match frame {
145146
ParseFrame::List { items } => Ok(BencodeValue::List(items)),
146-
ParseFrame::Dict { entries, pending_key } => {
147+
ParseFrame::Dict {
148+
entries,
149+
pending_key,
150+
} => {
147151
if pending_key.is_some() {
148152
return Err("Dictionary has unpaired key at end");
149153
}

crates/bencode/src/parser/stack.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use std::collections::BTreeMap;
66
#[derive(Debug)]
77
pub enum ParseFrame {
88
/// Collecting items into a list (terminated by 'e').
9-
List {
10-
items: Vec<BencodeValue>,
11-
},
9+
List { items: Vec<BencodeValue> },
1210
/// Collecting key-value pairs into a dictionary (terminated by 'e').
1311
Dict {
1412
entries: BTreeMap<Vec<u8>, BencodeValue>,

crates/bencode/src/parser/tests.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#[allow(clippy::module_inception)]
33
mod tests {
44
use crate::enums::bencode::BencodeValue;
5-
use crate::parser::{parse_bencode, Cursor};
5+
use crate::parser::{Cursor, parse_bencode};
66

77
// --- Strings ---
88

@@ -141,7 +141,10 @@ mod tests {
141141
match val {
142142
BencodeValue::Dict(map) => {
143143
assert_eq!(map.len(), 1);
144-
assert_eq!(*map.get(b"name".as_slice()).unwrap(), BencodeValue::Str(b"alice".to_vec()));
144+
assert_eq!(
145+
*map.get(b"name".as_slice()).unwrap(),
146+
BencodeValue::Str(b"alice".to_vec())
147+
);
145148
}
146149
_ => panic!("Expected dict"),
147150
}
@@ -154,7 +157,10 @@ mod tests {
154157
BencodeValue::Dict(map) => {
155158
assert_eq!(map.len(), 2);
156159
assert_eq!(*map.get(b"age".as_slice()).unwrap(), BencodeValue::Int(25));
157-
assert_eq!(*map.get(b"name".as_slice()).unwrap(), BencodeValue::Str(b"alice".to_vec()));
160+
assert_eq!(
161+
*map.get(b"name".as_slice()).unwrap(),
162+
BencodeValue::Str(b"alice".to_vec())
163+
);
158164
}
159165
_ => panic!("Expected dict"),
160166
}
@@ -169,7 +175,10 @@ mod tests {
169175
match map.get(b"addr".as_slice()) {
170176
Some(BencodeValue::Dict(inner)) => {
171177
assert_eq!(inner.len(), 1);
172-
assert_eq!(*inner.get(b"zipcode".as_slice()).unwrap(), BencodeValue::Int(90210));
178+
assert_eq!(
179+
*inner.get(b"zipcode".as_slice()).unwrap(),
180+
BencodeValue::Int(90210)
181+
);
173182
}
174183
_ => panic!("Expected nested dict"),
175184
}
@@ -234,9 +243,7 @@ mod tests {
234243
map.insert(b"key".to_vec(), BencodeValue::Int(-99));
235244
map
236245
}),
237-
BencodeValue::List(vec![
238-
BencodeValue::Str(b"nested".to_vec()),
239-
]),
246+
BencodeValue::List(vec![BencodeValue::Str(b"nested".to_vec())]),
240247
]);
241248

242249
let encoded = encode_bencode(original.clone()).unwrap();

0 commit comments

Comments
 (0)