| Crates.io | mime-type |
| lib.rs | mime-type |
| version | 0.2.0 |
| created_at | 2025-10-21 20:02:48.591323+00 |
| updated_at | 2025-10-22 08:44:43.594443+00 |
| description | A Rust library for handling MIME types. |
| homepage | |
| repository | https://github.com/secana/mime-type |
| max_upload_size | |
| id | 1894362 |
| size | 51,451 |
A Rust library for working with MIME types and file extensions. This crate provides a simple, ergonomic API for converting between file extensions and their corresponding MIME type strings.
use mime_type::{MimeType, MimeFormat};
// Get MIME type from extension
let mime = MimeType::from_ext("png");
assert_eq!(mime.unwrap().to_string(), "image/png");
// Works with multiple extensions for the same format
let jpeg1 = MimeType::from_ext("jpg");
let jpeg2 = MimeType::from_ext("jpeg");
assert_eq!(jpeg1.unwrap().to_string(), jpeg2.unwrap().to_string());
// Returns None for unknown extensions
let unknown = MimeType::from_ext("unknown");
assert!(unknown.is_none());
use mime_type::{MimeType, MimeFormat};
let mime = MimeType::from_mime("video/mp4");
match mime {
Some(MimeType::Video(video)) => println!("It's a video: {}", video),
_ => println!("Not a video"),
}
use mime_type::{Image, Video, Audio, Document, MimeFormat};
// Directly use category enums
let png = Image::Png;
println!("{}", png); // Outputs: image/png
// Check extensions for specific categories
if let Some(mime) = Image::from_ext("webp") {
println!("Found image: {}", mime);
}
use mime_type::{MimeType, MimeFormat};
let mime = MimeType::from_ext("mp3").unwrap();
match mime {
MimeType::Audio(audio) => println!("Audio file: {}", audio),
MimeType::Video(video) => println!("Video file: {}", video),
MimeType::Image(image) => println!("Image file: {}", image),
_ => println!("Other file type"),
}
JPEG, PNG, GIF, WebP, CR2, TIFF, BMP, HEIF, AVIF, JXR, PSD, ICO, ORA, DjVu
MP4, M4V, MKV, WebM, MOV, AVI, WMV, MPG, FLV
MIDI, MP3, M4A, OGG, FLAC, WAV, AMR, AAC, AIFF, DSF, APE
DOC, DOCX, XLS, XLSX, PPT, PPTX, ODT, ODS, ODP
ZIP, TAR, RAR, GZ, BZ2, 7Z, XZ, PDF, and many more
TTF, OTF, WOFF, WOFF2
EPUB, MOBI
WASM, EXE, DLL, ELF, and various executable formats
Some file formats share the same MIME type but have different extensions:
EXE and DLL: Both map to application/vnd.microsoft.portable-executable
EXE variant is returnedELF and OBJ: Both map to application/x-executable
ELF variant is returnedEPUB files appear in both Archive and Book categories:
Archive::Epub maps to application/epub+zipBook::Epub also maps to application/epub+zipBehavior:
MimeType::from_ext("epub") returns MimeType::Archive(Archive::Epub) (Archive is checked first)MimeType::from_mime("application/epub+zip") returns MimeType::Archive(Archive::Epub)Book::Epub variant is effectively unreachable through the standard APIRecommendation: Use Archive::Epub for EPUB files, or access Book::Epub directly if you need the Book variant specifically.
Font formats have some MIME type overlap:
TTF and OTF: Both return application/font-sfnt
TTF is returnedWOFF and WOFF2: Both currently return application/font-woff
font/woff2WOFF is returnedSome conversions are not perfectly reversible:
// Extension -> MIME -> Extension may not round-trip
let mime = MimeType::from_ext("dll").unwrap();
// mime.to_string() == "application/vnd.microsoft.portable-executable"
// But from_mime() would return Application::Exe, not Dll
.JPG (uppercase) will not match, only .jpg (lowercase) willTo publish a new version of the crate to crates.io, use the publish.sh script:
./publish.sh <version>
For example:
./publish.sh 0.2.0
The script will:
main branchCargo.tomlCargo.tomlNote: The actual publishing to crates.io is done by the GitHub Actions workflow.