-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser_file_in_file_out.rs
More file actions
68 lines (63 loc) · 1.94 KB
/
parser_file_in_file_out.rs
File metadata and controls
68 lines (63 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! Run with:
//!
//! ```not_rust
//! cargo run --example parser_file_in_file_out -- -i ./tests/fixtures/sample.bencode -o output.json
//! ```
//!
//! It should create the `output.json` with this content: `["spam"]`.
use std::{
fs::File,
io::{Read, Write},
};
use bencode2json::generators::json::Generator;
use clap::{Arg, Command};
fn main() {
let matches = Command::new("parser_file_in_file_out")
.version("0.1.0")
.author("Torrust Organization")
.about("Converts Bencode to JSON")
.arg(
Arg::new("input")
.short('i')
.long("input")
.help("Input file"),
)
.arg(
Arg::new("output")
.short('o')
.long("output")
.help("Output file"),
)
.get_matches();
// Handle input stream (file or stdin)
let input: Box<dyn Read> = if let Some(input_path) = matches.get_one::<String>("input") {
match File::open(input_path) {
Ok(file) => Box::new(file),
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
} else {
eprintln!("Error: missing input file path. Provide a file path with -i or --input");
std::process::exit(1);
};
// Handle output stream (file or stdout)
let mut output: Box<dyn Write> = if let Some(output_path) = matches.get_one::<String>("output")
{
match File::create(output_path) {
Ok(file) => Box::new(file),
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
} else {
eprintln!("Error: missing output file path. Provide a file path with -o or --output");
std::process::exit(1);
};
if let Err(e) = Generator::new(input).write_bytes(&mut output) {
eprintln!("Error: {e}");
std::process::exit(1);
}
}