Skip to content

Commit

Permalink
docs: another example
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 16, 2024
1 parent 5978351 commit eab61f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ use torrust_bencode2json::parsers::{BencodeParser};

let mut output = String::new();

let mut parser = BencodeParser::new(b"4:spam");
let mut parser = BencodeParser::new(&b"4:spam"[..]);

parser
.write_str(&mut output)
Expand All @@ -138,6 +138,8 @@ parser
println!("{output}"); // It prints the JSON string: "spam"
```

More [examples](./examples/).

## Test

Run unit and integration tests:
Expand Down
20 changes: 20 additions & 0 deletions examples/parser_stdin_stdout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Run with:
//!
//! ```not_rust
//! echo "4:spam" | cargo run --example parser_stdin_stdout
//! ```
//!
//! It prints "spam".
use std::io;

use torrust_bencode2json::parsers::BencodeParser;

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

if let Err(e) = BencodeParser::new(input).write_bytes(&mut output) {
eprintln!("Error: {e}");
std::process::exit(1);
}
}

0 comments on commit eab61f8

Please sign in to comment.