Skip to content

Commit 2d5c387

Browse files
authored
defmt::Format implementations for types forgotten during the first pass (#70)
1 parent 1a686a9 commit 2d5c387

File tree

10 files changed

+198
-28
lines changed

10 files changed

+198
-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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ 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!(
91+
"Processing message with header: {:?}",
92+
debug2format!(message.header())
93+
);
9194

9295
let mut responseb = domain::base::MessageBuilder::from_target(buf)?;
9396

@@ -106,10 +109,17 @@ pub fn reply(
106109
Ttl::from_duration_lossy(ttl),
107110
A::from_octets(ip[0], ip[1], ip[2], ip[3]),
108111
);
109-
debug!("Answering {:?} with {:?}", question, record);
112+
debug!(
113+
"Answering {:?} with {:?}",
114+
debug2format!(question),
115+
debug2format!(record)
116+
);
110117
answerb.push(record)?;
111118
} else {
112-
debug!("Question {:?} is not of type A, not answering", question);
119+
debug!(
120+
"Question {:?} is not of type A, not answering",
121+
debug2format!(question)
122+
);
113123
}
114124
}
115125

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: 47 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,26 @@ 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),
489+
debug2format!(e)
485490
);
486491
break true;
487492
}
@@ -493,25 +498,32 @@ pub async fn handle_connection<H, T, const N: usize>(
493498

494499
match result {
495500
Err(HandlerError::Connection(Error::ConnectionClosed)) => {
496-
debug!("Handler task {}: Connection closed", task_id);
501+
debug!(
502+
"Handler task {}: Connection closed",
503+
display2format!(task_id)
504+
);
497505
break false;
498506
}
499507
Err(e) => {
500508
warn!(
501509
"Handler task {}: Error when handling request: {:?}",
502-
task_id, e
510+
display2format!(task_id),
511+
debug2format!(e)
503512
);
504513
break true;
505514
}
506515
Ok(needs_close) => {
507516
if needs_close {
508517
debug!(
509518
"Handler task {}: Request complete; closing connection",
510-
task_id
519+
display2format!(task_id)
511520
);
512521
break true;
513522
} else {
514-
debug!("Handler task {}: Request complete", task_id);
523+
debug!(
524+
"Handler task {}: Request complete",
525+
display2format!(task_id)
526+
);
515527
}
516528
}
517529
}
@@ -521,7 +533,8 @@ pub async fn handle_connection<H, T, const N: usize>(
521533
if let Err(e) = io.close(Close::Both).await {
522534
warn!(
523535
"Handler task {}: Error when closing the socket: {:?}",
524-
task_id, e
536+
display2format!(task_id),
537+
debug2format!(e)
525538
);
526539
}
527540
} else {
@@ -557,6 +570,20 @@ where
557570
}
558571
}
559572

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

695725
let io = {
696726
let _guard = mutex.lock().await;
697727

698728
acceptor.accept().await.map_err(Error::Io)?.1
699729
};
700730

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

703736
handle_connection::<_, _, N>(
704737
io,
@@ -715,7 +748,10 @@ impl<const P: usize, const B: usize, const N: usize> Server<P, B, N> {
715748

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

718-
warn!("Server processing loop quit abruptly: {:?}", result);
751+
warn!(
752+
"Server processing loop quit abruptly: {:?}",
753+
debug2format!(result)
754+
);
719755

720756
result
721757
}

0 commit comments

Comments
 (0)