1use std::fmt;
2use std::ops::Deref;
3use Argdata;
4use ArgdataRef;
5use ReadError;
6use Value;
7
8struct FmtError<T>(Result<T, ReadError>);
9
10impl<T: fmt::Debug> fmt::Debug for FmtError<T> {
11 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
12 match self.0 {
13 Ok(ref value) => value.fmt(f),
14 Err(ref err) => write!(f, "error(\"{:?}\")", err),
15 }
16 }
17}
18
19impl<'a, 'd> fmt::Debug for ArgdataRef<'a, 'd> {
20 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
21 self.deref().fmt(f)
22 }
23}
24
25impl<'a, 'd> fmt::Debug for Argdata<'d> + 'a {
26 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
27 FmtError(self.read()).fmt(f)
28 }
29}
30
31impl<'a, 'd> fmt::Debug for Value<'a, 'd> {
32 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
33 match self {
34 Value::Null => write!(f, "null"),
35 Value::Binary(val) => write!(f, "binary({:?})", val),
36 Value::Bool(val) => val.fmt(f),
37 Value::Fd(fd) => write!(f, "fd({})", fd.raw_encoded_number()),
38 Value::Float(val) => fmt::Debug::fmt(val, f),
39 Value::Int(val) => fmt::Debug::fmt(val, f),
40 Value::Str(val) => fmt::Debug::fmt(&FmtError(val.as_str().map_err(Into::into)), f),
41 Value::Timestamp(val) => write!(f, "timestamp({}, {})", val.sec, val.nsec),
42 Value::Map(val) => {
43 let it = val.map(|x| match x {
44 Ok((k, v)) => (FmtError(Ok(k)), FmtError(Ok(v))),
45 Err(e) => (FmtError(Err(e)), FmtError(Err(e))),
46 });
47 f.debug_map().entries(it).finish()
48 }
49 Value::Seq(val) => {
50 let it = val.map(FmtError);
51 f.debug_list().entries(it).finish()
52 }
53 }
54 }
55}
56
57#[test]
58fn debug_fmt() {
59 let argdata = ::encoded(
60 &b"\x06\x87\x08Hello\x00\x87\x08World\x00\x81\x02\x82\x02\x01\x86\x09\
61 \x70\xF1\x80\x29\x15\x84\x05\x58\xe5\xd9\x80\x83\x06\x80\x80"[..],
62 );
63
64 assert_eq!(
65 format!("{:?}", &argdata as &Argdata),
66 "{\"Hello\": \"World\", false: true, timestamp(485, 88045333): 5826009, null: {null: null}}"
67 );
68
69 let argdata = ::encoded(&b"\x07\x81\x02\x82\x02\x01\x80\x87\x08Hello\x00\x81\x06\x81\x07"[..]);
70
71 assert_eq!(
72 format!("{:?}", &argdata as &Argdata),
73 "[false, true, null, \"Hello\", {}, []]"
74 );
75}