fix c string formatting
This commit is contained in:
parent
b5d2a63909
commit
15672536f6
@ -1,4 +1,4 @@
|
|||||||
use std::fmt::Debug;
|
use std::{ffi::CStr, fmt::Debug};
|
||||||
|
|
||||||
use bytemuck::{Pod, Zeroable};
|
use bytemuck::{Pod, Zeroable};
|
||||||
use eyre::eyre;
|
use eyre::eyre;
|
||||||
@ -85,18 +85,18 @@ impl Debug for Packet {
|
|||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let data = &self.data;
|
let data = &self.data;
|
||||||
|
|
||||||
let str_data = std::str::from_utf8(&data[..data.len().saturating_sub(1)]).ok();
|
let mut debugger = f.debug_struct("Packet");
|
||||||
|
|
||||||
let data = if let Some(str_data) = str_data.as_ref() {
|
debugger.field("kind", &PacketKind::from_u8(self.header.kind));
|
||||||
str_data as &dyn Debug
|
|
||||||
|
let c_str = CStr::from_bytes_until_nul(data).ok();
|
||||||
|
if let Some(str_data) = c_str.as_ref().and_then(|x| x.to_str().ok()) {
|
||||||
|
debugger.field("data", &str_data);
|
||||||
} else {
|
} else {
|
||||||
&data as &dyn Debug
|
debugger.field("data", &data);
|
||||||
};
|
}
|
||||||
|
|
||||||
f.debug_struct("Packet")
|
debugger.finish()
|
||||||
.field("kind", &PacketKind::from_u8(self.header.kind))
|
|
||||||
.field("data", &data)
|
|
||||||
.finish()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user