File tree Expand file tree Collapse file tree 5 files changed +148
-0
lines changed
Expand file tree Collapse file tree 5 files changed +148
-0
lines changed Original file line number Diff line number Diff line change 1+ //! Run with:
2+ //!
3+ //! ```not_rust
4+ //! cargo run --example parser_file_in_file_out -- -i ./tests/fixtures/sample.bencode -o output.json
5+ //! ```
6+ //!
7+ //! It should create the `output.json` with this content: `["spam"]`.
8+ use std:: {
9+ fs:: File ,
10+ io:: { Read , Write } ,
11+ } ;
12+
13+ use clap:: { Arg , Command } ;
14+ use torrust_bencode2json:: parsers:: BencodeParser ;
15+
16+ fn main ( ) {
17+ let matches = Command :: new ( "parser_file_in_file_out" )
18+ . version ( "0.1.0" )
19+ . author ( "Torrust Organization" )
20+ . about ( "Converts Bencode to JSON" )
21+ . arg (
22+ Arg :: new ( "input" )
23+ . short ( 'i' )
24+ . long ( "input" )
25+ . help ( "Input file" ) ,
26+ )
27+ . arg (
28+ Arg :: new ( "output" )
29+ . short ( 'o' )
30+ . long ( "output" )
31+ . help ( "Output file" ) ,
32+ )
33+ . get_matches ( ) ;
34+
35+ // Handle input stream (file or stdin)
36+ let input: Box < dyn Read > = if let Some ( input_path) = matches. get_one :: < String > ( "input" ) {
37+ match File :: open ( input_path) {
38+ Ok ( file) => Box :: new ( file) ,
39+ Err ( e) => {
40+ eprintln ! ( "Error: {e}" ) ;
41+ std:: process:: exit ( 1 ) ;
42+ }
43+ }
44+ } else {
45+ eprintln ! ( "Error: missing input file path. Provide a file path with -i or --input" ) ;
46+ std:: process:: exit ( 1 ) ;
47+ } ;
48+
49+ // Handle output stream (file or stdout)
50+ let mut output: Box < dyn Write > = if let Some ( output_path) = matches. get_one :: < String > ( "output" )
51+ {
52+ match File :: create ( output_path) {
53+ Ok ( file) => Box :: new ( file) ,
54+ Err ( e) => {
55+ eprintln ! ( "Error: {e}" ) ;
56+ std:: process:: exit ( 1 ) ;
57+ }
58+ }
59+ } else {
60+ eprintln ! ( "Error: missing output file path. Provide a file path with -o or --output" ) ;
61+ std:: process:: exit ( 1 ) ;
62+ } ;
63+
64+ if let Err ( e) = BencodeParser :: new ( input) . write_bytes ( & mut output) {
65+ eprintln ! ( "Error: {e}" ) ;
66+ std:: process:: exit ( 1 ) ;
67+ }
68+ }
Original file line number Diff line number Diff line change 1+ //! Run with:
2+ //!
3+ //! ```not_rust
4+ //! cargo run --example parser_string_in_string_out
5+ //! ```
6+ //!
7+ //! It prints "spam".
8+ use torrust_bencode2json:: parsers:: BencodeParser ;
9+
10+ fn main ( ) {
11+ let input = "4:spam" . to_string ( ) ;
12+ let mut output = String :: new ( ) ;
13+
14+ if let Err ( e) = BencodeParser :: new ( input. as_bytes ( ) ) . write_str ( & mut output) {
15+ eprintln ! ( "Error: {e}" ) ;
16+ std:: process:: exit ( 1 ) ;
17+ }
18+
19+ println ! ( "{output}" ) ;
20+ }
Original file line number Diff line number Diff line change 1+ //! Run with:
2+ //!
3+ //! ```not_rust
4+ //! cargo run --example parser_string_in_vec_out
5+ //! ```
6+ //!
7+ //! It prints "spam".
8+ use torrust_bencode2json:: parsers:: BencodeParser ;
9+
10+ fn main ( ) {
11+ let input = "4:spam" . to_string ( ) ;
12+ let mut output = Vec :: new ( ) ;
13+
14+ if let Err ( e) = BencodeParser :: new ( input. as_bytes ( ) ) . write_bytes ( & mut output) {
15+ eprintln ! ( "Error: {e}" ) ;
16+ std:: process:: exit ( 1 ) ;
17+ }
18+
19+ println ! ( "{}" , String :: from_utf8_lossy( & output) ) ;
20+ }
Original file line number Diff line number Diff line change 1+ //! Run with:
2+ //!
3+ //! ```not_rust
4+ //! cargo run --example parser_vec_in_string_out
5+ //! ```
6+ //!
7+ //! It prints "spam".
8+ use torrust_bencode2json:: parsers:: BencodeParser ;
9+
10+ fn main ( ) {
11+ let input = b"4:spam" . to_vec ( ) ;
12+ let mut output = String :: new ( ) ;
13+
14+ if let Err ( e) = BencodeParser :: new ( & input[ ..] ) . write_str ( & mut output) {
15+ eprintln ! ( "Error: {e}" ) ;
16+ std:: process:: exit ( 1 ) ;
17+ }
18+
19+ println ! ( "{output}" ) ;
20+ }
Original file line number Diff line number Diff line change 1+ //! Run with:
2+ //!
3+ //! ```not_rust
4+ //! cargo run --example parser_vec_in_vec_out
5+ //! ```
6+ //!
7+ //! It prints "spam".
8+ use torrust_bencode2json:: parsers:: BencodeParser ;
9+
10+ fn main ( ) {
11+ let input = b"4:spam" . to_vec ( ) ;
12+ let mut output = Vec :: new ( ) ;
13+
14+ if let Err ( e) = BencodeParser :: new ( & input[ ..] ) . write_bytes ( & mut output) {
15+ eprintln ! ( "Error: {e}" ) ;
16+ std:: process:: exit ( 1 ) ;
17+ }
18+
19+ println ! ( "{}" , String :: from_utf8_lossy( & output) ) ;
20+ }
You can’t perform that action at this time.
0 commit comments