file_type/
lib.rs

1//! # `file_type`
2//!
3//! [![Code Coverage](https://codecov.io/gh/theseus-rs/file-type/branch/main/graph/badge.svg)](https://codecov.io/gh/theseus-rs/file-type)
4//! [![Benchmarks](https://img.shields.io/badge/%F0%9F%90%B0_bencher-enabled-6ec241)](https://bencher.dev/perf/theseus-rs-file-type)
5//! [![License](https://img.shields.io/crates/l/file_type)](https://github.com/theseus-rs/file-type#license)
6//! [![Semantic Versioning](https://img.shields.io/badge/%E2%9A%99%EF%B8%8F_SemVer-2.0.0-blue)](https://semver.org/spec/v2.0.0.html)
7//!
8//! ## Getting Started
9//!
10//! A file type.  The file type is determined by examining the file or bytes against known file
11//! signatures and file extensions.
12//!
13//! Signature, extension and media type data are provided by:
14//! * [Apache HTTPD](https://github.com/apache/httpd/blob/trunk/docs/conf/mime.types)
15//! * [IANA](https://www.iana.org/assignments/media-types/media-types.xml)
16//! * [Linguist](https://github.com/github-linguist/linguist/blob/main/lib/linguist/languages.yml)
17//! * [The National Archives PRONOM](https://www.nationalarchives.gov.uk/pronom/)
18//! * [Wikidata](https://www.wikidata.org/wiki/Wikidata:WikiProject_Informatics/Structures/File_formats/List)
19//!
20//! # Example
21//!
22//! Detect a file type from bytes:
23//! ```
24//! use file_type::FileType;
25//!
26//! let file_type = FileType::from_bytes(b"\xCA\xFE\xBA\xBE");
27//! assert_eq!(file_type.name(), "Java class file");
28//! assert_eq!(file_type.extensions(), vec!["class"]);
29//! ```
30//!
31//! Retrieve a file type from an extension:
32//! ```
33//! use file_type::FileType;
34//!
35//! let file_types = FileType::from_extension("png");
36//! let file_type = file_types.first().expect("file format");
37//! assert_eq!(file_type.media_types(), vec!["image/png"]);
38//! ```
39//!
40//! Retrieve a file type from a media type:
41//! ```
42//! use file_type::FileType;
43//!
44//! let file_types = FileType::from_media_type("image/png");
45//! let file_type = file_types.first().expect("file format");
46//! assert_eq!(file_type.extensions(), vec!["png"]);
47//! ```
48//!
49//! ## Feature flags
50//!
51//! | Name       | Description                                                                                                                | Default? |
52//! |------------|----------------------------------------------------------------------------------------------------------------------------|----------|
53//! | `httpd`    | Enables [Apache HTTPD](https://github.com/apache/httpd/blob/trunk/docs/conf/mime.types) file types                         | No       |
54//! | `iana`     | Enables [IANA](https://www.iana.org/assignments/media-types/media-types.xml) file types                                    | No       |
55//! | `linguist` | Enables [Linguist](https://github.com/github-linguist/linguist/blob/main/lib/linguist/languages.yml) file types            | No       |
56//! | `pronom`   | Enables [PRONOM](https://www.nationalarchives.gov.uk/PRONOM) file types                                                    | No       |
57//! | `std`      | Enables support for the Rust standard library                                                                              | Yes      |
58//! | `wikidata` | Enables [Wikidata](https://www.wikidata.org/wiki/Wikidata:WikiProject_Informatics/Structures/File_formats/List) file types | Yes      |
59//!
60//! ## Supported File Types
61//!
62//! [List of supported file types](https://github.com/theseus-rs/file-type/blob/main/FILETYPES.md)
63//!
64//! ## Safety
65//!
66//! This crate uses `#![forbid(unsafe_code)]` to ensure everything is implemented in 100% safe Rust.
67
68#![cfg_attr(not(feature = "std"), no_std)]
69#![forbid(unsafe_code)]
70#![forbid(clippy::allow_attributes)]
71#![allow(dead_code)]
72#![deny(clippy::pedantic)]
73#![deny(clippy::unwrap_in_result)]
74#![deny(clippy::unwrap_used)]
75extern crate alloc;
76extern crate core;
77
78mod error;
79mod extensions;
80mod file_type;
81mod file_types;
82#[doc(hidden)]
83pub mod format;
84mod media_types;
85mod signatures;
86pub mod sources;
87
88pub use error::{Error, Result};
89pub use file_type::FileType;