From dcbb4108fd2b5bd4734314b8e1a02082be817bfd Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 8 Jul 2025 15:47:52 +0300 Subject: [PATCH] implement info-hash enum --- src/api.rs | 14 ++++++-------- src/api/info_hash.rs | 15 +++++++++++++++ src/main.rs | 2 ++ 3 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 src/api/info_hash.rs diff --git a/src/api.rs b/src/api.rs index 2e117a5..501d8fd 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,8 +1,11 @@ +mod info_hash; +use info_hash::InfoHash; + /// Parse infohash from the source filepath, -/// decode hash bytes to String array on success. +/// decode hash bytes to `InfoHash` array on success. /// /// * return `None` if the `path` is not reachable -pub fn get(path: &str) -> Option> { +pub fn get(path: &str) -> Option> { use std::io::Read; if path.contains("://") { todo!("URL sources yet not supported") @@ -16,12 +19,7 @@ pub fn get(path: &str) -> Option> { if l != L { break; } - r.push( - b[..l] - .iter() - .map(|i| format!("{i:02x}")) - .collect::(), - ) + r.push(InfoHash::V1(b[..l].to_vec())) } Some(r) } diff --git a/src/api/info_hash.rs b/src/api/info_hash.rs new file mode 100644 index 0000000..9fd85b2 --- /dev/null +++ b/src/api/info_hash.rs @@ -0,0 +1,15 @@ +pub enum InfoHash { + V1(Vec), +} + +impl std::fmt::Display for InfoHash { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::V1(i) => write!( + f, + "{}", + i.iter().map(|b| format!("{b:02x}")).collect::() + ), + } + } +} diff --git a/src/main.rs b/src/main.rs index 65edb4b..d9cbc4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -98,6 +98,8 @@ async fn main() -> Result<()> { continue; } } { + // convert to string once + let i = i.to_string(); // is already indexed? if index.has(&i) { continue;