parse_bandwidth

Function parse_bandwidth 

Source
pub fn parse_bandwidth(s: &str) -> Result<Bandwidth, Error>
Expand description

Parse bandwidth object 1Gbps 12Mbps 5bps or 1.012000005Gbps

The bandwidth object is a concatenation of rate spans. Where each rate span is an number and a suffix. Supported suffixes:

  • bps, bit/s, b/s – bit per second
  • kbps, kbit/s, kb/s – kilobit per second
  • Mbps, Mbit/s, Mb/s – megabit per second
  • Gbps, Gbit/s, Gb/s – gigabit per second
  • Tbps, Tbit/s, Tb/s – terabit per second

While the number can be integer or decimal, the fractional part less than 1bps will always be ignored.

§Examples

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

assert_eq!(parse_bandwidth("9Tbps 420Gbps"), Ok(Bandwidth::new(9420, 0)));
assert_eq!(parse_bandwidth("32Mbps"), Ok(Bandwidth::new(0, 32_000_000)));
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)));