use std::io::Write;
use bendy::encoding::{AsString, Error, SingleItemEncoder, ToBencode};
#[derive(Debug)]
struct MetaInfo {
pub announce: String,
pub info: Info,
pub comment: Option<String>, pub creation_date: Option<String>, pub http_seeds: Option<Vec<String>>, }
#[derive(Debug)]
struct Info {
pub piece_length: String,
pub pieces: Vec<u8>,
pub name: String,
pub file_length: String,
}
impl ToBencode for MetaInfo {
const MAX_DEPTH: usize = Info::MAX_DEPTH + 1;
fn encode(&self, encoder: SingleItemEncoder) -> Result<(), Error> {
encoder.emit_dict(|mut e| {
e.emit_pair(b"announce", &self.announce)?;
if let Some(comment) = &self.comment {
e.emit_pair(b"comment", comment)?;
}
if let Some(creation_date) = &self.creation_date {
e.emit_pair(b"creation date", creation_date)?;
}
if let Some(seeds) = &self.http_seeds {
e.emit_pair(b"httpseeds", seeds)?;
}
e.emit_pair(b"info", &self.info)
})?;
Ok(())
}
}
impl ToBencode for Info {
const MAX_DEPTH: usize = 1;
fn encode(&self, encoder: SingleItemEncoder) -> Result<(), Error> {
encoder.emit_dict(|mut e| {
e.emit_pair(b"length", &self.file_length)?;
e.emit_pair(b"name", &self.name)?;
e.emit_pair(b"piece length", &self.piece_length)?;
e.emit_pair(b"pieces", AsString(&self.pieces))
})?;
Ok(())
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let torrent = MetaInfo {
announce: "http://bttracker.debian.org:6969/announce".to_owned(),
comment: Some("\"Debian CD from cdimage.debian.org\"".to_owned()),
creation_date: Some(1_520_682_848.to_string()),
http_seeds: Some(vec![
"https://cdimage.debian.org/cdimage/release/9.4.0//srv/cdbuilder.debian.org/dst/deb-cd/weekly-builds/amd64/iso-cd/debian-9.4.0-amd64-netinst.iso".to_owned(),
"https://cdimage.debian.org/cdimage/archive/9.4.0//srv/cdbuilder.debian.org/dst/deb-cd/weekly-builds/amd64/iso-cd/debian-9.4.0-amd64-netinst.iso".to_owned(),
]),
info: Info {
piece_length: 262_144.to_string(),
pieces: include_bytes!("torrent_files/pieces.iso").to_vec(),
name: "debian-9.4.0-amd64-netinst.iso".to_owned(),
file_length: 305_135_616.to_string(),
},
};
let data = torrent.to_bencode()?;
std::io::stdout().write_all(&data)?;
Ok(())
}