From 7d7d82ac6d52590db768d2183694865ea064c6c6 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Fri, 9 Jul 2021 23:18:59 +0100 Subject: [PATCH] Nothing, just fixed tests and updated buffers debugging --- Cargo.lock | 1 - crates/buffers/Cargo.toml | 3 +- crates/buffers/src/lib.rs | 44 +++++++++++++------- crates/librqbit_core/src/torrent_metainfo.rs | 11 +++-- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5ff8380..e000004 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,7 +122,6 @@ name = "buffers" version = "0.1.0" dependencies = [ "clone_to_owned", - "hex 0.4.3", "serde", ] diff --git a/crates/buffers/Cargo.toml b/crates/buffers/Cargo.toml index 16c4505..d4f332b 100644 --- a/crates/buffers/Cargo.toml +++ b/crates/buffers/Cargo.toml @@ -7,5 +7,4 @@ edition = "2018" [dependencies] serde = {version = "1", features=["derive"]} -clone_to_owned = {path="../clone_to_owned"} -hex = "0.4" \ No newline at end of file +clone_to_owned = {path="../clone_to_owned"} \ No newline at end of file diff --git a/crates/buffers/src/lib.rs b/crates/buffers/src/lib.rs index 18d0874..007f8fa 100644 --- a/crates/buffers/src/lib.rs +++ b/crates/buffers/src/lib.rs @@ -25,47 +25,63 @@ impl<'a> ByteBufT for ByteBuf<'a> { } } -fn debug_bytes(b: &[u8], f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - if b.iter().all(|b| *b == 0) { - return write!(f, "<{} bytes, all zeroes>", b.len()); - } - match std::str::from_utf8(b) { - Ok(s) => write!(f, "{:?}", s), - Err(_e) => write!(f, "<{} bytes>", b.len()), +struct HexBytes<'a>(&'a [u8]); +impl<'a> std::fmt::Display for HexBytes<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for byte in self.0 { + write!(f, "{:02x?}", byte)?; + } + Ok(()) } } -fn display_bytes(b: &[u8], f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +fn debug_bytes(b: &[u8], f: &mut std::fmt::Formatter<'_>, debug_strings: bool) -> std::fmt::Result { if b.iter().all(|b| *b == 0) { return write!(f, "<{} bytes, all zeroes>", b.len()); } match std::str::from_utf8(b) { - Ok(s) => write!(f, "{}", s), - Err(_e) => write!(f, "<{} bytes>", b.len()), + Ok(s) => { + // A test if all chars are "printable". + if s.chars().all(|c| c.escape_debug().len() == 1) { + if debug_strings { + return write!(f, "{:?}", s); + } else { + return write!(f, "{}", s); + } + } + } + Err(_e) => {} + }; + + // up to 20 bytes, display hex + if b.len() <= 20 { + return write!(f, "<{} bytes, 0x{}>", b.len(), HexBytes(b)); } + + write!(f, "<{} bytes>", b.len()) } impl<'a> std::fmt::Debug for ByteBuf<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - debug_bytes(self.0, f) + debug_bytes(self.0, f, true) } } impl<'a> std::fmt::Display for ByteBuf<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - display_bytes(self.0, f) + debug_bytes(self.0, f, false) } } impl std::fmt::Debug for ByteString { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - debug_bytes(&self.0, f) + debug_bytes(&self.0, f, true) } } impl std::fmt::Display for ByteString { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - display_bytes(&self.0, f) + debug_bytes(&self.0, f, false) } } diff --git a/crates/librqbit_core/src/torrent_metainfo.rs b/crates/librqbit_core/src/torrent_metainfo.rs index 33b270e..19e3d72 100644 --- a/crates/librqbit_core/src/torrent_metainfo.rs +++ b/crates/librqbit_core/src/torrent_metainfo.rs @@ -256,11 +256,12 @@ mod tests { use super::*; + const TORRENT_FILENAME: &str = "../librqbit/resources/ubuntu-21.04-desktop-amd64.iso.torrent"; + #[test] fn test_deserialize_torrent_owned() { let mut buf = Vec::new(); - let filename = "resources/ubuntu-21.04-desktop-amd64.iso.torrent"; - std::fs::File::open(filename) + std::fs::File::open(TORRENT_FILENAME) .unwrap() .read_to_end(&mut buf) .unwrap(); @@ -272,8 +273,7 @@ mod tests { #[test] fn test_deserialize_torrent_borrowed() { let mut buf = Vec::new(); - let filename = "resources/ubuntu-21.04-desktop-amd64.iso.torrent"; - std::fs::File::open(filename) + std::fs::File::open(TORRENT_FILENAME) .unwrap() .read_to_end(&mut buf) .unwrap(); @@ -285,8 +285,7 @@ mod tests { #[test] fn test_deserialize_torrent_with_info_hash() { let mut buf = Vec::new(); - let filename = "resources/ubuntu-21.04-desktop-amd64.iso.torrent"; - std::fs::File::open(filename) + std::fs::File::open(TORRENT_FILENAME) .unwrap() .read_to_end(&mut buf) .unwrap();