Crate bity

Source
Expand description

SI prefix, data, packets, data-rate, packet-rate string parser and formater.

§Examples

assert_eq!(bity::si::parse("5.1M").unwrap(), 5_100_000);
assert_eq!(bity::bit::parse("12.34kb").unwrap(), 12_340);
assert_eq!(bity::bps::parse("8.65kB/s").unwrap(), 69_200);
assert_eq!(bity::byte::parse("55.6kB").unwrap(), 55_600);
assert_eq!(bity::byteps::parse("94.5kB/s").unwrap(), 94_500);
assert_eq!(bity::packet::parse("3.4kp").unwrap(), 3_400);
assert_eq!(bity::pps::parse("2.44Mpps").unwrap(), 2_440_000);

assert_eq!(bity::si::format(5_100_000), "5.1M");
assert_eq!(bity::bit::format(12_340), "12.34kb");
assert_eq!(bity::bps::format(69_200), "69.2kb/s");
assert_eq!(bity::byte::format(55_600), "55.6kB");
assert_eq!(bity::byteps::format(94_500), "94.5kB/s");
assert_eq!(bity::packet::format(3_400), "3.4kp");
assert_eq!(bity::pps::format(2_440_000), "2.44Mp/s");

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "kebab-case")]
struct Config {
    #[serde(with = "bity::si")]
    max_users: u64,
    #[serde(with = "bity::bit")]
    user_quota: u64,
    #[serde(with = "bity::bps")]
    bandwidth: u64,
    #[serde(with = "bity::byte")]
    disk_size: u64,
    #[serde(with = "bity::byteps")]
    write_speed: u64,
    #[serde(with = "bity::packet")]
    remaining: u64,
    #[serde(with = "bity::pps")]
    record: u64,
}

assert_eq!(
    toml::from_str::<Config>(
        r#"
        max-users = "1.5k"
        user-quota = "5.2Gb"
        bandwidth = "512kb/s"
        disk-size = "88.1TB"
        write-speed = "42.9MB/s"
        remaining = "43.88kp"
        record = "88.3Mp/s"
        "#
    )
    .unwrap(),
    Config {
        max_users: 1_500,
        user_quota: 5_200_000_000,
        bandwidth: 512_000,
        disk_size: 88_100_000_000_000,
        write_speed: 42_900_000,
        remaining: 43_880,
        record: 88_300_000,
    }
);

assert_eq!(
    toml::to_string(&Config {
        max_users: 1_500,
        user_quota: 5_200_000_000,
        bandwidth: 512_000,
        disk_size: 88_100_000_000_000,
        write_speed: 42_900_000,
        remaining: 43_883,
        record: 88_300_000,
    })
    .unwrap(),
    indoc! {
        r#"
        max-users = "1.5k"
        user-quota = "5.2Gb"
        bandwidth = "512kb/s"
        disk-size = "88.1TB"
        write-speed = "42.9MB/s"
        remaining = "43.88kp"
        record = "88.3Mp/s"
        "#
    }
);

§Features

  • No precision loss
  • Differentiate bits and bytes
  • serde support

§Limitations

  • Only support metric prefixes, IEC prefixes are not supported
  • No customizable formating
  • u64 limited (doesn’t go above exa, aka. 10^18)

Modules§

bit
SI prefixed data (bit) parsing and formatting.
bps
SI prefixed data-rate (bit) parsing and formatting.
byte
SI prefixed data (byte) parsing and formatting.
byteps
SI prefixed data-rate (byte) parsing and formatting.
packet
SI prefixed packets count parsing and formatting.
pps
SI prefixed packet-rate parsing and formatting.
si
SI prefix parsing and formatting.

Enums§

Error
Errors that can happen when using parsing functions.

Functions§

strip_per_second
Strip at most one per-second prefix such as /s or ps (per-second).