http-server-rs / http-server

Simple and configurable command-line HTTP server
https://crates.io/crates/http-server
Apache License 2.0
183 stars 20 forks source link

fix: implementation of file size formatting #379

Closed Antosser closed 11 months ago

Antosser commented 1 year ago

This PR removes src/utils/fmt.rs, since it only had the format_bytes function, which can also be used from another library.

The implementation of format_bytes is also incorrect. 1024 bytes is not equal to 1KB, as the test indicates. 1024 bytes is equal to 1KiB, and means Kibibyte, where bi stands for binary. Why not just use a library for that? That would eliminate such bugs.

The PR changes the file size type from String to u64 and adds a _bytes postfix to the property name.

- pub(crate) size: String,
+ pub(crate) size_bytes: u64,

I would love to store the file size as the Size struct provided by the size crate and remove the postfix, but Size doesn't implement the Serialize trait, and we'd have to wait for that before we can do that.

EstebanBorai commented 1 year ago

Usually computers doesnt render size using Base-2, the use of kilo, giga, and mega is more of jargon than actually the value.

Please take a peek on these two pictures:

This is the current main:

current

This is this branch proposal:

branch

My observations are:

Antosser commented 1 year ago

Usually computers doesnt render size using Base-2, the use of kilo, giga, and mega is more of jargon than actually the value.

Windows uses binary but incorrectly displays KiB as KB

Antosser commented 1 year ago

The point is, the way it is right now is incorrect. The number is binary while the unit is decimal

Antosser commented 1 year ago

but in neither of both the size matches OS specific size

That's because the number in http-server is in KiB, while the OS size is in bytes. It has almost nothing to do with floats

EstebanBorai commented 1 year ago

Usually computers doesnt render size using Base-2, the use of kilo, giga, and mega is

more of jargon than actually the value.

Windows uses binary but incorrectly displays KiB as KB

Im struggling finding an OS that uses Base-2 notations in their data size notations.

Even though I consider you are technically right, I still have to consider the user base counter part, which not only involves the technical side of the problem, but the educational.

OS developers and the Software Industry have been using Base-10 notation for years.

This would introduce confusion, and also would affect current users perception on the data size values because HTTP Server would render a different unit than their systems.

EstebanBorai commented 1 year ago

The point is, the way it is right now is incorrect. The number is binary while the unit is decimal

Thing is, no popular system uses binary notation. Its a matter of usage and how the data industry popularized data units.

Using Base-2 would be against that, please take into consideration all kind of users.

EstebanBorai commented 1 year ago

but in neither of both the size matches OS specific size

That's because the number in http-server is in KiB, while the OS size is in bytes. It has almost nothing to do with floats

Please pay attention to the size value in macOS Get Info window and compare it to HTTP Server's size value.

Antosser commented 1 year ago

This commit now uses the humansize crate, which allows choosing BINARY vs. DECIMAL. This is what if now looks like: image

Antosser commented 1 year ago

These are the possible options provided by the crate: image