Skip to content

Commit 0bb295c

Browse files
committed
Merge #245: Add prefix 0x to peer ID hex string
d51aae0 feat(tracker): [#164] add prefix 0x to peer ID hex string (Jose Celano) Pull request description: A peer id like this: ```rust peer::Id(*b"-qB00000000000000000") ``` has now this hex string representation: ```s 0x2d71423030303030303030303030303030303030 ``` with the `0x` prefix as suggested by @da2ce7 [here](#164 (comment)). Top commit has no ACKs. Tree-SHA512: 28d5522e14bf6e2159d7c0e3377a4b5d2242b45124d01403b50d32bf01e8c4e48eb8f86f1e1bbccea062ae2d48f7f2e3a9aa1c79dd9ae7593996c73e9dac3afa
2 parents 7206407 + d51aae0 commit 0bb295c

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/tracker/peer.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,13 @@ impl Id {
169169
pub fn to_hex_string(&self) -> Option<String> {
170170
let buff_size = self.0.len() * 2;
171171
let mut tmp: Vec<u8> = vec![0; buff_size];
172+
172173
binascii::bin2hex(&self.0, &mut tmp).unwrap();
173-
std::str::from_utf8(&tmp).ok().map(std::string::ToString::to_string)
174+
175+
match std::str::from_utf8(&tmp) {
176+
Ok(hex) => Some(format!("0x{hex}")),
177+
Err(_) => None,
178+
}
174179
}
175180

176181
#[must_use]
@@ -360,23 +365,23 @@ mod test {
360365
#[test]
361366
fn should_be_converted_to_hex_string() {
362367
let id = peer::Id(*b"-qB00000000000000000");
363-
assert_eq!(id.to_hex_string().unwrap(), "2d71423030303030303030303030303030303030");
368+
assert_eq!(id.to_hex_string().unwrap(), "0x2d71423030303030303030303030303030303030");
364369

365370
let id = peer::Id([
366371
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
367372
]);
368-
assert_eq!(id.to_hex_string().unwrap(), "009f9296009f9296009f9296009f9296009f9296");
373+
assert_eq!(id.to_hex_string().unwrap(), "0x009f9296009f9296009f9296009f9296009f9296");
369374
}
370375

371376
#[test]
372377
fn should_be_converted_into_string_type_using_the_hex_string_format() {
373378
let id = peer::Id(*b"-qB00000000000000000");
374-
assert_eq!(id.to_string(), "2d71423030303030303030303030303030303030");
379+
assert_eq!(id.to_string(), "0x2d71423030303030303030303030303030303030");
375380

376381
let id = peer::Id([
377382
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
378383
]);
379-
assert_eq!(id.to_string(), "009f9296009f9296009f9296009f9296009f9296");
384+
assert_eq!(id.to_string(), "0x009f9296009f9296009f9296009f9296009f9296");
380385
}
381386

382387
#[test]
@@ -390,6 +395,7 @@ mod test {
390395
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
391396

392397
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
398+
use serde_json::Value;
393399

394400
use crate::protocol::clock::{Current, Time};
395401
use crate::tracker::peer::{self, Peer};
@@ -406,12 +412,26 @@ mod test {
406412
event: AnnounceEvent::Started,
407413
};
408414

409-
let json_serialized_value = serde_json::to_string(&torrent_peer).unwrap();
415+
let raw_json = serde_json::to_string(&torrent_peer).unwrap();
416+
417+
let expected_raw_json = r#"
418+
{
419+
"peer_id": {
420+
"id": "0x2d71423030303030303030303030303030303030",
421+
"client": "qBittorrent"
422+
},
423+
"peer_addr":"126.0.0.1:8080",
424+
"updated":0,
425+
"uploaded":0,
426+
"downloaded":0,
427+
"left":0,
428+
"event":"Started"
429+
}
430+
"#;
410431

411432
assert_eq!(
412-
json_serialized_value,
413-
// todo: compare using pretty json format to improve readability
414-
r#"{"peer_id":{"id":"2d71423030303030303030303030303030303030","client":"qBittorrent"},"peer_addr":"126.0.0.1:8080","updated":0,"uploaded":0,"downloaded":0,"left":0,"event":"Started"}"#
433+
serde_json::from_str::<Value>(&raw_json).unwrap(),
434+
serde_json::from_str::<Value>(expected_raw_json).unwrap()
415435
);
416436
}
417437
}

0 commit comments

Comments
 (0)