Nothing, just fixed tests and updated buffers debugging

This commit is contained in:
Igor Katson 2021-07-09 23:18:59 +01:00
parent 7a947ddf79
commit 7d7d82ac6d
4 changed files with 36 additions and 23 deletions

1
Cargo.lock generated
View file

@ -122,7 +122,6 @@ name = "buffers"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clone_to_owned", "clone_to_owned",
"hex 0.4.3",
"serde", "serde",
] ]

View file

@ -8,4 +8,3 @@ edition = "2018"
[dependencies] [dependencies]
serde = {version = "1", features=["derive"]} serde = {version = "1", features=["derive"]}
clone_to_owned = {path="../clone_to_owned"} clone_to_owned = {path="../clone_to_owned"}
hex = "0.4"

View file

@ -25,47 +25,63 @@ impl<'a> ByteBufT for ByteBuf<'a> {
} }
} }
fn debug_bytes(b: &[u8], f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { struct HexBytes<'a>(&'a [u8]);
if b.iter().all(|b| *b == 0) { impl<'a> std::fmt::Display for HexBytes<'a> {
return write!(f, "<{} bytes, all zeroes>", b.len()); fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
} for byte in self.0 {
match std::str::from_utf8(b) { write!(f, "{:02x?}", byte)?;
Ok(s) => write!(f, "{:?}", s), }
Err(_e) => write!(f, "<{} bytes>", b.len()), 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) { if b.iter().all(|b| *b == 0) {
return write!(f, "<{} bytes, all zeroes>", b.len()); return write!(f, "<{} bytes, all zeroes>", b.len());
} }
match std::str::from_utf8(b) { match std::str::from_utf8(b) {
Ok(s) => write!(f, "{}", s), Ok(s) => {
Err(_e) => write!(f, "<{} bytes>", b.len()), // 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> { impl<'a> std::fmt::Debug for ByteBuf<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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> { impl<'a> std::fmt::Display for ByteBuf<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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 { impl std::fmt::Debug for ByteString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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 { impl std::fmt::Display for ByteString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
display_bytes(&self.0, f) debug_bytes(&self.0, f, false)
} }
} }

View file

@ -256,11 +256,12 @@ mod tests {
use super::*; use super::*;
const TORRENT_FILENAME: &str = "../librqbit/resources/ubuntu-21.04-desktop-amd64.iso.torrent";
#[test] #[test]
fn test_deserialize_torrent_owned() { fn test_deserialize_torrent_owned() {
let mut buf = Vec::new(); let mut buf = Vec::new();
let filename = "resources/ubuntu-21.04-desktop-amd64.iso.torrent"; std::fs::File::open(TORRENT_FILENAME)
std::fs::File::open(filename)
.unwrap() .unwrap()
.read_to_end(&mut buf) .read_to_end(&mut buf)
.unwrap(); .unwrap();
@ -272,8 +273,7 @@ mod tests {
#[test] #[test]
fn test_deserialize_torrent_borrowed() { fn test_deserialize_torrent_borrowed() {
let mut buf = Vec::new(); let mut buf = Vec::new();
let filename = "resources/ubuntu-21.04-desktop-amd64.iso.torrent"; std::fs::File::open(TORRENT_FILENAME)
std::fs::File::open(filename)
.unwrap() .unwrap()
.read_to_end(&mut buf) .read_to_end(&mut buf)
.unwrap(); .unwrap();
@ -285,8 +285,7 @@ mod tests {
#[test] #[test]
fn test_deserialize_torrent_with_info_hash() { fn test_deserialize_torrent_with_info_hash() {
let mut buf = Vec::new(); let mut buf = Vec::new();
let filename = "resources/ubuntu-21.04-desktop-amd64.iso.torrent"; std::fs::File::open(TORRENT_FILENAME)
std::fs::File::open(filename)
.unwrap() .unwrap()
.read_to_end(&mut buf) .read_to_end(&mut buf)
.unwrap(); .unwrap();