Commit Graph

83 Commits

Author SHA1 Message Date
Tobin C. Harding abc54d0343
Make amount module a directory
In preparation for splitting the error types out of `amount.rs` into
their own file move the `amount.rs` file to `amount/mod.rs`.

File move only, no other changes.
2024-10-31 09:52:32 +11:00
Casey Rodarmor 1d2cfb036c Make `Amount::to_sat` and `SignedAmount::to_sat` const 2024-10-19 11:49:14 -07:00
Jamil Lambert, PhD 88b53a471e
Unify deprecated note field format
All the deprecated note fields have been changed to be lower case and in
the format "use `a` instead".
2024-10-15 15:16:01 +01:00
Fmt Bot f1733309e0 2024-10-13 automated rustfmt nightly 2024-10-13 01:20:28 +00:00
yancy a0c58a4a8b Add checked weight division to Amount 2024-10-09 09:30:09 -05:00
merge-script 4fb2fccd16
Merge rust-bitcoin/rust-bitcoin#3367: Comment from_str methods
f5cae1cddd Comment from_str methods (yancy)

Pull request description:

  Follow up from https://github.com/rust-bitcoin/rust-bitcoin/pull/3346

ACKs for top commit:
  tcharding:
    ACK f5cae1cddd
  apoelstra:
    ACK f5cae1cddd successfully ran local tests

Tree-SHA512: 2b95381e5281754e2b3a49aa8dfaac5742c244970fb54f68024dc23b61a74955ae95b9a0e7ae848095ac0686df5faf93faf7de3371b2f341b108cc10e5d4a9cd
2024-09-18 18:26:38 +00:00
yancy f5cae1cddd Comment from_str methods 2024-09-17 17:19:51 -05:00
Fmt Bot 7990724ff4 2024-09-15 automated rustfmt nightly 2024-09-15 01:19:55 +00:00
merge-script 7360c3ce9a
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
2024-09-13 17:41:38 +00:00
yancy 894f82e7cc Add a condition for parsing zero from string when not denominated. 2024-09-12 09:34:18 -05:00
Shing Him Ng eda87517c0 Update documentation to indicate that the Display implementation in Amount is unstable 2024-09-07 15:05:14 -05:00
merge-script 3bbe821ce5
Merge rust-bitcoin/rust-bitcoin#3268: Fix clippy rustdocs warnings
b6371b5801 Fix clippy rustdocs warnings (Tobin C. Harding)

Pull request description:

  A new nightly version (`nightly-2024-08-28`) introduces a few warnings because of our rustdocs. These are valid warnings and should be fixed, thanks `clippy` team.

  (The `bip152` change is a bit sloppy, open to suggestions.)

ACKs for top commit:
  apoelstra:
    ACK b6371b5801 successfully ran local tests
  Kixunil:
    ACK b6371b5801

Tree-SHA512: 503fb9d48772b74a5acdb26c0f77a85c52323c03360f983204fccee0f28bedeff142237b067caa1ce6ea04ea9842cc493e0d06dc141ca00a98151fa002b62392
2024-09-02 14:20:44 +00:00
Fmt Bot fa71b0e044 2024-09-01 automated rustfmt nightly 2024-09-01 01:22:04 +00:00
merge-script c2e3e86106
Merge rust-bitcoin/rust-bitcoin#3274: Add Arbitrary to SignedAmount type
3f244db19d Add Arbitrary to SignedAmount type (yancy)

Pull request description:

  While proptesting, I've found Arbitrary for SingedAmount to be useful.  Would appreciate adding this as well.

ACKs for top commit:
  tcharding:
    ACK 3f244db19d
  Kixunil:
    ACK 3f244db19d

Tree-SHA512: f293215505db85998135c75e147f4cb0031a08c7db619b706e6c7d632c6f23783af56992531396aecc6e3da55b107a7f2c7ec4d68a3e5737ebcf4225f306ed29
2024-08-30 20:08:17 +00:00
yancy 3f244db19d Add Arbitrary to SignedAmount type 2024-08-29 20:19:48 -05:00
Tobin C. Harding b6371b5801
Fix clippy rustdocs warnings
A new nightly version (`nightly-2024-08-28`) introduces a few warnings
because of our rustdocs. These are valid warnings and should be fixed,
thanks `clippy` team.

(The `bip152` change is a bit sloppy, open to suggestions.)
2024-08-30 05:47:31 +10:00
Jamil Lambert, PhD 9fce57b738
Change T::from_str(s) to s.parse::<T>() in tests
`s.parse` is more idiomatic and produces more helpful error messages.

This has been changed repo wide in tests.
2024-08-28 16:13:03 +01:00
Jamil Lambert, PhD c835bb9eab
Change T::from_str(s) to s.parse::<T>() in docs
`s.parse` is more idiomatic and produces more helpful error messages.

This has been changed repo wide in docs.
2024-08-28 13:50:42 +01:00
Jamil Lambert, PhD a76d76eca1
Change `T::from_str(s)` to `s.parse::<T>()`
`s.parse` is more idiomatic and produces more helpful error messages.

