Add ':cbor' hint for CBOR Diagnostic Notation (EDN) #916
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I often find myself sending CBOR (a format for standardized data interchange with embedded devices; binary, using a superset of JSON's data model) through defmt. CBOR comes with a human-readable format (that is a superset of JSON's serialization), which is designed with human usability and not embedded devices in mind. The diagnostic notation is exactly capable of round-tripping binary data to a human readable form and back to binary.
Currently, the best way to send CBOR over defmt is in
{=[u8]:02x}
, so that output such asa1, 04, 41, 2b
can be copy-pasted into the right side of https://cbor.me/, so that after pressing the right-to-left green button, it shows what that means:{4: h'2B'}
. DerivingFormat
on parsed items is an alternative, but it has severe downsides of decreasing severity:more reasons
Format
shows the structure of the parsed item, and manually implementing it to look like CBOR EDN again is manual work (and moreover, there may be situations in which one would want to see the struct's view rather than the original data view)This PR proposes a new hint,
:cbor
, to defmt. If the same data[a1, 04, 41, 2b]
is passed into{=[u8]:cbor}
, the output is then{4: h'2B'}
on display right away, which a developer can make better use of.Note that this is not trying to hard-code an exception to the design choice that we don't take types that then need host knowledge of the type: This is not a concrete type, but a format of data serialization. It can be used with serialized data from a large set of CBOR serializing crates, and a large set of protocols and their implementations in types, using a single mechanism.
Example code
Output:
PR status
This is a working draft, but a bit minimal in that
{=[u8]:cbor}
and not with{:cbor}
-- maybe that's OK, maybe I'd just need to find the right spot in the code to put the code in (which will then need refactoring into a dedicated function)I still ask for review now already because while I've seen some slight positive reaction on the Rust Embedded channel, I don't want to sink too much time in here if the project does not want to go this way at all.