bencode: implement debug on the inner structs
This commit is contained in:
@@ -59,30 +59,14 @@ impl fmt::Debug for Object {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Object::Int(num) => write!(f, "{}", num),
|
||||
Object::Bytes(ref bytes) => {
|
||||
match str::from_utf8(bytes) {
|
||||
Ok(s) => write!(f, "{}", s),
|
||||
Err(_) => {
|
||||
for &b in bytes.iter() {
|
||||
write!(f, "{:X}", b)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
Object::List(ref list) => {
|
||||
f.debug_list().entries(list.iter()).finish()
|
||||
}
|
||||
Object::Dict(ref dict) => {
|
||||
f.debug_map()
|
||||
.entries(dict.iter().map(|(k, v)| (k.clone(), v)))
|
||||
.finish()
|
||||
}
|
||||
Object::Bytes(ref bytes) => bytes.fmt(f),
|
||||
Object::List(ref list) => list.fmt(f),
|
||||
Object::Dict(ref dict) => dict.fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
|
||||
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)]
|
||||
pub struct Bytes(Vec<u8>);
|
||||
|
||||
impl Bytes {
|
||||
@@ -113,7 +97,23 @@ impl DerefMut for Bytes {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
impl fmt::Debug for Bytes {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match str::from_utf8(&self.0) {
|
||||
Ok(s) => write!(f, "\"{}\"", s),
|
||||
Err(_) => {
|
||||
write!(f, "\"")?;
|
||||
for &b in self.0.iter() {
|
||||
write!(f, "{:x}", b)?;
|
||||
}
|
||||
write!(f, "\"")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub struct List(Vec<Object>);
|
||||
|
||||
impl List {
|
||||
@@ -140,7 +140,13 @@ impl DerefMut for List {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
impl fmt::Debug for List {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_list().entries(self.iter()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub struct Dict(BTreeMap<Bytes, Object>);
|
||||
|
||||
impl Dict {
|
||||
@@ -195,6 +201,14 @@ impl DerefMut for Dict {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Dict {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_map()
|
||||
.entries(self.iter())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
// convertion utilities
|
||||
|
||||
// int
|
||||
|
||||
Reference in New Issue
Block a user