rust-embedded-community / serde-json-core

`serde-json` for `no_std` programs
Apache License 2.0
161 stars 59 forks source link

Feature Request: Support defmt Formatting #75

Closed avlec closed 1 year ago

avlec commented 1 year ago

Working on a an embedded project where there is the ability to log errors and structures with defmt, this does require adding a new conditional dependency and adding a conditional derive as follows. This would be particularly useful for reporting errors from JSON de-serialization.

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
...
}
ryan-summers commented 1 year ago

I would be happy to review such a PR, and have for a few other crates, but I have used defmt yet personally (my projects all predate it), so I'm not one that can test the implementation

avlec commented 1 year ago

I did a quick little test locally and just adding it how I did with the error from JSON de-serialization helped me understand that the decoder yielded this error TrailingCharacters which quickly led me to understand I needed to properly size my slice to from_slice. Would there be anywhere else these messages might be useful?

ryan-summers commented 1 year ago

In general, the most useful approach I have seen is adding defmt::Format to the Error types, so for this library, it would be serde_json_core::ser::Error and serde_json_core::de::Error