From eab61f8e8c5661fd065a68c14719c8e5376ad929 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 16 Oct 2024 19:02:01 +0100 Subject: [PATCH] docs: another example --- README.md | 4 +++- examples/parser_stdin_stdout.rs | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 examples/parser_stdin_stdout.rs diff --git a/README.md b/README.md index cc74eed..19acf04 100644 --- a/README.md +++ b/README.md @@ -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) @@ -138,6 +138,8 @@ parser println!("{output}"); // It prints the JSON string: "spam" ``` +More [examples](./examples/). + ## Test Run unit and integration tests: diff --git a/examples/parser_stdin_stdout.rs b/examples/parser_stdin_stdout.rs new file mode 100644 index 0000000..44c4116 --- /dev/null +++ b/examples/parser_stdin_stdout.rs @@ -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); + } +}