This has been changed repo wide in the main codebase, not including
examples, rustdocs, and in the test module.

`use std::str::FromStr;` has been removed where this change makes
it unnecessary.
2024-08-27 17:31:00 +01:00
yancy 3e034d5ede Add Arbitrary dependency
Implement Arbitrary for a select subset of types.
2024-08-23 15:39:20 -05:00
Tobin C. Harding 60b3cabb41
Panic in const context
Now that our MSRV is past 1.57 we can panic in const contexts.

Fix: #2427
2024-08-22 17:37:17 +10:00
Jamil Lambert, PhD 2169b75bba Use lower case error messages
Error messages should start with a lower case character unless it is a
proper noun.

This has been changed everywhere.
2024-07-15 09:25:08 +01:00
Jamil Lambert, PhD 6dd5af9678 Add missing links
The use of links in the rustdocs was inconsistent.

Links have been added when missing.

[`locktime::absolute::Height`] and [`locktime::relative::Height`] did
not work and `(crate::locktime)` was appended to fix it.
2024-07-08 08:53:13 +01:00
Jamil Lambert, PhD 47e367f011 Standardize error headings
Created headings for a couple of function error descriptions to be
consistent with the rest of the crate.

Added a description explaining why Mega is not allowed in a
denomination.
2024-07-08 08:52:15 +01:00
Jamil Lambert, PhD 75f317a689 Fix rustdoc grammar
In the rustdocs, made all function descriptions third person. Corrected
some grammar and improved some wording.
2024-07-08 08:52:15 +01:00
Jamil Lambert, PhD 573f8ce724 Add backticks to rustdoc links
Links in rustdocs should be formatted with a backtick.  This has been
changed throughout Units.
2024-07-08 08:52:15 +01:00
Fmt Bot 91382977fb 2024-07-07 automated rustfmt nightly 2024-07-07 01:10:59 +00:00
merge-script 014c4931be
Merge rust-bitcoin/rust-bitcoin#2951: Fix `Amount` decimals handling
3196c271ac Deprecate `Amount::fmt_value_in` (Martin Habovstiak)
467546fc0c Round `Amount` when requested precision is too low (Martin Habovstiak)
7c95a777c1 Fix `Amount` decimals handling (Martin Habovstiak)

Pull request description:

  Displaying with minimal number of zeros is the default in Rust and we want to follow it. This was originally implemented in #716 but #2604 reversed it claiming this is common, however it broke people who rely on minimal display (e.g. BIP21) without fixing the root cause of #2136.

  This reverts commit d887423efc adds a test to prevent this change and also fixes the problem with `{:0.8}` not working.

  Closes #2136
  Closes #2948

  Can we backport this one too?

ACKs for top commit:
  tcharding:
    ACK 3196c271ac
  apoelstra:
    ACK 3196c271ac I also really like how this reduces the complexity of the module, basically passing everything through to `display_in`

Tree-SHA512: 3221f83086ac55af3d4caad03fe2b619be303533bba12096040419d119600c8597938809e171662f11b515d469156b083b2072b901d445e4fdfc7b1062cf7b6a
2024-07-04 13:19:32 +00:00
Martin Habovstiak 3196c271ac Deprecate `Amount::fmt_value_in`
`fmt_value_in` was added when `display_in` wasn't available. However
common usage patterns seem to favor `display_in`. It can be used within
format strings and supports formatting options.

Removing it will simplify the codebase, so this deprecates it.
2024-07-03 07:38:58 +02:00
Martin Habovstiak 467546fc0c Round `Amount` when requested precision is too low
Low precision was previously not considered because it was believed it
could lead to misleading data being displayed. However it's quite
possible that people using low precision know what they are doing and
it's sometimes useful to show rounded numbers.

To enable low precision we just compute what the rounded number would be
and display that one instead.

This actually fully closes #2136 since this issue was mentioned there
along with previously-fixed displaying of higher precisions.
2024-07-02 18:39:56 +02:00
Martin Habovstiak 7c95a777c1 Fix `Amount` decimals handling
Displaying with minimal number of zeros is the default in Rust and we
want to follow it. This was originally implemented in #716 but #2604
reversed it claiming this is common, however it broke people who rely on
minimal display (e.g. BIP21) without fixing the root cause of #2136.

This reverts commit d887423efc adds a test
to prevent this change and also fixes the problem with `{:0.8}` not
working.

Closes #2136
Closes #2948
2024-07-02 18:38:54 +02:00
Fmt Bot 19b093080b 2024-06-30 automated rustfmt nightly 2024-06-30 01:10:26 +00:00
Jamil Lambert, PhD 9ea8c58ad6 Fix case sensitivity of denomination
The comment in `FromStr` says that any combination of upper and lower case is considered valid, but the code only allowed a set list of variations. It also had a non exhaustive list of `CONFUSING_FORMS`.

