Skip to content

Conversation

@IanManske
Copy link
Member

@IanManske IanManske commented Nov 17, 2024

Description

Adds a new Filesize type so that FromValue can be used to convert a Value::Filesize to a Filesize. Currently, to extract a filesize from a Value using FromValue, you have to extract an i64 which coerces Value::Int, Value::Duration, and Value::Filesize to an i64.

Having a separate type also allows us to enforce checked math to catch overflows. Similarly, it allows us to specify other trait implementations like Display in a common place.

User-Facing Changes

Multiplication with filesizes now error on overflow. Should not be a breaking change for plugins (i.e., serialization) since Filesize is marked with serde(transparent).

Tests + Formatting

Updated some tests.

@IanManske IanManske added category:refactor notes:fixes Include the release notes summary in the "Bug fixes" section deprecated:pr-errors Deprecated: use A:error-handling, A:error-unhelpful, or A:error-silent-fail instead labels Nov 17, 2024
} else if *rhs == 0 {
if let Some(val) = checked_div_floor_i64(lhs.get(), rhs.get()) {
Ok(Value::int(val, span))
} else if *rhs == Filesize::ZERO {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you do quite a bit of this, why not just add a .is_zero()? I think it's nicer to read

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe, seems a little unnecessary though. Besides the filesize-specific methods, I only added methods also present on i64.

Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

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

Thanks for doing this tedious work!

(I was initially thinking for #14147 having that type just in FromValue would suffice but in Value its probably even better in the long term so folks don't reach for stupid mistakes)

Comment on lines +411 to +423
"B" => Self::B,
"kB" => Self::KB,
"MB" => Self::MB,
"GB" => Self::GB,
"TB" => Self::TB,
"PB" => Self::PB,
"EB" => Self::EB,
"KiB" => Self::KiB,
"MiB" => Self::MiB,
"GiB" => Self::GiB,
"TiB" => Self::TiB,
"PiB" => Self::PiB,
"EiB" => Self::EiB,
Copy link
Member

Choose a reason for hiding this comment

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

This is more strict than our existing case laissez-faire, where would this be applied?

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's not used ATM. This is PR is split off of a larger refactor. I can move this to the follow up PR if that's more clear (forgot to remove it).

/// ```
pub const fn as_str(&self) -> &'static str {
match self {
Self::B => "B",
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if this is also a concern for the NUON breaking 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.

It would be once it is used.

@@ -1,13 +1,442 @@
use crate::Config;
use crate::{Config, FromValue, IntoValue, ShellError, Span, Type, Value};
use byte_unit::UnitType;
Copy link
Member

Choose a reason for hiding this comment

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

I am wondering if with all the code you have written, we can basically get rid of the byte_unit dependency? Last time I checked we only used a few parts with many of our own types or formatting on top.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, in my other branch, I was able to get rid of the dependency.

Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

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

To avoid bitrot I would vouch for landing this soon, I think the somewhat additional code (work you started for byte_unit replacement, right?) is fine here, if we can be clear about the impact on our case-insensitive parse and on the nuon hashing in nupm.

@IanManske IanManske merged commit 7f61cbb into nushell:main Nov 29, 2024
13 checks passed
@github-actions github-actions bot added this to the v0.101.0 milestone Nov 29, 2024
@IanManske IanManske deleted the filesize-type branch March 9, 2025 21:52
sholderbach added a commit that referenced this pull request Sep 8, 2025
Fixes #16592

Maybe a more organized version of type Duration (like those of #14369)
is better, but that's beyond this naive fix.

## Release notes summary - What our users need to know

None

## Tasks after submitting

None

---------

Co-authored-by: Stefan Holderbach <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category:refactor deprecated:pr-errors Deprecated: use A:error-handling, A:error-unhelpful, or A:error-silent-fail instead notes:fixes Include the release notes summary in the "Bug fixes" section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants