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

RUST-1592 Support decimal128 to/from human-readable strings #404

Merged
merged 30 commits into from
Feb 15, 2023

Conversation

abr-egn
Copy link
Contributor

@abr-egn abr-egn commented Feb 9, 2023

RUST-1592

This adds support for the standard human-readable string traits (Display and FromStr) to Decimal128 via an intermediate ParsedDecimal128 struct, as well as using such in the extjson format. While it's possible to generate/parse the strings directly from the packed binary representation, I opted for the intermediate struct because it's way more readable and it'll be useful if we ever want to support more operations.

@abr-egn abr-egn marked this pull request as ready for review February 9, 2023 20:35
Copy link
Contributor

@isabelatkinson isabelatkinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good! just a few questions/comments for my own understanding.

impl Decimal128 {
pub(crate) fn parse(self) -> extjson::de::Result<crate::Decimal128> {
self.value.parse().map_err(|err| {
dbg!(err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this left over from debugging?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, yes, fixed.

Comment on lines 78 to 83
const BIAS: i16 = 6176;
const TINY: i16 = -6176;
const MAX: i16 = 6111;

const UNUSED_BITS: usize = 2;
const PACKED_WIDTH: usize = 14;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add comments on these consts (and same below) indicating what they represent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thought, done.

decimal_str = if rest.is_empty() { "0" } else { rest };

// Check decimal precision
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the purpose of this separated scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making sure that len doesn't accidentally leak into other things.

Copy link
Contributor

@isabelatkinson isabelatkinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@abr-egn abr-egn merged commit fda2cf3 into mongodb:main Feb 15, 2023
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.

2 participants