use std::collections::HashMap;
pub fn convert_units(value: u64, from: &str, to: &str) -> u64 {
let mut units = HashMap::new();
units.insert("bit", 1);
units.insert("byte", 8);
units.insert("kb", 8 * 1024);
units.insert("mb", 8 * 1024 * 1024);
units.insert("gb", 8 * 1024 * 1024 * 1024);
units.insert("tb", 8 * 1024 * 1024 * 1024 * 1024);
units.insert("pb", 8 * 1024 * 1024 * 1024 * 1024 * 1024);
units.insert("eb", 8 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
let value_in_bits = value * units.get(from).expect("Invalid 'from' unit");
let result = value_in_bits / units.get(to).expect("Invalid 'to' unit");
result
}
#[cfg(test)]
mod tests{
use super::*;
#[test]
fn it_should_convert_units(){
assert_eq!(convert_units(8, "bit", "byte"), 1);
assert_eq!(convert_units(1, "byte", "bit"), 8);
assert_eq!(convert_units(1, "kb", "byte"), 1024);
assert_eq!(convert_units(1, "mb", "kb"), 1024);
assert_eq!(convert_units(1, "gb", "mb"), 1024);
assert_eq!(convert_units(1, "tb", "gb"), 1024);
assert_eq!(convert_units(1, "pb", "tb"), 1024);
assert_eq!(convert_units(1, "eb", "pb"), 1024);
}
}