From 0fcedd3656d251fb026e85c21dfabf32b532d0e6 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 1 Jul 2022 16:03:15 -0700 Subject: [PATCH] Improve packet debug output --- packet-inspector/src/main.rs | 4 ++-- src/protocol/packets.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/packet-inspector/src/main.rs b/packet-inspector/src/main.rs index 7066f4d..a0bd085 100644 --- a/packet-inspector/src/main.rs +++ b/packet-inspector/src/main.rs @@ -2,10 +2,10 @@ use std::error::Error; use std::fmt; use std::net::SocketAddr; use std::sync::Arc; -use std::time::{Duration, Instant}; +use std::time::Duration; use anyhow::bail; -use chrono::{Utc, DateTime}; +use chrono::{DateTime, Utc}; use clap::Parser; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; diff --git a/src/protocol/packets.rs b/src/protocol/packets.rs index 781f52a..ec9d4b5 100644 --- a/src/protocol/packets.rs +++ b/src/protocol/packets.rs @@ -264,8 +264,7 @@ macro_rules! def_bitfield { ),* $(,)? } ) => { - // TODO: custom Debug impl. - #[derive(Clone, Copy, PartialEq, Eq, Debug)] + #[derive(Clone, Copy, PartialEq, Eq)] $(#[$struct_attrs])* pub struct $name($inner_ty); @@ -307,6 +306,18 @@ macro_rules! def_bitfield { } } + impl fmt::Debug for $name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut s = f.debug_struct(stringify!($name)); + paste! { + $( + s.field(stringify!($bit), &self. []()); + )* + } + s.finish() + } + } + impl $crate::protocol::Encode for $name { fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> { self.0.encode(w) @@ -328,7 +339,7 @@ macro_rules! def_packet_group { $($packet:ident),* $(,)? } ) => { - #[derive(Clone, Debug)] + #[derive(Clone)] $(#[$attrs])* pub enum $group_name { $($packet($packet)),* @@ -378,6 +389,18 @@ macro_rules! def_packet_group { } } } + + impl fmt::Debug for $group_name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut t = f.debug_tuple(stringify!($group_name)); + match self { + $( + Self::$packet(pkt) => t.field(pkt), + )* + }; + t.finish() + } + } } }