mod parse;
pub use self::parse::from_bytes;
use bytes::Bytes;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Metainfo {
pub announce: String,
pub announce_list: Vec<Vec<String>>,
pub info: Info,
pub creation_date: Option<i64>,
pub comment: Option<String>,
pub created_by: Option<String>,
pub encoding: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Info {
pub piece_length: u64,
pub pieces: Vec<[u8; 20]>,
pub mode: Mode,
pub raw_info: Bytes,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Mode {
Single {
name: String,
length: u64,
},
Multiple {
name: String,
files: Vec<FileInfo>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileInfo {
pub length: u64,
pub path: Vec<String>,
}
impl Metainfo {
pub fn info_hash(&self) -> [u8; 20] {
use sha1::{Digest, Sha1};
let mut hasher = Sha1::new();
hasher.update(&self.info.raw_info);
hasher.finalize().into()
}
pub fn to_bytes(&self) -> Vec<u8> {
unimplemented!("Metainfo::to_bytes is not yet supported")
}
}
impl Info {
pub fn total_size(&self) -> u64 {
match &self.mode {
Mode::Single { length, .. } => *length,
Mode::Multiple { files, .. } => files.iter().map(|f| f.length).sum(),
}
}
pub fn num_pieces(&self) -> usize {
self.pieces.len()
}
}