Skip to content
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 number suffixes and allow more number underscores #481

Merged
merged 10 commits into from Aug 23, 2023

Conversation

juntyr
Copy link
Member

@juntyr juntyr commented Aug 21, 2023

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.

  • I've included my change in CHANGELOG.md

@juntyr juntyr marked this pull request as draft August 21, 2023 20:30
@juntyr juntyr changed the title Add pretty number underscores and suffixes Add number suffixes Aug 21, 2023
@codecov-commenter
Copy link

codecov-commenter commented Aug 21, 2023

Codecov Report

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

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

❗ 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
@@            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 Coverage Δ
src/error.rs 33.54% <20.00%> (-9.73%) ⬇️
src/ser/mod.rs 71.92% <56.75%> (+0.35%) ⬆️
src/parse.rs 82.36% <88.10%> (+7.05%) ⬆️
tests/481_number_underscores_suffixes.rs 99.79% <99.79%> (ø)
src/de/mod.rs 75.30% <100.00%> (+0.39%) ⬆️
src/ser/tests.rs 85.97% <100.00%> (-0.34%) ⬇️
tests/370_float_parsing.rs 66.07% <100.00%> (-32.15%) ⬇️

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@juntyr juntyr marked this pull request as ready for review August 22, 2023 00:58
@juntyr
Copy link
Member Author

juntyr commented Aug 22, 2023

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

src/ser/mod.rs Outdated Show resolved Hide resolved
src/parse.rs Outdated Show resolved Hide resolved
docs/grammar.md Outdated Show resolved Hide resolved
@juntyr juntyr force-pushed the number-underscores-suffixes branch from 4b4d20a to c6622b2 Compare August 22, 2023 14:42
@juntyr juntyr changed the title Add number suffixes Add number suffixes and allow more number underscores Aug 22, 2023
@juntyr
Copy link
Member Author

juntyr commented Aug 22, 2023

?r @torkleyy and @cart

@juntyr
Copy link
Member Author

juntyr commented Aug 22, 2023

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.

@juntyr juntyr mentioned this pull request Aug 20, 2023
17 tasks
@juntyr juntyr merged commit 388cfe3 into ron-rs:master Aug 23, 2023
8 checks passed
@juntyr juntyr deleted the number-underscores-suffixes branch August 23, 2023 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lossless primitive types
3 participants