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"
dependencies = [
"clone_to_owned",
"hex 0.4.3",
"serde",
]

View file

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

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 {
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)
}
}

View file

@ -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();