human-bandwidth

Crates.iohuman-bandwidth
lib.rshuman-bandwidth
version
sourcesrc
created_at2024-06-25 09:58:09.20838
updated_at2024-12-12 06:52:49.182013
descriptionA library for representing bandwidth speed in a human-readable format.
homepagehttps://github.com/stack-rs/human-bandwidth
repositoryhttps://github.com/stack-rs/human-bandwidth
max_upload_size
id1283189
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Yixin Shen (BobAnkh)

documentation

https://docs.rs/human-bandwidth

README

Human Bandwidth

github-repo crates.io docs.rs LICENSE Apache-2.0

A library providing human-readable format parsing and formating for bandwidth. Enable serde feature for serde integration.

MSRV: 1.60

Examples

More detailed usage can be found on documentation.

For parsing and formating:

use bandwidth::Bandwidth;
use human_bandwidth::Bandwidth;

fn main() {
    // Parse bandwidth from human-readable string
    assert_eq!(parse_bandwidth("9Tbps 420Gbps"), Ok(Bandwidth::new(9420, 0)));
    assert_eq!(parse_bandwidth("32Mbps"), Ok(Bandwidth::new(0, 32_000_000)));

    // Format bandwidth to human-readable string
    let val1 = Bandwidth::new(9420, 0);
    assert_eq!(format_bandwidth(val1).to_string(), "9Tbps 420Gbps");
    let val2 = Bandwidth::new(0, 32_000_000);
    assert_eq!(format_bandwidth(val2).to_string(), "32Mbps");
}

To integrate with serde:

use serde::{Serialize, Deserialize};
use bandwidth::Bandwidth;

#[derive(Serialize, Deserialize)]
struct Foo {
    #[serde(with = "human_bandwidth::serde")]
    bandwidth: Bandwidth,
}

fn main () {
    let json = r#"{"bandwidth": "1kbps"}"#;
    let foo = serde_json::from_str::<Foo>(json).unwrap();
    assert_eq!(foo.bandwidth, Bandwidth::from_kbps(1));
    let reverse = serde_json::to_string(&foo).unwrap();
    assert_eq!(reverse, r#"{"bandwidth":"1kbps"}"#)
}

Maintainer

@BobAnkh

How to contribute

You should follow our Code of Conduct.

See CONTRIBUTING GUIDELINES for contributing conventions.

Make sure to pass all the tests before submitting your code.

Contributors

LICENSE

Apache-2.0 © stack-rs

Credits

Commit count: 9

cargo fmt