net_bytes/
lib.rs

1mod download_acceleration;
2mod download_speed;
3mod file_size;
4
5use rust_decimal::Decimal;
6
7pub use download_acceleration::DownloadAcceleration;
8pub use download_speed::DownloadSpeed;
9pub use file_size::FileSize;
10
11/// Standard for file size units
12/// 
13/// 文件大小单位标准
14#[derive(Debug, Clone, Copy)]
15pub enum SizeStandard {
16    /// SI standard (base-1000, units: KB, MB)
17    /// 
18    /// SI 标准 (base-1000, 单位 KB, MB)
19    SI,
20    /// IEC standard (base-1024, units: KiB, MiB)
21    /// 
22    /// IEC 标准 (base-1024, 单位 KiB, MiB)
23    IEC,
24}
25
26/// Format a numeric value and return (formatted_value, unit_index)
27/// 
28/// 格式化数值部分,返回 (formatted_value, unit_index)
29pub(crate) fn format_parts(
30    mut value: Decimal,
31    base: Decimal,
32    units: &'static [&'static str],
33) -> (String, &'static str) {
34    let mut unit_index = 0;
35
36    while value >= base && unit_index < units.len() - 1 {
37        value /= base;
38        unit_index += 1;
39    }
40
41    let formatted_value = format_decimal_value(value);
42    (formatted_value, units[unit_index])
43}
44
45/// Format a decimal value with automatic decimal places selection based on value
46/// - Less than 10: 2 decimal places
47/// - 10 or greater: 1 decimal place
48///
49/// 格式化十进制数值,根据值的大小自动选择小数位数
50/// - 小于10: 显示2位小数
51/// - 大于等于10: 显示1位小数
52#[inline]
53pub(crate) fn format_decimal_value(value: Decimal) -> String {
54    if value < Decimal::from(10) {
55        let mut rounded = value.round_dp(2);
56        rounded.rescale(2);
57        rounded.to_string()
58    } else {
59        let mut rounded = value.round_dp(1);
60        rounded.rescale(1);
61        rounded.to_string()
62    }
63}