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

Display for IEC units #37

Open
ctron opened this issue Sep 15, 2023 · 2 comments · May be fixed by #38
Open

Display for IEC units #37

ctron opened this issue Sep 15, 2023 · 2 comments · May be fixed by #38
Assignees
Labels
Milestone

Comments

@ctron
Copy link

ctron commented Sep 15, 2023

By default the Display implementation is using SI units (e.g. KB), which looks weird when printing those:

      --http-server-request-limit <http-server-request-limit>
          The overall request limit [env: HTTP_SERVER_REQUEST_LIMIT=] [default: "262.1 KB"]
      --http-server-json-limit <http-server-json-limit>
          The JSON request limit [env: HTTP_SERVER_JSON_LIMIT=] [default: "2.1 MB"]

It would be great if there would be a newtype for defaulting to "binary format" (powers of 2, e.g. KiB).

@ctron
Copy link
Author

ctron commented Sep 15, 2023

For example:

#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
pub struct BinaryByteSize(pub ByteSize);

impl Deref for BinaryByteSize {
    type Target = ByteSize;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for BinaryByteSize {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Display for BinaryByteSize {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0.to_string_as(true))
    }
}

impl FromStr for BinaryByteSize {
    type Err = <ByteSize as FromStr>::Err;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        ByteSize::from_str(s).map(BinaryByteSize)
    }
}

Turns this into:

      --http-server-request-limit <http-server-request-limit>
          The overall request limit [env: HTTP_SERVER_REQUEST_LIMIT=] [default: "256.0 kiB"]
      --http-server-json-limit <http-server-json-limit>
          The JSON request limit [env: HTTP_SERVER_JSON_LIMIT=] [default: "2.0 MiB"]

@Andrew15-5 Andrew15-5 linked a pull request Oct 6, 2023 that will close this issue
@MrCroxx
Copy link
Collaborator

MrCroxx commented Nov 18, 2024

Planned with 2.0. 🫡

@MrCroxx MrCroxx self-assigned this Nov 18, 2024
@MrCroxx MrCroxx added the Q & A label Nov 18, 2024
@MrCroxx MrCroxx added this to the 2.0 milestone Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants