Skip to content

Commit a2eb63c

Browse files
committed
Merge #13: Include <string> tags for valid UTF-8 byte sequences
15e4584 docs: recover original header (magecnion) 0927211 docs: recover original header (magecnion) 838ffc0 docs: update README.md (magecnion) 9f38ef6 feat!: include <string> tag for valid utf8 byte sequences (magecnion) Pull request description: - Introduce `<string>` tags around valid UTF-8 byte sequences in the output. - Update documentation to reflect these changes. - Modify the `bencoded_string_with_repeated_byte` function so it works with other lengths different from `1000000`. ACKs for top commit: josecelano: ACK 15e4584 Tree-SHA512: cbe3e888d9fdca2ebfccd01ecbfb9c9bb6597fdfff2f6f84cbbfba599e1feb4203df14f609b192f05d7ea3776957817ad82399bef0fb99d76cadf8c88b3850d8
2 parents bc0fd09 + 15e4584 commit a2eb63c

File tree

7 files changed

+296
-160
lines changed

7 files changed

+296
-160
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ Run the binary with stdin and stdout (UTF-8):
3232

3333
```console
3434
echo "4:spam" | cargo run
35-
"spam"
35+
"<string>spam</string>"
3636
```
3737

3838
Run the binary with stdin and stdout (non UTF-8):
3939

4040
```console
4141
printf "d3:bar2:\xFF\xFEe" | cargo run
42-
{"bar":"<hex>fffe</hex>"}
42+
{"<string>bar</string>":"<hex>fffe</hex>"}
4343
```
4444

4545
```console
4646
printf "d2:\xFF\xFE3:bare" | cargo run
47-
{"<hex>fffe</hex>":"bar"}
47+
{"<hex>fffe</hex>":"<string>bar</string>"}
4848
```
4949

5050
> NOTICE: We need two escape the two bytes `FF` and `FE` with `\x` inside the string.
@@ -53,7 +53,7 @@ More examples:
5353

5454
```console
5555
cat ./tests/fixtures/sample.bencode | cargo run
56-
["spam"]
56+
["<string>spam</string>"]
5757
```
5858

5959
More examples with invalid Bencode:
@@ -81,9 +81,9 @@ echo "d3:foold3:bari42eeee" | cargo run | jq
8181

8282
```json
8383
{
84-
"foo": [
84+
"<string>foo</string>": [
8585
{
86-
"bar": 42
86+
"<string>bar</string>": 42
8787
}
8888
]
8989
}
@@ -121,7 +121,7 @@ use bencode2json::{try_bencode_to_json};
121121

122122
let result = try_bencode_to_json(b"d4:spam4:eggse").unwrap();
123123

124-
assert_eq!(result, r#"{"spam":"eggs"}"#);
124+
assert_eq!(result, r#"{"<string>spam</string>":"<string>eggs</<string>string>"}"#);
125125
```
126126

127127
Example using the low-level parser:
@@ -137,7 +137,7 @@ parser
137137
.write_str(&mut output)
138138
.expect("Bencode to JSON conversion failed");
139139

140-
println!("{output}"); // It prints the JSON string: "spam"
140+
println!("{output}"); // It prints the JSON string: "<string>spam</string>"
141141
```
142142

143143
More [examples](./examples/).

examples/try_bencode_to_json.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ use bencode2json::try_bencode_to_json;
88
fn main() {
99
let result = try_bencode_to_json(b"d4:spam4:eggse").unwrap();
1010

11-
assert_eq!(result, r#"{"spam":"eggs"}"#);
11+
assert_eq!(
12+
result,
13+
r#"{"<string>spam</string>":"<string>eggs</string>"}"#
14+
);
1215
}

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//!
2323
//! let result = try_bencode_to_json(b"d4:spam4:eggse").unwrap();
2424
//!
25-
//! assert_eq!(result, r#"{"spam":"eggs"}"#);
25+
//! assert_eq!(result, r#"{"<string>spam</string>":"<string>eggs</string>"}"#);
2626
//! ```
2727
//!
2828
//! The primary goal of this lib is to provide a simple and easy-to-use API for
@@ -73,7 +73,10 @@ mod tests {
7373
fn when_it_succeeds() {
7474
let result = try_bencode_to_json(b"d4:spam4:eggse").unwrap();
7575

76-
assert_eq!(result, r#"{"spam":"eggs"}"#);
76+
assert_eq!(
77+
result,
78+
r#"{"<string>spam</string>":"<string>eggs</string>"}"#
79+
);
7780
}
7881

7982
#[test]

0 commit comments

Comments
 (0)