Skip to content

Commit 182027e

Browse files
committed
defmt::Format implementations for types forgotten during the first pass
1 parent 1a686a9 commit 182027e

File tree

10 files changed

+176
-28
lines changed

10 files changed

+176
-28
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@ jobs:
4848
run: cargo build --no-default-features --features embassy,defmt
4949
- name: Build | Examples
5050
run: cargo build --examples --features log
51+
- name: Build | Examples - defmt
52+
run: export DEFMT_LOG=trace; cargo check --examples --features std,defmt

Cargo.toml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,47 @@ async-compat = "0.2" # For the `mqtt_client` example
5050

5151
[[example]]
5252
name = "captive_portal"
53-
required-features = ["std", "log"]
53+
required-features = ["std"]
5454

5555
[[example]]
5656
name = "dhcp_client"
57-
required-features = ["std", "log"]
57+
required-features = ["std"]
5858

5959
[[example]]
6060
name = "dhcp_server"
61-
required-features = ["std", "log"]
61+
required-features = ["std"]
6262

6363
[[example]]
6464
name = "http_client"
65-
required-features = ["std", "log"]
65+
required-features = ["std"]
6666

6767
[[example]]
6868
name = "http_server"
69-
required-features = ["std", "log"]
69+
required-features = ["std"]
7070

7171
[[example]]
7272
name = "mdns_responder"
73-
required-features = ["std", "log"]
73+
required-features = ["std"]
7474

7575
[[example]]
7676
name = "mdns_service_responder"
77-
required-features = ["std", "log"]
77+
required-features = ["std"]
7878

7979
[[example]]
8080
name = "ws_client"
81-
required-features = ["std", "log"]
81+
required-features = ["std"]
8282

8383
[[example]]
8484
name = "ws_server"
85-
required-features = ["std", "log"]
85+
required-features = ["std"]
8686

8787
[[example]]
8888
name = "nal_std"
89-
required-features = ["std", "log"]
89+
required-features = ["std"]
9090

9191
[[example]]
9292
name = "mqtt_client"
93-
required-features = ["std", "embedded-svc", "log"]
93+
required-features = ["std", "embedded-svc"]
9494

9595
[workspace]
9696
members = [

edge-captive/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn reply(
8787
let buf = Buf(buf, 0);
8888

8989
let message = domain::base::Message::from_octets(request)?;
90-
debug!("Processing message with header: {:?}", message.header());
90+
debug!("Processing message with header: {:?}", debug2format!(message.header()));
9191

9292
let mut responseb = domain::base::MessageBuilder::from_target(buf)?;
9393

@@ -106,10 +106,10 @@ pub fn reply(
106106
Ttl::from_duration_lossy(ttl),
107107
A::from_octets(ip[0], ip[1], ip[2], ip[3]),
108108
);
109-
debug!("Answering {:?} with {:?}", question, record);
109+
debug!("Answering {:?} with {:?}", debug2format!(question), debug2format!(record));
110110
answerb.push(record)?;
111111
} else {
112-
debug!("Question {:?} is not of type A, not answering", question);
112+
debug!("Question {:?} is not of type A, not answering", debug2format!(question));
113113
}
114114
}
115115

edge-http/src/io.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,29 @@ where
116116
}
117117
}
118118

119+
#[cfg(feature = "defmt")]
120+
impl<E> defmt::Format for Error<E>
121+
where
122+
E: defmt::Format,
123+
{
124+
fn format(&self, f: defmt::Formatter<'_>) {
125+
match self {
126+
Self::InvalidHeaders => defmt::write!(f, "Invalid HTTP headers or status line"),
127+
Self::InvalidBody => defmt::write!(f, "Invalid HTTP body"),
128+
Self::TooManyHeaders => defmt::write!(f, "Too many HTTP headers"),
129+
Self::TooLongHeaders => defmt::write!(f, "HTTP headers section is too long"),
130+
Self::TooLongBody => defmt::write!(f, "HTTP body is too long"),
131+
Self::IncompleteHeaders => defmt::write!(f, "HTTP headers section is incomplete"),
132+
Self::IncompleteBody => defmt::write!(f, "HTTP body is incomplete"),
133+
Self::InvalidState => defmt::write!(f, "Connection is not in requested state"),
134+
Self::HeadersMismatchError(e) => defmt::write!(f, "Headers mismatch: {}", e),
135+
Self::WsUpgradeError(e) => defmt::write!(f, "WebSocket upgrade error: {}", e),
136+
Self::ConnectionClosed => defmt::write!(f, "Connection closed"),
137+
Self::Io(e) => defmt::write!(f, "{}", e),
138+
}
139+
}
140+
}
141+
119142
#[cfg(feature = "std")]
120143
impl<E> std::error::Error for Error<E> where E: std::error::Error {}
121144

edge-http/src/io/server.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ where
332332
}
333333