The comment and code has been changed to only consider all upper case or all lower case denominations as valid or where BTC/btc is prefixed with a lower case c, m or u.  `CONFUSING_FORMS` has been changed to contain all combinations of an upper case prefix on an otherwise valid denomination.
2024-06-18 10:46:17 +01:00
Jamil Lambert, PhD 67569ca632 Remove denominations < 1 satoshi
Removed all millisatoshi, nano and pico bitcoin denominations and tests from `Amount`
2024-06-18 10:26:20 +01:00
Jamil Lambert, PhD 4a9f74b55c fix missing fullstops in bitcoin rustdoc
Added missing fullstops to the rustdoc titles for everything on the main page of the bitcoin crate
2024-06-06 16:37:12 +01:00
jamil.lambert d219ceb68e Standardize function doc Examples
Changed the function docs to have the same format of
///
/// # Examples
///
/// description
2024-05-24 09:59:42 +01:00
jamil.lambert 233a9133d8 Standardize function doc Panics
Changed the function docs to have the same format of
///
/// # Panics
///
/// description
2024-05-24 09:59:29 +01:00
jamil.lambert 101378c4d0 Removed //! spare line at end of headers
Some of the headers had a //! at the end but most didn't. They have all been removed in units/src/ to make the files consistent
2024-05-22 12:29:41 +01:00
Tobin C. Harding e47e57c265
Fix kani test
In a kani test we are checking if the result of `to_unsigned()` is an
error but setting `is_signed` to `true`, this field represents the
signed-ness of the return type of `to_unsigned` (`Amount`) so should be
`false`.

Fix kani daily job.
2024-04-19 06:15:49 +10:00
Tobin C. Harding 5981b15902
kani: Rename tests
The tests currently include the word "add" but they test addition as
well as subtraction. Elect to keep the multiple assertions per test and
just make the names more general.
2024-04-16 10:05:07 +10:00
Tobin C. Harding 17bacc6fb6
kani: Remove redundant import
`cargo` is reporting a redundant import warning:

 warning: the item `TryInto` is imported redundantly

Remove the import statement from the `verification` module.
2024-04-16 06:18:06 +10:00
Fmt Bot a565db9fdd 2024-03-31 automated rustfmt nightly 2024-03-31 01:03:18 +00:00
Tobin C. Harding 669d5e8fc6
base58: Add InvalidCharacterError for decoding
The `base58::decode` function can only return a single error type, add a
`InvalidCharacterError` struct (leaf error) to use as the return type.
2024-03-21 06:22:51 +11:00
448 OG d887423efc
Enforce displaying Amount with trailing zeros
It is common to display bitcoins using trailing zeros upto 8 decimals.
This commit enforces:
- Displaying Amount in BTC with trailing zeroes by eight decimal places
  if a precision on the Amount is not specified.
- Displaying Amount in BTC upto the precision specified truncating the insignificant zeros.
- Displaying amount in BTC without any decimals if the remainder of the amount
  divided by the satoshis in 1 BTC is equal to zero using formula `satoshis.rem_euclid(Amount::ONE_BTC.to_sat()) != 0`

These are not breaking changes and all previous tests pass.

A testcase is added to for changes introduced.

Resolves: #2136
2024-03-17 17:55:59 +03:00
Tobin C. Harding 62f726d5a8
Remove useless convertion
No clue how this got into master; found with `just sane`.

Clippy emits:

 useless conversion to the same type: `amount::ParseAmountError`

As suggested, remove the useless conversion.
2024-03-12 12:56:07 +11:00
Liam Aharon b9f7462958
Implement infallible for errors
Creates a new macro `impl_from_infallible`, and applies it to custom
error types in the codebase.

Closes #1222.
2024-03-08 16:48:34 +11:00
Andrew Poelstra 6f14a1031a
Merge rust-bitcoin/rust-bitcoin#2527: Tidy description
e3db95226a Tidy description (yancy)

Pull request description:

  I noticed `uncheced_add` looked really bad with two spaces (my mistake).  Fixed some others as well.

ACKs for top commit:
  apoelstra:
    ACK e3db95226a oops, we should have caught this. Thanks for the fix!
  tcharding:
    ACK e3db95226a

Tree-SHA512: 8a2e7f85262f17063bea6ac22855ae45d99a1559a2d30f2627ffba1108f0fd8ebd0b541b50fe746b5af2ebb013cb3e9ea432987b90f37c046120388a808c5443
2024-02-29 21:14:31 +00:00
yancy e3db95226a Tidy description
Add one space between header and description body
2024-02-29 15:21:46 +01:00
Martin Habovstiak 08f83898a3 Report the position of an invalid char in amount
It can be helpful to report the exact position where the invalid
character was encountered. This change adds he feature.
2024-02-24 09:42:10 +01:00
Martin Habovstiak 73b325aec5 Report position of the first "too precise" digit
Sometimes people don't remember the exact number of decimal places
supported by denomination or don't want to count (e.g. when converting
fiat to BTC the calculator may yield too precise value). It's helpful to
say in error message at which digit the precision is too high.

This adds `TooPreciseError` struct containing the information and
improves the error message to display it.
2024-02-24 08:55:32 +01:00