diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..f6f4ed4c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,35 @@ +{ + "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" + } + } + ] +} \ No newline at end of file diff --git a/src/bin/parse_torrent.rs b/src/bin/parse_torrent.rs new file mode 100644 index 00000000..ccef09a9 --- /dev/null +++ b/src/bin/parse_torrent.rs @@ -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 = env::args().collect(); + if args.len() != 2 { + eprintln!("Usage: cargo run --bin parse_torrent "); + 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::(&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}"), + )), + } +} diff --git a/tests/fixtures/torrents/MC_GRID.zip-3cd18ff2d3eec881207dcc5ca5a2c3a2a3afe462.torrent b/tests/fixtures/torrents/MC_GRID.zip-3cd18ff2d3eec881207dcc5ca5a2c3a2a3afe462.torrent new file mode 100644 index 00000000..38e24e4b Binary files /dev/null and b/tests/fixtures/torrents/MC_GRID.zip-3cd18ff2d3eec881207dcc5ca5a2c3a2a3afe462.torrent differ