This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
Support emitting comments, blocks and alternate integer encodings in serialized output #234
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.
This PR represents a first attempt at allowing serde-yaml to emit comments, multi-line string blocks and alternate integer encodings in serialized output.
Why might you want this
I have a number of tools in which I want to emit human & machine readable outputs. Some of the outputs represent data from hardware devices (e.g. SPI eeproms) or low-level information about hardware (CPU addresses, register values). For human readability, it is often more convenient represent these integers as hex or binary.
Furthermore, since some of my tools are diagnostic in nature, it is convenient to have comments which would either show the breakdown of register bitfields or to tell the user what a particular value should be: ie: the
signature
field of a SPI eeprom's SFDP header should be the value0x50444653
(aka'SFDP'
). See the samples intests/test_format.rs
.The implementation
In order to accomplish this, I have some as-yet uncommitted changes to the rust-yaml library which permit emitting comments and multi-line string blocks.
Within the serde-yaml library, I've added a procedural macro which allows the user to derive
YamlFormat
on a struct and specify attributes which inform the serializer about the preferred output forms (e.g.#[yaml(format=hex, comment="CPU Address")]
).The generated code uses the
inventory
crate to remember the type-ids of every struct for whichYamlFormat
is derived. During serialization, the type-id of each struct is looked up and if the struct hasYamlFormat
, it is queried for preferred output forms and comments and the serializer formats the output accordingly.Concerns
This is my first go at this sort of thing.
thiserror
and some of your other work.'static
andstd::any::TypeId
(ie: why should TypeId care about 'static?) and have written my own type-id function based on this example.Yaml
enum.