Implemented handling of extra keys

This commit is contained in:
Fabian 2024-07-25 22:35:23 +02:00
parent 1c2c21ec98
commit 5ca91ed6cb

View File

@ -27,7 +27,7 @@ struct Torrent {
announce: String, announce: String,
info: TorrentInfo, info: TorrentInfo,
info_hash: [u8; 20], info_hash: [u8; 20],
extra: HashMap<String, String> _extra: HashMap<String, String>
} }
impl Torrent { impl Torrent {
@ -77,7 +77,7 @@ impl Torrent {
} }
} else if let Some(b_files) = info_dict.get(&ByteString::from_str("files")) { } else if let Some(b_files) = info_dict.get(&ByteString::from_str("files")) {
let Bencode::List(files_list) = b_files else { 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(); let mut files = Vec::new();
@ -124,12 +124,38 @@ impl Torrent {
let Bencode::Bytes(announce) = b_announce else { let Bencode::Bytes(announce) = b_announce else {
return Err(anyhow!("announce not a string")) 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 { let result = Self {
announce: announce.to_string()?, announce: announce.to_string()?,
info_hash: b_info.sha1(), info_hash: b_info.sha1(),
info, info,
// TODO _extra,
extra: HashMap::new(),
}; };
Ok(result) Ok(result)
} }