Skip to main content

Crate tpt_mime_pure

Crate tpt_mime_pure 

Source
Expand description

§tpt-mime-pure

docs.rs crates.io

Pure Rust MIME type detection via magic bytes and file extension fallback. 100% no_std compatible.

No OS calls, no shelling out to file. Works in minimal Docker containers, WASM, and embedded targets.

§Features

  • Magic byte detection — checks the file’s leading bytes against known signatures
  • Extension fallbackdetect_by_extension("pdf") for when you only have a filename
  • no_std compatible — works without the standard library (with alloc); disable the default std feature
  • No dependencies — zero external crates
  • ~28 common formats — images, video, audio, archives, documents, binaries

§Usage

use tpt_mime_pure::{detect, MimeType};

let jpeg_header = &[0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10];
assert_eq!(detect(jpeg_header), Some(MimeType::Jpeg));
println!("{}", MimeType::Jpeg.as_str()); // image/jpeg

§Extension fallback

use tpt_mime_pure::{detect_by_extension, MimeType};

assert_eq!(detect_by_extension("pdf"), Some(MimeType::Pdf));
assert_eq!(detect_by_extension("PDF"), Some(MimeType::Pdf)); // case-insensitive

§File detection (requires std feature, enabled by default)

use tpt_mime_pure::detect_file;

let mime = detect_file("/path/to/file.jpg").unwrap();

§no_std usage

[dependencies]
tpt-mime-pure = { version = "0.1", default-features = false }

§Supported Types

VariantMIMEMagic bytes
Jpegimage/jpegFF D8 FF
Pngimage/png89 50 4E 47 ...
Gifimage/gif47 49 46 38
WebPimage/webpRIFF + WEBP
Bmpimage/bmp42 4D
Icoimage/x-icon00 00 01 00
Tiffimage/tiffII or MM header
Mp4video/mp4ftyp box at offset 4 (unknown brand)
QuickTimevideo/quicktimeftyp brand qt
ThreeGpvideo/3gppftyp brand 3gp4/3gp5/3gp6
Mkvvideo/x-matroskaEBML 1A 45 DF A3, DocType matroska
WebMvideo/webmEBML 1A 45 DF A3, DocType webm
Avivideo/x-msvideoRIFF + AVI
Heicimage/heicftyp brand heic/heix/hevc/hevx
Heifimage/heifftyp brand mif1
Avifimage/avifftyp brand avif/avis
Mp3audio/mpegID3 or FF FB
Wavaudio/wavRIFF + WAVE
Flacaudio/flacfLaC
Oggaudio/oggOggS
Pdfapplication/pdf%PDF
Zipapplication/zipPK\x03\x04
Gzipapplication/gzip1F 8B
Tarapplication/x-tarustar at offset 257
Sqliteapplication/x-sqlite3SQLite format 3\0
Wasmapplication/wasm\0asm
Elfapplication/x-elf\x7FELF
PeExeapplication/x-msdownloadMZ

§License

Licensed under either of Apache License 2.0 or MIT at your option. Pure Rust MIME type detection via magic bytes and file extension fallback. See detect, detect_by_extension, and MimeType.

Enums§

MimeType
A detected MIME type.

Functions§

detect
Detect MIME type from the leading bytes of a file.
detect_by_extension
Detect MIME type from a file extension (without leading dot, case-insensitive).
detect_file
Read up to 512 bytes from path and detect the MIME type.