coap_message_utils/debug.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
//! Helpers and implementations around the public [`show()`] wrapper.
use coap_message::{MessageOption as _, ReadableMessage};
pub struct ShowMessage<'a, M: ReadableMessage>(&'a M);
impl<'a, M: ReadableMessage> core::fmt::Debug for ShowMessage<'a, M> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// not used because unstable / see https://github.com/rust-lang/rust/issues/117729
// (and not complete yet for lack of options display)
#[cfg(any())] // aka. cfg(false)
f.debug_struct("ReadableMessage")
.field("type", &core::any::type_name::<M>())
.field("code", self.0.code().show_dotted_and_named())
.field_with("payload", |f| Ok(()))
.finish();
write!(
f,
"ReadableMessage {{\n type: {},\n code: {:?},\n options: {{\n ",
core::any::type_name::<M>(),
self.0.code().show_dotted_and_named(),
)?;
for (i, o) in self.0.options().enumerate() {
if i != 0 {
write!(f, ",\n ")?;
}
if let Some(name) = coap_numbers::option::to_name(o.number()) {
write!(f, "{} ({}): {:?}", name, o.number(), o.value())?;
} else {
write!(f, "{}: {:?}", o.number(), o.value())?;
}
}
write!(f, "\n }},\n payload: {:?}\n}}", self.0.payload())?;
Ok(())
}
}
#[cfg(feature = "defmt_0_3")]
impl<'a, M: ReadableMessage> defmt_0_3::Format for ShowMessage<'a, M> {
fn format(&self, f: defmt_0_3::Formatter) {
use defmt::write;
use defmt_0_3 as defmt;
write!(
f,
"ReadableMessage {{\n type: {=str},\n code: {},\n options: {{\n ",
core::any::type_name::<M>(),
self.0.code().show_dotted_and_named(),
);
for (i, o) in self.0.options().enumerate() {
if i != 0 {
write!(f, ",\n ");
}
if let Some(name) = coap_numbers::option::to_name(o.number()) {
write!(f, "{} ({}): {:?}", name, o.number(), o.value());
} else {
write!(f, "{}: {:?}", o.number(), o.value());
}
}
write!(f, "\n }},\n payload: {:?}\n}}", self.0.payload());
}
}
/// Extension trait providing utility functions on [coap_message::ReadableMessage].
pub trait ShowMessageExt: ReadableMessage + Sized {
/// Wraps the message to have a [`core::fmt::Debug`] imlementation, and also provide
/// [`defmt_0_3::Format`] if the `defmt_0_3` feature is selected.
///
/// ```
/// # use coap_message::MinimalWritableMessage;
/// # let mut message = coap_message_implementations::heap::HeapMessage::new();
/// message.set_code(coap_numbers::code::GET);
/// message.add_option_str(coap_numbers::option::URI_PATH, "hello");
///
/// use coap_message_utils::ShowMessageExt;
/// let shown = format!("{:?}", message.show());
/// // The precise format is not fixed, but it contains some usable details:
/// assert!(shown.contains("code: 0.01 GET"));
/// assert!(shown.contains("Uri-Path (11):"));
/// ```
fn show(&self) -> ShowMessage<'_, Self> {
ShowMessage(self)
}
}
impl<M: ReadableMessage> ShowMessageExt for M {}
pub struct ShowDotted(u8);
impl core::fmt::Debug for ShowDotted {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
coap_numbers::code::format_dotted(self.0, f)
}
}
#[cfg(feature = "defmt_0_3")]
impl defmt_0_3::Format for ShowDotted {
fn format(&self, f: defmt_0_3::Formatter) {
use defmt::write;
use defmt_0_3 as defmt;
write!(f, "{=u8}.{=u8:02}", self.0 >> 5, self.0 & 0x1f,)
}
}
pub struct ShowNamed(u8);
impl core::fmt::Debug for ShowNamed {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if let Some(name) = coap_numbers::code::to_name(self.0) {
write!(f, "{}", name)
} else {
write!(f, "{:?}", self.0.show_dotted())
}
}
}
#[cfg(feature = "defmt_0_3")]
impl defmt_0_3::Format for ShowNamed {
fn format(&self, f: defmt_0_3::Formatter) {
use defmt::write;
use defmt_0_3 as defmt;
if let Some(name) = coap_numbers::code::to_name(self.0) {
write!(f, "{}", name)
} else {
write!(f, "{:?}", self.0.show_dotted())
}
}
}
pub struct ShowDottedAndNamed(u8);
impl core::fmt::Debug for ShowDottedAndNamed {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if let Some(name) = coap_numbers::code::to_name(self.0) {
write!(f, "{:?} {}", self.0.show_dotted(), name)
} else {
write!(f, "{:?}", self.0.show_dotted())
}
}
}
#[cfg(feature = "defmt_0_3")]
impl defmt_0_3::Format for ShowDottedAndNamed {
fn format(&self, f: defmt_0_3::Formatter) {
use defmt::write;
use defmt_0_3 as defmt;
if let Some(name) = coap_numbers::code::to_name(self.0) {
write!(f, "{:?} {=str}", self.0.show_dotted(), name)
} else {
write!(f, "{:?}", self.0.show_dotted())
}
}
}
/// Extension trait providing utility functions on [coap_message::Code].
pub trait ShowCodeExt: coap_message::Code {
/// Wraps the code to have a [`core::fmt::Debug`] imlementation that produces dotted format,
/// and also provides [`defmt_0_3::Format`] if the `defmt_0_3` feature is selected.
///
/// ```
/// use coap_message_utils::ShowCodeExt;
/// assert_eq!("2.05", format!("{:?}", coap_numbers::code::CONTENT.show_dotted()));
/// ```
fn show_dotted(self) -> ShowDotted {
let numeric: u8 = self.into();
ShowDotted(numeric)
}
/// Wraps the code to have a [`core::fmt::Debug`] imlementation that produces the code's name
/// (falling back to dotted format), and also provides [`defmt_0_3::Format`] if the `defmt_0_3`
/// feature is selected.
///
/// ```
/// use coap_message_utils::ShowCodeExt;
/// assert_eq!("Content", format!("{:?}", coap_numbers::code::CONTENT.show_named()));
/// assert_eq!("0.31", format!("{:?}", 31u8.show_named()));
/// ```
fn show_named(self) -> ShowNamed {
let numeric: u8 = self.into();
ShowNamed(numeric)
}
/// Wraps the code to have a [`core::fmt::Debug`] imlementation that produces the code's name
/// (falling back to dotted format), and also provides [`defmt_0_3::Format`] if the `defmt_0_3`
/// feature is selected.
///
/// ```
/// use coap_message_utils::ShowCodeExt;
/// assert_eq!("2.05 Content", format!("{:?}", coap_numbers::code::CONTENT.show_dotted_and_named()));
/// assert_eq!("0.31", format!("{:?}", 31u8.show_dotted_and_named()));
/// ```
fn show_dotted_and_named(self) -> ShowDottedAndNamed {
let numeric: u8 = self.into();
ShowDottedAndNamed(numeric)
}
}
impl<C: coap_message::Code> ShowCodeExt for C {}