Skip to content

Commit 86f64ff

Browse files
committed
Improve debug output for Number and Value
- Add Display impl for Number - Add specialized Debug impl for Value
1 parent 0c3c48b commit 86f64ff

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/value.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Represents valid Bencode data.
22
33
use crate::error::Error;
4+
use core::fmt::Display;
45
use serde::{
56
de::{Deserialize, DeserializeOwned, MapAccess, SeqAccess, Visitor},
67
ser::Serialize,
@@ -9,6 +10,7 @@ use serde_bytes::ByteBuf;
910

1011
#[cfg(all(feature = "alloc", not(feature = "std")))]
1112
use alloc::{collections::BTreeMap, fmt, str, str::FromStr, string::String, vec::Vec};
13+
1214
#[cfg(feature = "std")]
1315
use std::{collections::BTreeMap, fmt, str, str::FromStr, string::String, vec::Vec};
1416

@@ -21,6 +23,15 @@ pub enum Number {
2123
Unsigned(u64),
2224
}
2325

26+
impl Display for Number {
27+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28+
match self {
29+
Number::Signed(arg0) => fmt::Display::fmt(arg0, f),
30+
Number::Unsigned(arg0) => fmt::Display::fmt(arg0, f),
31+
}
32+
}
33+
}
34+
2435
impl From<isize> for Number {
2536
fn from(value: isize) -> Self {
2637
Number::Signed(value as i64)
@@ -85,7 +96,7 @@ impl From<u8> for Number {
8596
///
8697
/// It is useful when it is unknown what the data may contain (e.g. when different kinds of
8798
/// messages can be received in a network packet).
88-
#[derive(Clone, Debug, PartialEq)]
99+
#[derive(Clone, PartialEq)]
89100
pub enum Value {
90101
/// A byte string.
91102
///
@@ -243,6 +254,42 @@ impl Value {
243254
}
244255
}
245256

257+
impl fmt::Debug for Value {
258+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
259+
struct DebugByteStr<'a>(&'a ByteBuf);
260+
261+
impl<'a> fmt::Debug for DebugByteStr<'a> {
262+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
263+
match core::str::from_utf8(self.0) {
264+
Ok(key) => f.debug_tuple("ByteStr").field(&key).finish(),
265+
Err(_) => f.debug_tuple("ByteStr").field(&self.0).finish(),
266+
}
267+
}
268+
}
269+
270+
match self {
271+
Value::ByteStr(arg0) => fmt::Debug::fmt(&DebugByteStr(arg0), f),
272+
Value::Int(arg0) => f.debug_tuple("Int").field(arg0).finish(),
273+
Value::List(arg0) => f.debug_tuple("List").field(arg0).finish(),
274+
Value::Dict(arg0) => {
275+
struct DebugDict<'a>(&'a BTreeMap<ByteBuf, Value>);
276+
277+
impl<'a> fmt::Debug for DebugDict<'a> {
278+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
279+
let mut d = &mut f.debug_map();
280+
for (key, value) in self.0 {
281+
d = d.entry(&DebugByteStr(key), value);
282+
}
283+
d.finish()
284+
}
285+
}
286+
287+
f.debug_tuple("Dict").field(&DebugDict(arg0)).finish()
288+
}
289+
}
290+
}
291+
}
292+
246293
impl From<i8> for Value {
247294
fn from(other: i8) -> Value {
248295
Value::Int(Number::from(other))

0 commit comments

Comments
 (0)