Skip to content

Commit a39f00b

Browse files
committed
fix units
1 parent 2338824 commit a39f00b

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rfcalc"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
edition = "2021"
55
authors = ["R4 Cheng <[email protected]>"]
66
license = "MIT"

src/calc.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,48 +27,49 @@ pub fn calc_combination(n: u64, k: u64) -> u64 {
2727
numerator / denominator
2828
}
2929

30+
/// B: byte
3031
#[derive(PartialEq, PartialOrd)]
31-
enum BitUnit {
32-
Bit,
32+
enum ByteUnit {
33+
B,
3334
KB,
3435
MB,
3536
GB,
3637
TB,
3738
}
3839

39-
impl BitUnit {
40+
impl ByteUnit {
4041
fn to_bits(&self) -> u64 {
4142
match self {
42-
BitUnit::Bit => 1,
43-
BitUnit::KB => 2u64.pow(10), // 2^10
44-
BitUnit::MB => 2u64.pow(20), // 2^20
45-
BitUnit::GB => 2u64.pow(30), // 2^30
46-
BitUnit::TB => 2u64.pow(40), // 2^40
43+
ByteUnit::B => 1,
44+
ByteUnit::KB => 2u64.pow(10), // 2^10
45+
ByteUnit::MB => 2u64.pow(20), // 2^20
46+
ByteUnit::GB => 2u64.pow(30), // 2^30
47+
ByteUnit::TB => 2u64.pow(40), // 2^40
4748
}
4849
}
4950
}
5051

5152
pub fn devide_bytes(expression: &str) -> String {
52-
let re = Regex::new(r"(?<num1>\d+)\s*(?<unit1>bits|bit|kb|mb|gb|tb)\s*(?<op>/)\s*(?<num2>\d+)\s*(?<unit2>bits|bit|kb|mb|gb|tb)").unwrap();
53+
let re = Regex::new(r"(?<num1>\d+)\s*(?<unit1>byte|bytes|kb|mb|gb|tb)\s*(?<op>/)\s*(?<num2>\d+)\s*(?<unit2>byte|bytes|kb|mb|gb|tb)").unwrap();
5354
if let Some(caps) = re.captures(expression) {
5455
let num1: u64 = caps.name("num1").unwrap().as_str().parse().unwrap();
5556
let unit1 = match caps.name("unit1").unwrap().as_str() {
56-
"bits" => BitUnit::Bit,
57-
"bit" => BitUnit::Bit,
58-
"kb" => BitUnit::KB,
59-
"mb" => BitUnit::MB,
60-
"gb" => BitUnit::GB,
61-
"tb" => BitUnit::TB,
57+
"byte" => ByteUnit::B,
58+
"bytes" => ByteUnit::B,
59+
"kb" => ByteUnit::KB,
60+
"mb" => ByteUnit::MB,
61+
"gb" => ByteUnit::GB,
62+
"tb" => ByteUnit::TB,
6263
_ => return "Wrong input".to_string(),
6364
};
6465
let num2: u64 = caps.name("num2").unwrap().as_str().parse().unwrap();
6566
let unit2 = match caps.name("unit2").unwrap().as_str() {
66-
"bits" => BitUnit::Bit,
67-
"bit" => BitUnit::Bit,
68-
"kb" => BitUnit::KB,
69-
"mb" => BitUnit::MB,
70-
"gb" => BitUnit::GB,
71-
"tb" => BitUnit::TB,
67+
"byte" => ByteUnit::B,
68+
"bytes" => ByteUnit::B,
69+
"kb" => ByteUnit::KB,
70+
"mb" => ByteUnit::MB,
71+
"gb" => ByteUnit::GB,
72+
"tb" => ByteUnit::TB,
7273
_ => return "Wrong input".to_string(),
7374
};
7475

0 commit comments

Comments
 (0)