Skip to content

Add number suffixes and allow more number underscores#481

Merged
juntyr merged 10 commits into
ron-rs:masterfrom
juntyr:number-underscores-suffixes
Aug 23, 2023
Merged

Add number suffixes and allow more number underscores#481
juntyr merged 10 commits into
ron-rs:masterfrom
juntyr:number-underscores-suffixes

Conversation

@juntyr

@juntyr juntyr commented Aug 21, 2023

Copy link
Copy Markdown
Member

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

codecov-commenter commented Aug 21, 2023

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 91.90372% with 74 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.02%. Comparing base (3f877c2) to head (afd2ec0).
⚠️ Report is 75 commits behind head on master.

Files with missing lines Patch % Lines
src/parse.rs 88.10% 37 Missing ⚠️
src/ser/mod.rs 56.75% 32 Missing ⚠️
src/error.rs 20.00% 4 Missing ⚠️
tests/481_number_underscores_suffixes.rs 99.79% 1 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
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     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

juntyr commented Aug 22, 2023

Copy link
Copy Markdown
Member Author

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

Comment thread src/ser/mod.rs Outdated
Comment thread src/parse.rs Outdated
Comment thread docs/grammar.md Outdated
@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

juntyr commented Aug 22, 2023

Copy link
Copy Markdown
Member Author

?r @torkleyy and @cart

@juntyr

juntyr commented Aug 22, 2023

Copy link
Copy Markdown
Member Author

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
@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