Skip to content

Commit

Permalink
feat: new binary to par torrent files
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Sep 1, 2023
1 parent b3fe7f9 commit 9381212
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug executable 'parse_torrent'",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/parse_torrent",
"args": ["./tests/fixtures/torrents/MC_GRID.zip-3cd18ff2d3eec881207dcc5ca5a2c3a2a3afe462.torrent"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "cargo build",
"miDebuggerPath": "/usr/bin/gdb",
"linux": {
"miDebuggerPath": "/usr/bin/gdb"
},
"windows": {
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe"
},
"osx": {
"miDebuggerPath": "/usr/local/bin/gdb"
}
}
]
}
41 changes: 41 additions & 0 deletions src/bin/parse_torrent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! Command line tool to parse a torrent file and print the decoded torrent.
//!
//! It's only used for debugging purposes.
use std::env;
use std::fs::File;
use std::io::{self, Read};

use serde_bencode::de::from_bytes;
use serde_bencode::value::Value as BValue;
use torrust_index_backend::utils::parse_torrent;

fn main() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
eprintln!("Usage: cargo run --bin parse_torrent <PATH_TO_TORRENT_FILE>");
eprintln!("Example: cargo run --bin parse_torrent ./tests/fixtures/torrents/MC_GRID.zip-3cd18ff2d3eec881207dcc5ca5a2c3a2a3afe462.torrent");
std::process::exit(1);
}

println!("Reading the torrent file ...");

let mut file = File::open(&args[1])?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;

println!("Decoding torrent with standard serde implementation ...");

match from_bytes::<BValue>(&bytes) {
Ok(_value) => match parse_torrent::decode_torrent(&bytes) {
Ok(torrent) => {
println!("Parsed torrent: \n{torrent:#?}");
Ok(())
}
Err(e) => Err(io::Error::new(io::ErrorKind::Other, format!("Error: invalid torrent!. {e}"))),
},
Err(e) => Err(io::Error::new(
io::ErrorKind::Other,
format!("Error: invalid bencode data!. {e}"),
)),
}
}
Binary file not shown.

0 comments on commit 9381212

Please sign in to comment.