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

fix inverted SI / IEC conditions #69

Merged
merged 9 commits into from
Feb 12, 2025
Merged

fix inverted SI / IEC conditions #69

merged 9 commits into from
Feb 12, 2025

Conversation

robjtede
Copy link
Member

@robjtede robjtede commented Feb 10, 2025

rebase and follow on from #38

Fixes #25
Fixes #26
Fixed #37

- Conditions that check whether the SI or IEC units must be used were
  inverted, i.e., when `si_prefix == true` it would use `"iB"` instead of
  `"B"`.
- KB & kiB were used instead of kB & KiB.
- Switches (true/false) in tests are also fixed.
Comment on lines +78 to +83
#[derive(Debug, Clone, Default)]
pub enum Format {
#[default]
IEC,
SI,
}
Copy link
Member Author

@robjtede robjtede Feb 10, 2025

Choose a reason for hiding this comment

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

this enum may or may not stay public (i have an idea to present in a follow up PR) but it really makes this PR easier to reason about

@robjtede robjtede requested review from MrCroxx and hyunsik February 10, 2025 00:55
@wookietreiber
Copy link

Thinking about these changes and #32, IMHO I would deprecate/remove to_string_as, impl Display for ByteSize, and all other variants that take in a bool for IEC/SI. The usage of bool / true / false is discouraged, see Rust API Guidelines. It will always be non-obvious if true is SI or IEC and choosing a default for e.g. impl Display for ByteSize is and will always be opinionated.

Copy link
Collaborator

@MrCroxx MrCroxx left a comment

Choose a reason for hiding this comment

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

Generally LGTM

Thank you, Rob.

src/serde.rs Outdated
fn test_serde_json() {
let json = serde_json::to_string(&ByteSize::mib(1)).unwrap();
assert_eq!(json, "\"1.0 MiB\"");
assert_eq!(json, "\"1024.0 KiB\"");
Copy link
Collaborator

@MrCroxx MrCroxx Feb 11, 2025

Choose a reason for hiding this comment

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

Why is the output here "1024.0 KiB" instead of "1.0 MiB"?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd put it down to a floating point error, but I agree that it's worth figuring out what changed. I notice now that the original PR didn't have to make this change 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

The plot thickens...

assert_to_string("1.0 MiB", ByteSize::mib(1), false);

in the original PR fails the assertion.

Copy link
Member Author

Choose a reason for hiding this comment

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

Solved! It was related to the floating point situation. I've increased the precision of the LN_ constants and now "1.0 MiB" is generated.

@robjtede
Copy link
Member Author

I would deprecate/remove to_string_as

For sure. There's no way I'm letting this fn get in v2.0 😆

Copy link
Collaborator

@MrCroxx MrCroxx left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you, Rob.

There is a tiny problem with the rust doc. Besides, we should setup a pipeline for doc test. Let me open an issue first.

Comment on lines +26 to +27
//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(false));
//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(true));
Copy link
Collaborator

Choose a reason for hiding this comment

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

The rust doc also needs to be updated.

Copy link
Member Author

Choose a reason for hiding this comment

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

it will be, this fn still exists for now

@robjtede robjtede merged commit 81d19ce into master Feb 12, 2025
11 checks passed
@robjtede robjtede deleted the fix-si-iec branch February 12, 2025 01:28
@robjtede robjtede added this to the 2.0 milestone Feb 19, 2025
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.

Display for IEC units LN_KIB / LN_KB are backwards SI unit should be decimal units
4 participants