Skip to content

Commit

Permalink
tweak: Disallow dec!(".123") and require "0.123"
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedey committed Sep 20, 2023
1 parent 22f7a12 commit d208ef7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
9 changes: 6 additions & 3 deletions radix-engine-common/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,9 @@ impl FromStr for Decimal {
unreachable!("InvalidLength is only for parsing &[u8], not &str")
}
ParseI192Error::InvalidDigit => return Err(ParseDecimalError::InvalidDigit),
ParseI192Error::Empty => I192::ZERO, // Allows parsing ".123"
// We have decided to be restrictive to force people to type "0.123" instead of ".123"
// for clarity, and to align with how rust's float literal works
ParseI192Error::Empty => return Err(ParseDecimalError::EmptyIntegralPart),
},
};

Expand Down Expand Up @@ -849,6 +851,7 @@ impl fmt::Debug for Decimal {
pub enum ParseDecimalError {
InvalidDigit,
Overflow,
EmptyIntegralPart,
EmptyFractionalPart,
MoreThanEighteenDecimalPlaces,
MoreThanOneDecimalPoint,
Expand Down Expand Up @@ -980,8 +983,8 @@ mod tests {
Err(ParseDecimalError::Overflow),
);
assert_eq!(
Decimal::from_str(".000000000000000231").unwrap(),
Decimal(231.into()),
Decimal::from_str(".000000000000000231"),
Err(ParseDecimalError::EmptyIntegralPart),
);
assert_eq!(
Decimal::from_str("231."),
Expand Down
9 changes: 6 additions & 3 deletions radix-engine-common/src/math/precise_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,9 @@ impl FromStr for PreciseDecimal {
unreachable!("InvalidLength is only for parsing &[u8], not &str")
}
ParseI256Error::InvalidDigit => return Err(ParsePreciseDecimalError::InvalidDigit),
ParseI256Error::Empty => I256::ZERO,
// We have decided to be restrictive to force people to type "0.123" instead of ".123"
// for clarity, and to align with how rust's float literal works
ParseI256Error::Empty => return Err(ParsePreciseDecimalError::EmptyIntegralPart),
},
};

Expand Down Expand Up @@ -891,6 +893,7 @@ impl fmt::Debug for PreciseDecimal {
pub enum ParsePreciseDecimalError {
InvalidDigit,
Overflow,
EmptyIntegralPart,
EmptyFractionalPart,
MoreThanThirtySixDecimalPlaces,
MoreThanOneDecimalPoint,
Expand Down Expand Up @@ -1072,8 +1075,8 @@ mod tests {
Err(ParsePreciseDecimalError::Overflow),
);
assert_eq!(
PreciseDecimal::from_str(".000000000000000231").unwrap(),
PreciseDecimal(I256::from(231) * I256::TEN.pow(18)),
PreciseDecimal::from_str(".000000000000000231"),
Err(ParsePreciseDecimalError::EmptyIntegralPart),
);
assert_eq!(
PreciseDecimal::from_str("231."),
Expand Down
10 changes: 8 additions & 2 deletions radix-engine-macros/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ fn decimal_error_reason(error: ParseDecimalError) -> &'static str {
match error {
ParseDecimalError::InvalidDigit => "There is an invalid character.",
ParseDecimalError::Overflow => "The number is too large to fit in a decimal.",
ParseDecimalError::EmptyIntegralPart => {
"If there is a decimal point, the number must include at least one digit before it. Use a 0 if necessary."
},
ParseDecimalError::EmptyFractionalPart => {
"The number cannot have an empty fractional part."
"If there is a decimal point, the number must include at least one digit after it."
}
ParseDecimalError::MoreThanEighteenDecimalPlaces => {
"A decimal cannot have more than eighteen decimal places."
Expand All @@ -93,8 +96,11 @@ fn precise_decimal_error_reason(error: ParsePreciseDecimalError) -> &'static str
ParsePreciseDecimalError::Overflow => {
"The number being too large to fit in a precise decimal"
}
ParsePreciseDecimalError::EmptyIntegralPart => {
"If there is a decimal point, the number must include at least one digit before it. Use a 0 if necessary."
}
ParsePreciseDecimalError::EmptyFractionalPart => {
"The number cannot have an empty fractional part."
"If there is a decimal point, the number must include at least one digit after it."
}
ParsePreciseDecimalError::MoreThanThirtySixDecimalPlaces => {
"A precise decimal cannot have more than thirty-six decimal places."
Expand Down
2 changes: 1 addition & 1 deletion radix-engine-tests/tests/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn test_dec_macro() {
const X13: Decimal = dec!("-3138550867693340381917894711603833208051.177722232017256448");
assert_eq!(X13, Decimal::MIN);

const X14: Decimal = dec!(".000000000000000048");
const X14: Decimal = dec!("0.000000000000000048");
assert_eq!(X14, Decimal(I192::from(48)));
}

Expand Down

0 comments on commit d208ef7

Please sign in to comment.