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

Implement the FromStr trait for ByteSize #20

Merged
merged 11 commits into from
Jul 30, 2021

Conversation

jRimbault
Copy link
Contributor

@jRimbault jRimbault commented Jul 23, 2020

A use case I came around when I wanted the user to provide a ByteSize in my program CLI arguments and using a argument parser like structopt or argh or gumdrop.

Allows parsing a string directly into a ByteSize:

let size = "128GB".parse::<ByteSize>().unwrap();

I'm bad at writing negative test cases though.

Short example of a binary using structopt taking a size as a command line argument:

use bytesize::ByteSize;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Args {
    size: ByteSize,
}

fn main() {
    println!("{}", Args::from_args().size);
}

PS: the two 'static lifetime markers I removed because of clippy's suggestion

@orhun
Copy link

orhun commented Jul 20, 2021

In case someone is looking for something like this: https://crates.io/crates/byte-unit

e.g. https://docs.rs/byte-unit/4.0.12/byte_unit/struct.Byte.html#method.from_str

Copy link
Member

@hyunsik hyunsik left a comment

Choose a reason for hiding this comment

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

Sorry for a late review. The patch looks straightforward and good to me.

@hyunsik hyunsik merged commit 3cb1a70 into bytesize-rs:master Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants