implement info-hash enum

This commit is contained in:
yggverse 2025-07-08 15:47:52 +03:00
parent b3bf89b6f7
commit dcbb4108fd
3 changed files with 23 additions and 8 deletions

View file

@ -1,8 +1,11 @@
mod info_hash;
use info_hash::InfoHash;
/// Parse infohash from the source filepath, /// 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 /// * return `None` if the `path` is not reachable
pub fn get(path: &str) -> Option<Vec<String>> { pub fn get(path: &str) -> Option<Vec<InfoHash>> {
use std::io::Read; use std::io::Read;
if path.contains("://") { if path.contains("://") {
todo!("URL sources yet not supported") todo!("URL sources yet not supported")
@ -16,12 +19,7 @@ pub fn get(path: &str) -> Option<Vec<String>> {
if l != L { if l != L {
break; break;
} }
r.push( r.push(InfoHash::V1(b[..l].to_vec()))
b[..l]
.iter()
.map(|i| format!("{i:02x}"))
.collect::<String>(),
)
} }
Some(r) Some(r)
} }

15
src/api/info_hash.rs Normal file
View file

@ -0,0 +1,15 @@
pub enum InfoHash {
V1(Vec<u8>),
}
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::<String>()
),
}
}
}

View file

@ -98,6 +98,8 @@ async fn main() -> Result<()> {
continue; continue;
} }
} { } {
// convert to string once
let i = i.to_string();
// is already indexed? // is already indexed?
if index.has(&i) { if index.has(&i) {
continue; continue;