7个版本

0.1.7 2024年7月23日
0.1.6 2024年7月23日

#3 in #guess

Download history 76/week @ 2024-07-14 356/week @ 2024-07-21 28/week @ 2024-07-28

460 每月下载量

MIT 许可证

34KB
575 行代码

mime_more

mime_moremime 包的扩展,提供了处理MIME类型的额外功能。⚡️🦀

功能

  • 基于 mime 构建,继承其所有功能。
  • 基于扩展的MIME猜测:
    • extension 功能:使用 mime_guess 包根据文件扩展名猜测MIME类型。
    • extension-light 功能extension 功能的轻量级版本,仅支持常用扩展。比 extension 快8倍!
  • 基于魔法字节的MIME猜测(《magic》功能):使用 infer 包根据文件魔法字节确定MIME类型。
  • 纹理验证(《texture》功能):提供高性能的纹理验证方法。
  • 生成数据URL(《dataurl》功能):生成和解析相对较短的长度和较少时间的data URLs。编码 Source Han Sans 字体大约需要1.5毫秒,解码大约需要6毫秒!

用法

mime_more 添加到您的 Cargo.toml

[dependencies]
mime_more = "0.1.7"

示例用法

直接解析MIME类型

use mime_more::Mime;

fn main() {
    let mime = Mime::from_extension("html").unwrap();
    println!("{}", mime);
}

通过文件路径猜测MIME类型

需要 extension (函数名为 from_path) 或 extension-light (函数名为 from_path_light) 功能。

use mime_more::Mime;

fn main() {
    let mime = Mime::from_path_light("index.html").unwrap().to_string();
    assert_eq!(mime, "text/html".to_string());
}

通过文件魔法字节推断MIME类型

需要 magic 功能。

use mime_more::Mime;

fn main() {
    let data = &[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x11, 0x45, 0x14, 0x19, 0x19, 0x81, 0x0];
    let mime = Mime::from_magic(data).unwrap().to_string();
    assert_eq!(mime, "image/png".to_string());
    // `89 50 4E 47 0D 0A 1A 0A` is the magic byte sequence for PNG.
}

验证纹理

需要 texture 功能。

use mime_more::Mime;

fn main() {
    let data = Mime::from_str("image/png").unwrap();
    assert!(data.is_texture());
    
    let data = Mime::from_str("application/json").unwrap();
    assert!(!data.is_texture());
}

生成数据URL

需要 dataurl 功能。

use mime_more::{Mime, dataurl::Dataurl};

fn main() {
    let dataurl = Dataurl::from_path("path/to/file.jpg").unwrap();
    println!("{}", dataurl); // We implemented Display for Dataurl.
}

许可证

本项目使用MIT许可证。

作者

本项目由 Ethan Goh 开发。

基准结果

     Running benches/function_benchmark.rs (target/release/deps/function_benchmark-ce680cff84e61f76)
Gnuplot not found, using plotters backend
ModuleType::from_known_str
                        time:   [25.839 ns 25.892 ns 25.945 ns]
                        change: [-3.5366% -3.3160% -3.0938%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

ModuleType::from_ext    time:   [288.99 ns 289.51 ns 290.04 ns]
                        change: [-2.5172% -2.2286% -1.9559%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  5 (5.00%) high mild
  1 (1.00%) high severe

ModuleType::from_ext_light
                        time:   [34.723 ns 34.816 ns 34.918 ns]
                        change: [-1.9821% -1.7247% -1.4603%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  3 (3.00%) high mild
  1 (1.00%) high severe

ModuleType::from_content
                        time:   [58.444 ns 58.535 ns 58.623 ns]
                        change: [-0.9603% -0.6856% -0.4151%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 4 outliers among 100 measurements (4.00%)
  4 (4.00%) high mild

ModuleType::is_texture_std
                        time:   [2.3147 ns 2.3186 ns 2.3239 ns]
                        change: [-1.9097% -1.5987% -1.1806%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high severe

ModuleType::is_texture_mime
                        time:   [32.679 ns 32.756 ns 32.831 ns]
                        change: [-1.7499% -1.2054% -0.6722%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) high mild
  1 (1.00%) high severe

ModuleType::is_texture_manual
                        time:   [2.0520 ns 2.0657 ns 2.0824 ns]
                        change: [-8.7075% -8.0549% -7.1646%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) high severe

ModuleType::is_texture_std #2
                        time:   [7.5723 ns 7.5904 ns 7.6098 ns]
                        change: [-8.9537% -8.4932% -7.9971%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

ModuleType::is_texture_mime #2
                        time:   [27.372 ns 27.453 ns 27.570 ns]
                        change: [+0.4884% +0.9216% +1.4523%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 9 outliers among 100 measurements (9.00%)
  2 (2.00%) low mild
  3 (3.00%) high mild
  4 (4.00%) high severe

ModuleType::is_texture_manual #2
                        time:   [6.1268 ns 6.1600 ns 6.2030 ns]
                        change: [-0.7090% -0.3402% +0.0458%] (p = 0.08 > 0.05)
                        No change in performance detected.
Found 7 outliers among 100 measurements (7.00%)
  4 (4.00%) high mild
  3 (3.00%) high severe

ModuleType::is_texture_std #3
                        time:   [37.125 µs 37.223 µs 37.331 µs]
                        change: [-2.5728% -2.2761% -1.9794%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  1 (1.00%) low severe
  1 (1.00%) low mild
  3 (3.00%) high mild
  1 (1.00%) high severe

ModuleType::is_texture_mime #3
                        time:   [27.118 ns 27.208 ns 27.323 ns]
                        change: [-0.6506% -0.2591% +0.4263%] (p = 0.35 > 0.05)
                        No change in performance detected.
Found 5 outliers among 100 measurements (5.00%)
  2 (2.00%) high mild
  3 (3.00%) high severe

ModuleType::is_texture_manual #3
                        time:   [10.296 µs 11.559 µs 12.854 µs]
                        change: [-47.428% -43.320% -38.513%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  2 (2.00%) high mild
  8 (8.00%) high severe

Dataurl::from_path      time:   [7.2911 µs 7.3147 µs 7.3452 µs]
                        change: [+0.5418% +0.8471% +1.2078%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 8 outliers among 100 measurements (8.00%)
  4 (4.00%) high mild
  4 (4.00%) high severe

Dataurl::from_str       time:   [97.890 ns 98.028 ns 98.169 ns]
                        change: [+0.0012% +0.2894% +0.5681%] (p = 0.04 < 0.05)
                        Change within noise threshold.
Found 3 outliers among 100 measurements (3.00%)
  1 (1.00%) low mild
  2 (2.00%) high mild

依赖项

~0.7–1.4MB
~30K SLoC