Skip to content

Add amount::Display - make formatting configurable#716

Merged
sanket1729 merged 1 commit intorust-bitcoin:masterfrom
Kixunil:configurable_amount_display
Apr 30, 2022
Merged

Add amount::Display - make formatting configurable#716
sanket1729 merged 1 commit intorust-bitcoin:masterfrom
Kixunil:configurable_amount_display

Conversation

@Kixunil
Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil commented Nov 23, 2021

This significatnly refactors the formatting code to make formatting more
configurable. The main addition is the Display type which is a
builder that can configure denomination or other things (possibly more
in the future).

Further, this makes all representations of numbers minimal by default,
so should be documented as a possibly-breaking change.

Because of the effort to support all other fmt::Formatter options this
required practically complete rewrite of fmt_satoshi_in. As a
byproduct I took the opportunity of removing one allocation from there.

Closes #709

@Kixunil Kixunil added enhancement help wanted API break This PR requires a version bump for the next release labels Nov 23, 2021
@dr-orlovsky
Copy link
Copy Markdown
Collaborator

the way Amount is displayed (and also parsed from string) is clearly poor. But I haven't got from the original discussion why just using formatting directives is not enough and we need to introduce dedicated Display type and builder.

@Kixunil
Copy link
Copy Markdown
Collaborator Author

Kixunil commented Nov 23, 2021

Because there's no way to pass more than two possible custom formats into fmt::Formatter (not abusing other things which we actually want to use for what they were designed for).

@dr-orlovsky
Copy link
Copy Markdown
Collaborator

Concept ACK

@apoelstra
Copy link
Copy Markdown
Member

concept ACK. It's not clear from skimming the PR what happens when you just try to Display a bare amount, but I guess it should be the same as using display_dynamic?

@Kixunil
Copy link
Copy Markdown
Collaborator Author

Kixunil commented Dec 20, 2021

@apoelstra I didn't change the original implementation which was displaying amount in BTC with BTC suffix.

@apoelstra
Copy link
Copy Markdown
Member

Ok, that's a reasonable default too.

@Kixunil Kixunil force-pushed the configurable_amount_display branch from 5267db1 to a41c43f Compare February 9, 2022 13:17
@Kixunil Kixunil marked this pull request as ready for review February 9, 2022 13:17
@Kixunil Kixunil changed the title Add DisplayAmount - make formatting configurable Add amount::Display - make formatting configurable Feb 9, 2022
@Kixunil
Copy link
Copy Markdown
Collaborator Author

Kixunil commented Feb 9, 2022

Rebased, added tests, refactored&implemented SignedAmount

I'm not sure if the commits should be squashed though. Leaning a bit towards yes. What do you think? Decided to go for it, since the commits were becoming a mess.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Forgot to mention: I'm making them separate tests because in practice it was very hard to identify which failed.

Copy link
Copy Markdown
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

concept ACK

Some doc nits.

@Kixunil Kixunil force-pushed the configurable_amount_display branch 3 times, most recently from 2b8cc5d to 88bd771 Compare February 9, 2022 22:40
@Kixunil
Copy link
Copy Markdown
Collaborator Author

Kixunil commented Feb 9, 2022

The MSRV is soooo old 😭

Copy link
Copy Markdown
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

Wow, thorough testing, nice effort.

@Kixunil Kixunil force-pushed the configurable_amount_display branch 2 times, most recently from 3a0eb09 to c991b07 Compare February 10, 2022 12:37
@Kixunil
Copy link
Copy Markdown
Collaborator Author

Kixunil commented Feb 10, 2022

@tcharding not thorough enough. I forgot to test precision > 0 branch and indeed it was broken.

Further, I originally wanted sats to behave like integer (ignoring .1) but it recently started feeling super-weird, so I changed it to also work like floats.

I just pushed both fixes.

@Kixunil Kixunil force-pushed the configurable_amount_display branch from c991b07 to 31ae760 Compare February 10, 2022 12:59
@Kixunil
Copy link
Copy Markdown
Collaborator Author

Kixunil commented Feb 10, 2022

The latest push changes naive manual exponentiation to faster pow method.


