-
Notifications
You must be signed in to change notification settings - Fork 116
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 support for outputting integers as hexadecimal #812
Comments
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 We can support this in |
In #781, I mention the following API ideas
#837 was posted with
Should we also allow reflecting on what the current style is? |
Hi. I submitted #837 and would like to offer my opinions on the matter:
As for the API design itself, I have little to offer. In #837 I chose to add Personally, I would love to see that hexadecimal/octal/binary numbers are supported first, and then try to come up with a solution for the more complex formatting decisions (digit separators, single/double-quote strings, etc.). Getting a perfect solution should not preclude a workable one. |
In your examples, they are always grouped by a standard convention. For example, hex is usually grouped by 4s while decimal is usually grouped by 3s, starting from the right. This could easily be handled by just having a "group by" option. This can trivially be implemented by post-processing a string (starting from the back, insert a
Yes but how do you select more than one? If I say "group in 4's" and then say "format as hex", the latter will overwrite the first. If we are ok with being order dependent, then we could re-parse the repr, check the base, and then handle it. This isn't about being perfect but whether the design scales. |
The Indian numbering system (mentioned in TOML 1.0 spec!) groups the rightmost three digits, then each two digits afterwards. Likewise, when representing USD in number of cents, one may wish to group the two rightmost digits, then each three digits afterwards.
The point is, coming up with a solution that satisfies enough users is difficult.
I don't understand what you mean by "overwrite"? I was under the impression that Continuing the let formatter = IntegerFormat::new()
.hex_upper() // or hex_lower(), oct(), bin()
.separator( /* ... */ )
.other_options_we_may_add_later();
let v: Value = formatter.try_format(123i64)?;
let formatter = StringFormat::new()
.basic() // or basic_multiline(), literal(), literal_multiline()
.other_options_we_may_add_later();
let v: Value = formatter.try_format("Hello,\nworld!")?; |
Decor is mutable and the Repr can be as well, to a limited degree. The main constraint is keeping the Even if you treat it as "visibly immutable", that leads to builder methods like |
I see. In which case a builder would still work and avoid internal mutation of the repr. To clarify, by "builder" I meant a builder for a // Note: I am not calling methods on Value itself,
// but rather the IntegerFormat type.
let formatter = IntegerFormat::new()
.hex_upper()
.separator( /* ... */ )
.other_options_we_may_add_later();
// Generate value and repr in single step.
// no need to worry about order of operations.
let v: Value = formatter.try_format(123i64)?; // formatted as 0x7B |
I don't see the formatter being a factory for The most likely API I see is I can also see having a chained One question is how do we report that a format didn't apply.
|
It might be better to incrementally build a separate let mut f = Formatted::new(123i64);
let opts = IntegerFormatOptions::new()
.to_hex_lower()
.separator(/* ... */);
f.format(opts)?;
Is there precedent for a IMO the only scenarios I can think of where formatting would fail are:
Regarding your earlier question:
I feel we could add those separately from the formatting API. For now, the user can directly examine the current repr with |
I'd just call that
"failure" is relative. People might be fine with a best effort. This wouldn't be too different than |
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.The text was updated successfully, but these errors were encountered: