Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement defmt::Format for Error #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ documentation = "https://docs.rs/esp-at-nal"
[dependencies]
atat = "0.17.0"
embedded-nal = "0.6.0"
defmt = { version = "0.3", optional = true }
nb = "1.0.0"
fugit = "0.3.6"
fugit-timer = "0.1.3"
Expand All @@ -30,6 +31,7 @@ serialport = { git = "https://github.com/dbrgn/serialport-rs", branch = "embedde

[features]
default = ["examples"]
defmt = ["dep:defmt", "atat/defmt"]

# Fail on warnings
strict = []
Expand Down
28 changes: 28 additions & 0 deletions src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,34 @@ pub enum Error {
TimerError,
}

#[cfg(feature = "defmt")]
impl defmt::Format for Error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any reasons against the use of derive?

#[cfg_attr(feature = "defmt", derive(defmt::Format))]

However, I have to admit that I have not looked at the output of cargo expand and of course there could be good reasons for a manual implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I simply wasn't aware that a derive macro exists for defmt 🙂 I'll test the output differences in my project sometime. (I haven't figured out how to convert defmt writes to a string on regular std-Rust on Linux...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems this is not possible currently: knurling-rs/defmt#463

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you had a chance to test the derive? If not, I'm also fine with the current implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, not yet. I'll try to take a look again soon.

fn format(&self, f: defmt::Formatter) {
match self {
Error::EnablingMultiConnectionsFailed(e) => {
defmt::write!(f, "Error::EnablingMultiConnectionsFailed({})", e)
}
Error::EnablingPassiveSocketModeFailed(e) => {
defmt::write!(f, "Error::EnablingPassiveSocketModeFailed({})", e)
}
Error::ConnectError(e) => defmt::write!(f, "Error::ConnectError({})", e),
Error::TransmissionStartFailed(e) => defmt::write!(f, "Error::TransmissionStartFailed({})", e),
Error::SendFailed(e) => defmt::write!(f, "Error::SendFailed({})", e),
Error::ReceiveFailed(e) => defmt::write!(f, "Error::ReceiveFailed({})", e),
Error::CloseError(e) => defmt::write!(f, "Error::CloseError({})", e),
Error::PartialSend => defmt::write!(f, "Error::PartialSend"),
Error::UnconfirmedSocketState => defmt::write!(f, "Error::UnconfirmedSocketState"),
Error::NoSocketAvailable => defmt::write!(f, "Error::NoSocketAvailable"),
Error::AlreadyConnected => defmt::write!(f, "Error::AlreadyConnected"),
Error::SocketUnconnected => defmt::write!(f, "Error::SocketUnconnected"),
Error::ClosingSocket => defmt::write!(f, "Error::ClosingSocket"),
Error::ReceiveOverflow => defmt::write!(f, "Error::ReceiveOverflow"),
Error::UnexpectedWouldBlock => defmt::write!(f, "Error::UnexpectedWouldBlock"),
Error::TimerError => defmt::write!(f, "Error::TimerError"),
}
}
}

impl<A: AtatClient, T: Timer<TIMER_HZ>, const TIMER_HZ: u32, const TX_SIZE: usize, const RX_SIZE: usize> TcpClientStack
for Adapter<A, T, TIMER_HZ, TX_SIZE, RX_SIZE>
{
Expand Down