toml-rs / toml

Rust TOML Parser
https://docs.rs/toml
Apache License 2.0
725 stars 107 forks source link

Add support for outputting integers as hexadecimal #812

Open MatthewMorozov opened 2 hours ago

MatthewMorozov commented 2 hours ago

When using serde to serialize a data structure containing integers (u32, i16, etc) to TOML format, I would like the fields to be written in the generated TOML file in hexadecimal format. TOML format supports hexadecimal numbers, and this feature would make it easier to read fields such as 32-bit addresses. This may could be an option such as #[serde_with_format = "{:0X}"] above integer-type fields.

epage commented 2 hours ago

Some prior discussion at #781.

I don't see us supporting customization of formatting through serde. Serde doesn't natively support it. To support this, we'd need a newtype that serializes to a struct that toml recognizes as an integer with formatting. We'd support deserializing from either the struct or a number. This has some odd cases in serde, particularly that its not compatible with any other serialization format. Not just that the formatting gets ignored but that the number won't be sererialized correctly.

We can support this in toml_edit and people could deserialize to a DocumentMut and then apply a custom formatter. We'd just need to work out the API for customizing an integer's repr.