A small refactor

This commit is contained in:
Igor Katson 2021-07-12 21:59:08 +01:00
parent 2eabebb5c3
commit 6eef3b9b66
25 changed files with 111 additions and 189 deletions

View file

@ -2,7 +2,7 @@ use std::{cmp::Ordering, str::FromStr};
use serde::{Deserialize, Deserializer, Serialize};
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Id20(pub [u8; 20]);
impl FromStr for Id20 {
@ -67,6 +67,9 @@ impl<'de> Deserialize<'de> for Id20 {
}
impl Id20 {
pub fn to_string(&self) -> String {
hex::encode(self.0)
}
pub fn distance(&self, other: &Id20) -> Id20 {
let mut xor = [0u8; 20];
for (idx, (s, o)) in self

View file

@ -1,7 +0,0 @@
pub type InfoHash = [u8; 20];
pub fn decode_info_hash(hash_str: &str) -> anyhow::Result<InfoHash> {
let mut hash_arr = [0u8; 20];
hex::decode_to_slice(hash_str, &mut hash_arr)?;
Ok(hash_arr)
}

View file

@ -1,6 +1,5 @@
pub mod constants;
pub mod id20;
pub mod info_hash;
pub mod lengths;
pub mod magnet;
pub mod peer_id;

View file

@ -1,8 +1,11 @@
use crate::info_hash::{decode_info_hash, InfoHash};
use std::str::FromStr;
use anyhow::Context;
use crate::id20::Id20;
pub struct Magnet {
pub info_hash: InfoHash,
pub info_hash: Id20,
pub trackers: Vec<String>,
}
@ -12,13 +15,13 @@ impl Magnet {
if url.scheme() != "magnet" {
anyhow::bail!("expected scheme magnet");
}
let mut info_hash: Option<InfoHash> = None;
let mut info_hash: Option<Id20> = None;
let mut trackers = Vec::<String>::new();
for (key, value) in url.query_pairs() {
match key.as_ref() {
"xt" => match value.as_ref().strip_prefix("urn:btih:") {
Some(infohash) => {
info_hash.replace(decode_info_hash(infohash)?);
info_hash.replace(Id20::from_str(infohash)?);
}
None => anyhow::bail!("expected xt to start with urn:btih:"),
},

View file

@ -1,3 +1,5 @@
use crate::id20::Id20;
#[derive(Debug)]
pub enum AzureusStyleKind {
Deluge,
@ -23,7 +25,8 @@ impl AzureusStyleKind {
}
}
fn try_decode_azureus_style(p: &[u8; 20]) -> Option<AzureusStyle> {
fn try_decode_azureus_style(p: &Id20) -> Option<AzureusStyle> {
let p = p.0;
if !(p[0] == b'-' && p[7] == b'-') {
return None;
}
@ -40,11 +43,11 @@ pub enum PeerId {
AzureusStyle(AzureusStyle),
}
pub fn try_decode_peer_id(p: [u8; 20]) -> Option<PeerId> {
pub fn try_decode_peer_id(p: Id20) -> Option<PeerId> {
Some(PeerId::AzureusStyle(try_decode_azureus_style(&p)?))
}
pub fn generate_peer_id() -> [u8; 20] {
pub fn generate_peer_id() -> Id20 {
let mut peer_id = [0u8; 20];
let u = uuid::Uuid::new_v4();
@ -52,5 +55,5 @@ pub fn generate_peer_id() -> [u8; 20] {
(&mut peer_id[..8]).copy_from_slice(b"-rQ0001-");
peer_id
Id20(peer_id)
}

View file

@ -5,6 +5,8 @@ use buffers::{ByteBuf, ByteString};
use clone_to_owned::CloneToOwned;
use serde::Deserialize;
use crate::id20::Id20;
pub type TorrentMetaV1Borrowed<'a> = TorrentMetaV1<ByteBuf<'a>>;
pub type TorrentMetaV1Owned = TorrentMetaV1<ByteString>;
@ -14,7 +16,10 @@ pub fn torrent_from_bytes<'de, ByteBuf: Deserialize<'de>>(
let mut de = BencodeDeserializer::new_from_buf(buf);
de.is_torrent_info = true;
let mut t = TorrentMetaV1::deserialize(&mut de)?;
t.info_hash = de.torrent_info_digest.unwrap();
t.info_hash = Id20(
de.torrent_info_digest
.ok_or_else(|| anyhow::anyhow!("programming error"))?,
);
Ok(t)
}
@ -35,7 +40,7 @@ pub struct TorrentMetaV1<BufType> {
pub creation_date: Option<usize>,
#[serde(skip)]
pub info_hash: [u8; 20],
pub info_hash: Id20,
}
impl<BufType> TorrentMetaV1<BufType> {
@ -292,8 +297,8 @@ mod tests {
let torrent: TorrentMetaV1Borrowed = torrent_from_bytes(&buf).unwrap();
assert_eq!(
torrent.info_hash,
*b"\x64\xa9\x80\xab\xe6\xe4\x48\x22\x6b\xb9\x30\xba\x06\x15\x92\xe4\x4c\x37\x81\xa1"
torrent.info_hash.to_string(),
"64a980abe6e448226bb930ba061592e44c3781a1"
);
}
}