From 5ca91ed6cbf45ef89238fcd56f4d0944899bcd25 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Thu, 25 Jul 2024 22:35:23 +0200 Subject: [PATCH] Implemented handling of extra keys --- src/main.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 87ca417..6f65ce7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ struct Torrent { announce: String, info: TorrentInfo, info_hash: [u8; 20], - extra: HashMap + _extra: HashMap } impl Torrent { @@ -77,7 +77,7 @@ impl Torrent { } } else if let Some(b_files) = info_dict.get(&ByteString::from_str("files")) { let Bencode::List(files_list) = b_files else { - return Err(anyhow!("files not an list")) + return Err(anyhow!("files not a list")) }; let mut files = Vec::new(); @@ -124,12 +124,38 @@ impl Torrent { let Bencode::Bytes(announce) = b_announce else { return Err(anyhow!("announce not a string")) }; + + let mut _extra = HashMap::new(); + for (k, v) in decoded { + if k == &ByteString::from_str("announce") || k == &ByteString::from_str("info") { + continue + } + let Ok(k_str) = k.to_string() else { + println!("Ignoring non-UTF-8 key {k:?}"); + continue; + }; + match v { + Bencode::Integer(i) => { + _extra.insert(k_str, format!("{}", i)); + } + Bencode::Bytes(s) => { + let Ok(s_str) = s.to_string() else { + println!("Ignoring non-UTF-8 value of key {k_str}"); + continue; + }; + _extra.insert(k_str, s_str); + } + _ => { + println!("Ignoring extra of type list or dict") + } + } + } + let result = Self { announce: announce.to_string()?, info_hash: b_info.sha1(), info, - // TODO - extra: HashMap::new(), + _extra, }; Ok(result) }