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

Add attributes for Format derive to use Debug2Format on specific fields #662

Merged
merged 8 commits into from
Feb 22, 2022

Conversation

mattico
Copy link
Contributor

@mattico mattico commented Feb 17, 2022

I often have structs or enums where most fields impl Format, but a few third-party types do not. It is a pain to have to manually impl Format for the whole type when only one field doesn't work. I added an attribute that lets you override specific fields to use Debug2Format or Display2Format like so:

#[derive(defmt::Format)]
pub enum Error {
    Serial(serial::Error),
    Timeout(us_timer::TimeoutError),
    Serde(#[defmt(Debug2Format)] serde_json::Error),
    ResponseTooLarge,
}

@mattico mattico force-pushed the debug-display-attr-derive branch 3 times, most recently from cddcb3e to 92a4b69 Compare February 17, 2022 18:18
@Urhengulas
Copy link
Member

Looks really good! Thank you @mattico!

@Urhengulas
Copy link
Member

Since I am not super proficient with macros I'd like @jonas-schievink to take a quick look before merging.

@jonas-schievink
Copy link
Contributor

Great to see this, thanks!

bors r+

@bors
Copy link
Contributor

bors bot commented Feb 22, 2022

Build succeeded:

@bors bors bot merged commit e64138f into knurling-rs:main Feb 22, 2022

if let Some(FormatOption::Debug2Format) = format_opt {
stmts.push(quote!(defmt::export::fmt(&defmt::Debug2Format(&#ident))));
} else if let Some(FormatOption::Display2Format) = format_opt {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose these didn't need to be if lets

Copy link
Member

Choose a reason for hiding this comment

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

True, either normal if-else or match might be better suited.

@mattico mattico deleted the debug-display-attr-derive branch February 23, 2022 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants