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 all commits
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
18 changes: 15 additions & 3 deletions .github/workflows/qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ jobs:
RUSTFLAGS: -D warnings
run: cargo test

- name: Build default features
- name: Build in strict mode with default features
run: cargo build --release --features strict

- name: Build in strict mode with log feature
Copy link
Member

Choose a reason for hiding this comment

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

My first intuition would have been to implement the features as a matrix axis. But that would have made too much overhead, and so it is nice and lean. 👍

Copy link
Contributor Author

@dbrgn dbrgn Oct 27, 2022

Choose a reason for hiding this comment

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

Yeah, I think a matrix makes sense if we have even more distinct features, but for now this is probably the simplest and fastest way (because build cache is shared).

run: cargo build --release --features strict,log

- name: Build in strict mode with defmt feature
run: cargo build --release --features strict,defmt

no_std_builds:
name: Build no_std targets
runs-on: ubuntu-latest
Expand Down Expand Up @@ -120,7 +126,7 @@ jobs:
uses: Swatinem/rust-cache@v1

- name: Check documentation
run: cargo rustdoc --all-features -- -D warnings
run: cargo rustdoc -- -D warnings

clippy:
name: Linting
Expand All @@ -139,4 +145,10 @@ jobs:
uses: Swatinem/rust-cache@v1

- name: Clippy
run: cargo clippy --all-targets --all-features -- -D warnings
run: cargo clippy --all-targets -- -D warnings

- name: Clippy (with log feature)
run: cargo clippy --all-targets --features log -- -D warnings

- name: Clippy (with defmt feature)
run: cargo clippy --all-targets --features defmt -- -D warnings
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