Expand description
§tpt-mime-pure
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 fallback —
detect_by_extension("pdf")for when you only have a filename no_stdcompatible — works without the standard library (withalloc); disable the defaultstdfeature- 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
| Variant | MIME | Magic bytes |
|---|---|---|
Jpeg | image/jpeg | FF D8 FF |
Png | image/png | 89 50 4E 47 ... |
Gif | image/gif | 47 49 46 38 |
WebP | image/webp | RIFF + WEBP |
Bmp | image/bmp | 42 4D |
Ico | image/x-icon | 00 00 01 00 |
Tiff | image/tiff | II or MM header |
Mp4 | video/mp4 | ftyp box at offset 4 (unknown brand) |
QuickTime | video/quicktime | ftyp brand qt |
ThreeGp | video/3gpp | ftyp brand 3gp4/3gp5/3gp6 |
Mkv | video/x-matroska | EBML 1A 45 DF A3, DocType matroska |
WebM | video/webm | EBML 1A 45 DF A3, DocType webm |
Avi | video/x-msvideo | RIFF + AVI |
Heic | image/heic | ftyp brand heic/heix/hevc/hevx |
Heif | image/heif | ftyp brand mif1 |
Avif | image/avif | ftyp brand avif/avis |
Mp3 | audio/mpeg | ID3 or FF FB |
Wav | audio/wav | RIFF + WAVE |
Flac | audio/flac | fLaC |
Ogg | audio/ogg | OggS |
Pdf | application/pdf | %PDF |
Zip | application/zip | PK\x03\x04 |
Gzip | application/gzip | 1F 8B |
Tar | application/x-tar | ustar at offset 257 |
Sqlite | application/x-sqlite3 | SQLite format 3\0 |
Wasm | application/wasm | \0asm |
Elf | application/x-elf | \x7FELF |
PeExe | application/x-msdownload | MZ |
§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§
- Mime
Type - 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
pathand detect the MIME type.