ron-rs / ron

Rusty Object Notation
https://docs.rs/ron
Apache License 2.0
3.31k stars 122 forks source link

Add number suffixes and allow more number underscores #481

Closed juntyr closed 1 year ago

juntyr commented 1 year ago

Fixes #241

This PR implements optional number type suffixes, which can be turned on with the PrettyConfig::number_suffixes option and are allowed during parsing. To allow Rusty suffixes to work with floating literals, this PR also finally allows using underscores in float literals, using the Rust syntax as a reference.

This PR turned out to be significantly more work than I had anticipated. In particular, a number which could fit into the destination type, e.g. 1_u8, now has to reject when deserialising into a typed u16, since the type is an explicit mismatch. I tried my best to keep the changes in parsing small-ish and to use this opportunity to improve some error reporting a bit.

For users of RON, this should be an unobservable change (apart from some error code changes which are now more meaningful). However, writing RON files can now be a bit more Rusty as types can be encoded explicitly and underscores can be used to make (long) number literals more readable. However, anyone serialising RON data should probably not turn the PrettyConfig::number_suffixes flag on unless they need lossless roundtrips through Value::Number variants, since explicit typing makes it harder to upgrade a format to different number types without breaking compatibility.

codecov-commenter commented 1 year ago

Codecov Report

Patch coverage: 91.90% and project coverage change: +1.07% :tada:

Comparison is base (3f877c2) 82.95% compared to head (afd2ec0) 84.02%.

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #481 +/- ## ========================================== + Coverage 82.95% 84.02% +1.07% ========================================== Files 74 75 +1 Lines 9267 10035 +768 ========================================== + Hits 7687 8432 +745 - Misses 1580 1603 +23 ``` | [Files Changed](https://app.codecov.io/gh/ron-rs/ron/pull/481?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | | |---|---|---| | [src/error.rs](https://app.codecov.io/gh/ron-rs/ron/pull/481?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2Vycm9yLnJz) | `33.54% <20.00%> (-9.73%)` | :arrow_down: | | [src/ser/mod.rs](https://app.codecov.io/gh/ron-rs/ron/pull/481?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL3Nlci9tb2QucnM=) | `71.92% <56.75%> (+0.35%)` | :arrow_up: | | [src/parse.rs](https://app.codecov.io/gh/ron-rs/ron/pull/481?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL3BhcnNlLnJz) | `82.36% <88.10%> (+7.05%)` | :arrow_up: | | [tests/481\_number\_underscores\_suffixes.rs](https://app.codecov.io/gh/ron-rs/ron/pull/481?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-dGVzdHMvNDgxX251bWJlcl91bmRlcnNjb3Jlc19zdWZmaXhlcy5ycw==) | `99.79% <99.79%> (ø)` | | | [src/de/mod.rs](https://app.codecov.io/gh/ron-rs/ron/pull/481?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2RlL21vZC5ycw==) | `75.30% <100.00%> (+0.39%)` | :arrow_up: | | [src/ser/tests.rs](https://app.codecov.io/gh/ron-rs/ron/pull/481?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL3Nlci90ZXN0cy5ycw==) | `85.97% <100.00%> (-0.34%)` | :arrow_down: | | [tests/370\_float\_parsing.rs](https://app.codecov.io/gh/ron-rs/ron/pull/481?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-dGVzdHMvMzcwX2Zsb2F0X3BhcnNpbmcucnM=) | `66.07% <100.00%> (-32.15%)` | :arrow_down: | ... and [2 files with indirect coverage changes](https://app.codecov.io/gh/ron-rs/ron/pull/481/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

juntyr commented 1 year ago

Manually checking the coverage shows that 100% of the patch seems to be covered

juntyr commented 1 year ago

?r @torkleyy and @cart

juntyr commented 1 year ago

One interesting detail for the future is that:

  1. currently if you deserialise into a typed integer but a different suffix is used, an error is thrown
  2. deserialising into a Value::Number preserves the type of the suffix
  3. deserialising from a Value::Number allows casting between types to occur

If we don't want this behaviour (in a future where roundtripping through Value is possible and should produce the same errors), Value would need to keep track of whether a prefix was used or not.