334334
#[derive(Debug)]
335+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
335336
pub enum HandlerError<T, E> {
336337
Io(T),
337338
Connection(Error<T>),
@@ -466,22 +467,25 @@ pub async fn handle_connection<H, T, const N: usize>(
466467
T: Read + Write + Readable + TcpSplit + TcpShutdown,
467468
{
468469
let close = loop {
469-
debug!("Handler task {}: Waiting for a new request", task_id);
470+
debug!(
471+
"Handler task {}: Waiting for a new request",
472+
display2format!(task_id)
473+
);
470474

471475
if let Some(keepalive_timeout_ms) = keepalive_timeout_ms {
472476
let wait_data = with_timeout(keepalive_timeout_ms, io.readable()).await;
473477
match wait_data {
474478
Err(WithTimeoutError::Timeout) => {
475479
info!(
476480
"Handler task {}: Closing connection due to inactivity",
477-
task_id
481+
display2format!(task_id)
478482
);
479483
break true;
480484
}
481485
Err(e) => {
482486
warn!(
483487
"Handler task {}: Error when handling request: {:?}",
484-
task_id, e
488+
display2format!(task_id), debug2format!(e)
485489
);
486490
break true;
487491
}
@@ -493,25 +497,32 @@ pub async fn handle_connection<H, T, const N: usize>(
493497

494498
match result {
495499
Err(HandlerError::Connection(Error::ConnectionClosed)) => {
496-
debug!("Handler task {}: Connection closed", task_id);
500+
debug!(
501+
"Handler task {}: Connection closed",
502+
display2format!(task_id)
503+
);
497504
break false;
498505
}
499506
Err(e) => {
500507
warn!(
501508
"Handler task {}: Error when handling request: {:?}",
502-
task_id, e
509+
display2format!(task_id),
510+
debug2format!(e)
503511
);
504512
break true;
505513
}
506514
Ok(needs_close) => {
507515
if needs_close {
508516
debug!(
509517
"Handler task {}: Request complete; closing connection",
510-
task_id
518+
display2format!(task_id)
511519
);
512520
break true;
513521
} else {
514-
debug!("Handler task {}: Request complete", task_id);
522+
debug!(
523+
"Handler task {}: Request complete",
524+
display2format!(task_id)
525+
);
515526
}
516527
}
517528
}
@@ -521,7 +532,8 @@ pub async fn handle_connection<H, T, const N: usize>(
521532
if let Err(e) = io.close(Close::Both).await {
522533
warn!(
523534
"Handler task {}: Error when closing the socket: {:?}",
524-
task_id, e
535+
display2format!(task_id),
536+
debug2format!(e)
525537
);
526538
}
527539
} else {
@@ -557,6 +569,20 @@ where
557569
}
558570
}
559571

572+
#[cfg(feature = "defmt")]
573+
impl<C, E> defmt::Format for HandleRequestError<C, E>
574+
where
575+
C: defmt::Format,
576+
E: defmt::Format,
577+
{
578+
fn format(&self, f: defmt::Formatter<'_>) {
579+
match self {
580+
Self::Connection(e) => defmt::write!(f, "Connection error: {}", e),
581+
Self::Handler(e) => defmt::write!(f, "Handler error: {}", e),
582+
}
583+
}
584+
}
585+
560586
impl<C, E> embedded_io_async::Error for HandleRequestError<C, E>
561587
where
562588
C: Debug + embedded_io_async::Error,
@@ -690,15 +716,21 @@ impl<const P: usize, const B: usize, const N: usize> Server<P, B, N> {
690716
unwrap!(tasks
691717
.push(async move {
692718
loop {
693-
debug!("Handler task {}: Waiting for connection", task_id);
719+
debug!(
720+
"Handler task {}: Waiting for connection",
721+
display2format!(task_id)
722+
);
694723

695724
let io = {
696725
let _guard = mutex.lock().await;
697726

698727
acceptor.accept().await.map_err(Error::Io)?.1
699728
};
700729

701-
debug!("Handler task {}: Got connection request", task_id);
730+
debug!(
731+
"Handler task {}: Got connection request",
732+
display2format!(task_id)
733+
);
702734

703735
handle_connection::<_, _, N>(
704736
io,
@@ -715,7 +747,7 @@ impl<const P: usize, const B: usize, const N: usize> Server<P, B, N> {
715747

716748
let (result, _) = embassy_futures::select::select_slice(&mut tasks).await;
717749

718-
warn!("Server processing loop quit abruptly: {:?}", result);
750+
warn!("Server processing loop quit abruptly: {:?}", debug2format!(result));
719751

720752
result
721753
}

edge-http/src/lib.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ impl Display for HeadersMismatchError {
4545
}
4646
}
4747

48+
#[cfg(feature = "defmt")]
49+
impl defmt::Format for HeadersMismatchError {
50+
fn format(&self, f: defmt::Formatter<'_>) {
51+
match self {
52+
Self::ResponseConnectionTypeMismatchError => defmt::write!(
53+
f,
54+
"Response connection type is different from the request connection type"
55+
),
56+
Self::BodyTypeError(s) => defmt::write!(f, "Body type mismatch: {}", s),
57+
}
58+
}
59+
}
60+
4861
/// Http methods
4962
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5063
#[cfg_attr(feature = "std", derive(Hash))]
@@ -202,6 +215,13 @@ impl Display for Method {
202215
}
203216
}
204217

218+
#[cfg(feature = "defmt")]
219+
impl defmt::Format for Method {
220+
fn format(&self, f: defmt::Formatter<'_>) {
221+
defmt::write!(f, "{}", self.as_str())
222+
}
223+
}
224+
205225
/// HTTP headers
206226
#[derive(Debug)]
207227
pub struct Headers<'b, const N: usize = 64>([httparse::Header<'b>; N]);
@@ -565,6 +585,17 @@ impl Display for ConnectionType {
565585
}
566586
}
567587

588+
#[cfg(feature = "defmt")]
589+
impl defmt::Format for ConnectionType {
590+
fn format(&self, f: defmt::Formatter<'_>) {
591+
match self {
592+
Self::KeepAlive => defmt::write!(f, "Keep-Alive"),
593+
Self::Close => defmt::write!(f, "Close"),
594+
Self::Upgrade => defmt::write!(f, "Upgrade"),
595+
}
596+
}
597+
}
598+
568599
/// Body type
569600
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
570601
pub enum BodyType {
@@ -734,6 +765,17 @@ impl Display for BodyType {
734765
}
735766
}
736767

768+
#[cfg(feature = "defmt")]
769+
impl defmt::Format for BodyType {
770+
fn format(&self, f: defmt::Formatter<'_>) {
771+
match self {
772+
Self::Chunked => defmt::write!(f, "Chunked"),
773+
Self::ContentLen(len) => defmt::write!(f, "Content-Length: {}", len),
774+
Self::Raw => defmt::write!(f, "Raw"),
775+
}
776+
}
777+
}
778+
737779
/// Request headers including the request line (method, path)
738780
#[derive(Debug)]
739781
pub struct RequestHeaders<'b, const N: usize> {
@@ -790,6 +832,23 @@ impl<const N: usize> Display for RequestHeaders<'_, N> {
790832
}
791833
}
792834

835+
#[cfg(feature = "defmt")]
836+
impl<const N: usize> defmt::Format for RequestHeaders<'_, N> {
837+
fn format(&self, f: defmt::Formatter<'_>) {
838+
defmt::write!(f, "{} ", if self.http11 { "HTTP/1.1" } else { "HTTP/1.0" });
839+
840+
defmt::write!(f, "{} {}\n", self.method, self.path);
841+
842+
for (name, value) in self.headers.iter() {
843+
if name.is_empty() {
844+
break;
845+
}
846+
847+
defmt::write!(f, "{}: {}\n", name, value);
848+
}
849+
}
850+
}
851+
793852
/// Response headers including the response line (HTTP version, status code, reason phrase)
794853
#[derive(Debug)]
795854
pub struct ResponseHeaders<'b, const N: usize> {
@@ -851,6 +910,23 @@ impl<const N: usize> Display for ResponseHeaders<'_, N> {
851910
}
852911
}
853912

913+
#[cfg(feature = "defmt")]
914+
impl<const N: usize> defmt::Format for ResponseHeaders<'_, N> {
915+
fn format(&self, f: defmt::Formatter<'_>) {
916+
defmt::write!(f, "{} ", if self.http11 { "HTTP/1.1 " } else { "HTTP/1.0" });
917+
918+
defmt::write!(f, "{} {}\n", self.code, self.reason.unwrap_or(""));
919+
920+
for (name, value) in self.headers.iter() {
921+
if name.is_empty() {
922+
break;
923+
}
924+
925+
defmt::write!(f, "{}: {}\n", name, value);
926+
}
927+
}
928+
}
929+
854930
/// Websocket utilities
855931
pub mod ws {
856932
use crate::Method;
@@ -935,6 +1011,17 @@ pub mod ws {
9351011
}
9361012
}
9371013

1014+
#[cfg(feature = "defmt")]
1015+
impl defmt::Format for UpgradeError {
1016+
fn format(&self, f: defmt::Formatter<'_>) {
1017+
match self {
1018+
Self::NoVersion => defmt::write!(f, "No Sec-WebSocket-Version header"),
1019+
Self::NoSecKey => defmt::write!(f, "No Sec-WebSocket-Key header"),
1020+
Self::UnsupportedVersion => defmt::write!(f, "Unsupported Sec-WebSocket-Version"),
1021+
}
1022+
}
1023+
}
1024+
9381025
#[cfg(feature = "std")]
9391026
impl std::error::Error for UpgradeError {}
9401027

edge-mdns/src/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ where
360360
);
361361

362362
if let Err(err) = send.send(remote, data).await {
363-
warn!("Failed to reply privately to {}: {:?}", remote, err);
363+
warn!("Failed to reply privately to {}: {:?}", remote, debug2format!(err));
364364
}
365365
} else {
366366
// Otherwise, re-broadcast the response

0 commit comments

Comments
 (0)