/// SignedAmount
///
/// The [SignedAmount] type can be used to express Bitcoin amounts that supports
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not introduced in this PR but 'supports' shouldn't be plural.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are two instances of this typo.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

How about having an issue where people can link typos and once there are many we do a larger batch of fixes?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's a good idea, should save time.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

dr-orlovsky
dr-orlovsky previously approved these changes Mar 12, 2022
Copy link
Copy Markdown
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

utACK 31ae760

This significatnly refactors the amount formatting code to make
formatting more configurable. The main addition is the
`amount::Display` type which is a builder that can configure
denomination or other things (possibly more in the future).

Further, this makes all representations of numbers minimal by default,
so should be documented as a possibly-breaking change.

Because of the effort to support all other `fmt::Formatter` options this
required practically complete rewrite of `fmt_satoshi_in`. As a
byproduct I took the opportunity of removing one allocation from there.

Closes rust-bitcoin#709
@Kixunil
Copy link
Copy Markdown
Collaborator Author

Kixunil commented Mar 14, 2022

Renamed.

Copy link
Copy Markdown
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

ACK 4f1200d

@dr-orlovsky dr-orlovsky added this to the 0.29.0 milestone Mar 18, 2022
@Kixunil Kixunil added the before edition bump We want to merge this before edition is bumped label Apr 20, 2022
Copy link
Copy Markdown
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

ACK 4f1200d
Thanks for being super careful with this and the elaborate test cases. I learned a lot about format options reviewing this. Hopefully, this settles the Amount Display issue for a long time.

We should add this in release notes as a breaking change

@tcharding
Copy link
Copy Markdown
Member

ACK 4f1200d

@sanket1729 sanket1729 merged commit bcc923c into rust-bitcoin:master Apr 30, 2022
@Kixunil Kixunil deleted the configurable_amount_display branch May 2, 2022 11:53
ChallengeDev210 pushed a commit to ChallengeDev210/rust-bitcoin that referenced this pull request Aug 1, 2022
…matting configurable

4f1200d Added `amount::Display` - configurable formatting (Martin Habovstiak)

Pull request description:

  This significatnly refactors the formatting code to make formatting more
  configurable. The main addition is the `Display` type which is a
  builder that can configure denomination or other things (possibly more
  in the future).

  Further, this makes all representations of numbers minimal by default,
  so should be documented as a possibly-breaking change.

  Because of the effort to support all other `fmt::Formatter` options this
  required practically complete rewrite of `fmt_satoshi_in`. As a
  byproduct I took the opportunity of removing one allocation from there.

  Closes #709

ACKs for top commit:
  tcharding:
    ACK 4f1200d
  dr-orlovsky:
    ACK 4f1200d
  sanket1729:
    ACK 4f1200d

Tree-SHA512: 3fafdf63fd720fd4514e026e9d323ac45dfcd3d3a53a4943178b1e84e4cf7603cb6235ecd3989d46c4ae29453c4b0bb2f2a5996fbddf341cd3f68dc286062144
Kixunil added a commit to Kixunil/rust-bitcoin that referenced this pull request Jul 2, 2024
Displaying with minimal number of zeros is the default in Rust and we
want to follow it. This was originally implemented in rust-bitcoin#716 but rust-bitcoin#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 rust-bitcoin#2136.

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

Closes rust-bitcoin#2136
Closes rust-bitcoin#2948
apoelstra added a commit that referenced this pull request Jul 4, 2024
3196c27 Deprecate `Amount::fmt_value_in` (Martin Habovstiak)
467546f Round `Amount` when requested precision is too low (Martin Habovstiak)
7c95a77 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 d887423 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 3196c27
  apoelstra:
    ACK 3196c27 I also really like how this reduces the complexity of the module, basically passing everything through to `display_in`

Tree-SHA512: 3221f83086ac55af3d4caad03fe2b619be303533bba12096040419d119600c8597938809e171662f11b515d469156b083b2072b901d445e4fdfc7b1062cf7b6a
jlest01 added a commit to jlest01/rust-bitcoin that referenced this pull request Aug 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API break This PR requires a version bump for the next release before edition bump We want to merge this before edition is bumped enhancement release notes mention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Amount does not use minimal representation when displaying

6 participants