#bandwidth #human #serde #networking #data-structures

human-bandwidth

A library for representing bandwidth speed in a human-readable format

4 releases

0.1.3 Dec 30, 2024
0.1.2 Dec 12, 2024
0.1.1 Jun 25, 2024
0.1.0 Jun 25, 2024

#508 in Network programming

Download history 14/week @ 2024-09-18 7/week @ 2024-09-25 4/week @ 2024-10-02 2/week @ 2024-10-09 2/week @ 2024-11-06 2/week @ 2024-11-13 6/week @ 2024-11-20 2/week @ 2024-11-27 27/week @ 2024-12-04 173/week @ 2024-12-11 5/week @ 2024-12-18 104/week @ 2024-12-25 39/week @ 2025-01-01

331 downloads per month
Used in netem-trace

Apache-2.0

43KB
889 lines

Human Bandwidth

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

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

MSRV: 1.60

Examples

More detailed usage can be found on documentation.

For parsing and formatting:

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

fn main() {
    // Parse bandwidth from human-readable string
    // ------------------------------------------
    // Parse bandwidth from integer format
    assert_eq!(parse_bandwidth("9Tbps 420Gbps"), Ok(Bandwidth::new(9420, 0)));
    assert_eq!(parse_bandwidth("32Mbps"), Ok(Bandwidth::new(0, 32_000_000)));
    // Parse bandwidth from decimal format
    assert_eq!(parse_bandwidth("150.024kbps"), Ok(Bandwidth::new(0, 150_024)));
    // The fractional part less than 1bps will always be ignored
    assert_eq!(parse_bandwidth("150.02456kbps"), Ok(Bandwidth::new(0, 150_024)));

    // Format bandwidth to human-readable string
    // -----------------------------------------
    // By default, the bandwidth will be formatted to
    // the highest unit possible in decimal format
    let val1 = Bandwidth::new(9420, 0);
    assert_eq!(format_bandwidth(val1).to_string(), "9.42Tbps");
    let val2 = Bandwidth::new(0, 32_000_000);
    assert_eq!(format_bandwidth(val2).to_string(), "32Mbps");
    // To format bandwidth in integer format, enable the `display-integer` feature
    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

Dependencies

~215KB