Merge rust-bitcoin/rust-bitcoin#3346: Add a condition for parsing zero from string when not denominated.

894f82e7cc Add a condition for parsing zero from string when not denominated. (yancy)

Pull request description:

  closes https://github.com/rust-bitcoin/rust-bitcoin/issues/3307

ACKs for top commit:
  Kixunil:
    ACK 894f82e7cc
  tcharding:
    ACK 894f82e7cc
  apoelstra:
    ACK 894f82e7cc9eb459a297d43e82734621e0824610; successfully ran local tests.

Tree-SHA512: 6d32847c74ccedc3355d615838e2dd1e08add29cc47ff69d9ef22683eda93049d41131dd0c992c8d7be3a018f5438856c415a3d2f3cb9de9c986534b268ee386
This commit is contained in:
merge-script 2024-09-13 17:41:38 +00:00
commit 7360c3ce9a
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 51 additions and 2 deletions

View File

@ -1157,7 +1157,22 @@ impl ops::DivAssign<u64> for Amount {
impl FromStr for Amount {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> { Amount::from_str_with_denomination(s) }
fn from_str(s: &str) -> Result<Self, Self::Err> {
let result = Amount::from_str_with_denomination(s);
match result {
Err(ParseError::MissingDenomination(_)) => {
let d = Amount::from_str_in(s, Denomination::Satoshi);
if d == Ok(Amount::ZERO) {
Ok(Amount::ZERO)
} else {
result
}
},
_ => result
}
}
}
impl TryFrom<SignedAmount> for Amount {
@ -1580,7 +1595,22 @@ impl ops::Neg for SignedAmount {
impl FromStr for SignedAmount {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> { SignedAmount::from_str_with_denomination(s) }
fn from_str(s: &str) -> Result<Self, Self::Err> {
let result = SignedAmount::from_str_with_denomination(s);
match result {
Err(ParseError::MissingDenomination(_)) => {
let d = SignedAmount::from_str_in(s, Denomination::Satoshi);
if d == Ok(SignedAmount::ZERO) {
Ok(SignedAmount::ZERO)
} else {
result
}
},
_ => result
}
}
}
impl TryFrom<Amount> for SignedAmount {
@ -2079,6 +2109,25 @@ mod tests {
}
}
#[test]
fn from_str_zero_without_denomination() {
let _a = Amount::from_str("0").unwrap();
let _a = Amount::from_str("0.0").unwrap();
let _a = Amount::from_str("00.0").unwrap();
assert!(Amount::from_str("-0").is_err());
assert!(Amount::from_str("-0.0").is_err());
assert!(Amount::from_str("-00.0").is_err());
let _a = SignedAmount::from_str("-0").unwrap();
let _a = SignedAmount::from_str("-0.0").unwrap();
let _a = SignedAmount::from_str("-00.0").unwrap();
let _a = SignedAmount::from_str("0").unwrap();
let _a = SignedAmount::from_str("0.0").unwrap();
let _a = SignedAmount::from_str("00.0").unwrap();
}
#[test]
fn from_int_btc() {
let amt = Amount::from_int_btc(2);