net-bytes 0.1.0

A Rust library for handling file sizes, download speeds, and download acceleration with support for both SI and IEC standards
Documentation
# Net Bytes

[![Crates.io](https://img.shields.io/crates/v/net-bytes)](https://crates.io/crates/net-bytes)
[![文档](https://docs.rs/net-bytes/badge.svg)](https://docs.rs/net-bytes)
[![许可证](https://img.shields.io/crates/l/net-bytes)](LICENSE-MIT)

一个用于处理文件大小、下载速度和下载加速度的 Rust 库,支持 SI (base-1000) 和 IEC (base-1024) 两种标准。

## 功能

- **文件大小格式化**:自动选择合适的单位格式化文件大小(B、KB/KiB、MB/MiB 等)
- **下载速度计算**:计算和格式化下载速度
- **下载加速度**:测量和格式化下载加速度
- **双标准支持**:同时支持 SI (base-1000) 和 IEC (base-1024) 标准
- **精确的十进制处理**:使用 `rust_decimal` 进行精确计算

## 安装

在 `Cargo.toml` 中添加:

```toml
[dependencies]
net-bytes = "0.1.0"
```

## 使用示例

### 文件大小

```rust
use net_bytes::{FileSize, SizeStandard};
use std::num::NonZeroU64;

// 创建 1.5 MB 的文件大小(SI 标准)
let size = FileSize::new(NonZeroU64::new(1_500_000).unwrap(), SizeStandard::SI);
println!("大小: {}", size); // 输出: 1.50 MB

// 使用 IEC 标准 (base-1024)
let size_iec = FileSize::new(NonZeroU64::new(1_500_000).unwrap(), SizeStandard::IEC);
println!("大小 (IEC): {}", size_iec); // 输出: 1.43 MiB
```

### 下载速度

```rust
use net_bytes::{DownloadSpeed, SizeStandard};
use std::time::Duration;
use std::num::NonZeroU64;

// 从字节数和时间创建下载速度
let speed = DownloadSpeed::new(
    NonZeroU64::new(10_000_000).unwrap(), // 10 MB
    Duration::from_secs(2),               // 2 秒内
    SizeStandard::SI
);
println!("速度: {}/s", speed); // 输出: 5.00 MB/s
```

### 下载加速度

```rust
use net_bytes::{DownloadAcceleration, SizeStandard};
use std::time::Duration;
use std::num::NonZeroU64;

// 从初速度、末速度和时间创建下载加速度
let acceleration = DownloadAcceleration::new(
    NonZeroU64::new(1_000_000).unwrap(),  // 初速度 1 MB/s
    NonZeroU64::new(5_000_000).unwrap(),  // 末速度 5 MB/s
    Duration::from_secs(4),               // 4 秒内
    SizeStandard::SI
);
println!("加速度: {}/s²", acceleration); // 输出: 1.00 MB/s²
```

## API 参考

### FileSize

- `new(bytes: NonZeroU64, standard: SizeStandard) -> Self` - 创建新的文件大小
- `get_si_parts() -> (String, &'static str)` - 获取 SI 标准下的格式化值和单位
- `get_iec_parts() -> (String, &'static str)` - 获取 IEC 标准下的格式化值和单位
- `as_u64() -> u64` - 获取字节数 (u64)
- `get_nonzero() -> NonZeroU64` - 获取 NonZeroU64 类型的字节数

### DownloadSpeed

- `new(bytes: NonZeroU64, duration: Duration, standard: SizeStandard) -> Self` - 从字节数和时间创建
- `from_raw(bytes_per_second: NonZeroU64, standard: SizeStandard) -> Self` - 从字节/秒创建
- `as_decimal() -> Decimal` - 获取 Decimal 类型的速度值
- `as_u64() -> u64` - 获取 u64 类型的速度值(向下取整)
- `get_si_parts() -> (String, &'static str)` - 获取 SI 标准下的格式化值和单位
- `get_iec_parts() -> (String, &'static str)` - 获取 IEC 标准下的格式化值和单位

### DownloadAcceleration

- `new(initial_speed: NonZeroU64, final_speed: NonZeroU64, duration: Duration, standard: SizeStandard) -> Self` - 从速度变化和时间创建
- `from_raw(bytes_per_second_sq: i64, standard: SizeStandard) -> Self` - 从字节/秒²创建
- `as_i64() -> i64` - 获取 i64 类型的加速度值(向下取整)
- `get_si_parts() -> (String, &'static str)` - 获取 SI 标准下的格式化值和单位
- `get_iec_parts() -> (String, &'static str)` - 获取 IEC 标准下的格式化值和单位

## 许可证

根据您的选择,使用以下任一许可证:

- [MIT 许可证]LICENSE-MIT
- [Apache 许可证 2.0 版]http://www.apache.org/licenses/LICENSE-2.0