pub struct FileType { /* private fields */ }Expand description
A file type. The file type is determined by examining the file or bytes against known file signatures and file extensions.
§Example
Detect a Java class file from bytes:
use file_type::FileType;
let file_type = FileType::from_bytes(b"\xCA\xFE\xBA\xBE");
assert_eq!(file_type.extensions(), vec!["class"]);Retrieve a file type from an extension:
use file_type::FileType;
let file_types = FileType::from_extension("png");
let file_type = file_types.first().expect("file format");
assert_eq!(file_type.media_types(), vec!["image/png"]);Retrieve a file type from a media type:
use file_type::FileType;
let file_types = FileType::from_media_type("image/png");
let file_type = file_types.first().expect("file format");
assert_eq!(file_type.extensions(), vec!["png"]);Implementations§
Source§impl FileType
impl FileType
Sourcepub fn source_type(&self) -> &SourceType
pub fn source_type(&self) -> &SourceType
Get the source for this file type.
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Get the human-readable name of the file type
§Example
use file_type::FileType;
let file_types = FileType::from_media_type("image/png");
let file_type = file_types.first().expect("file format");
assert!(file_type.name().contains("Portable Network Graphics"));Sourcepub fn extensions(&self) -> &[&str]
pub fn extensions(&self) -> &[&str]
Get the file type extensions
§Example
use file_type::FileType;
let file_types = FileType::from_media_type("image/png");
let file_type = file_types.first().expect("file format");
assert_eq!(file_type.extensions(), vec!["png"]);Sourcepub fn media_types(&self) -> &[&str]
pub fn media_types(&self) -> &[&str]
Get the Media Type
§Example
use file_type::FileType;
let file_types = FileType::from_extension("png");
let file_type = file_types.first().expect("file format");
assert_eq!(file_type.media_types(), vec!["image/png"]);Sourcepub fn from_extension<S: AsRef<str>>(extension: S) -> &'static [&'static Self]
pub fn from_extension<S: AsRef<str>>(extension: S) -> &'static [&'static Self]
Get the file types for a given extension.
§Example
use file_type::FileType;
let file_types = FileType::from_extension("png");
let file_type = file_types.first().expect("file format");
assert_eq!(file_type.media_types(), vec!["image/png"]);Sourcepub fn from_media_type<S: AsRef<str>>(media_type: S) -> &'static [&'static Self]
pub fn from_media_type<S: AsRef<str>>(media_type: S) -> &'static [&'static Self]
Get the file types for a given media type.
§Example
use file_type::FileType;
let file_types = FileType::from_media_type("image/png");
let file_type = file_types.first().expect("file format");
assert_eq!(file_type.extensions(), vec!["png"]);Sourcepub fn from_bytes<B: AsRef<[u8]>>(bytes: B) -> &'static Self
pub fn from_bytes<B: AsRef<[u8]>>(bytes: B) -> &'static Self
Attempt to determine the FileType from a byte slice.
§Example
use file_type::FileType;
let file_type = FileType::from_bytes(b"\xCA\xFE\xBA\xBE");
assert_eq!(file_type.extensions(), vec!["class"]);Sourcepub fn try_from_reader<R: Read>(reader: R) -> Result<&'static Self>
pub fn try_from_reader<R: Read>(reader: R) -> Result<&'static Self>
Attempt to determine the FileType from a reader.
§Errors
if the file type is unknown
§Example
use file_type::FileType;
use std::io::BufReader;
let bytes = b"\xCA\xFE\xBA\xBE";
let reader = BufReader::new(&bytes[..]);
let file_type = FileType::try_from_reader(reader).expect("file type not found");
assert_eq!(file_type.extensions(), vec!["class"]);Sourcepub fn try_from_file<P: AsRef<Path>>(path: P) -> Result<&'static Self>
pub fn try_from_file<P: AsRef<Path>>(path: P) -> Result<&'static Self>
Attempt to determine the FileType from a file.
§Errors
if the file type is unknown
§Example
use file_type::FileType;
use std::path::Path;
let file_path = Path::new("image.png");
let file_type = FileType::try_from_file(file_path).expect("file type not found");
assert_eq!(file_type.extensions(), vec!["png"]);
assert_eq!(file_type.media_types(), vec!["image/png"]);Trait Implementations§
Source§impl Ord for FileType
impl Ord for FileType
Source§impl PartialOrd for FileType
impl PartialOrd for FileType
impl Eq for FileType
impl StructuralPartialEq for FileType
Auto Trait Implementations§
impl Freeze for FileType
impl RefUnwindSafe for FileType
impl Send for FileType
impl Sync for FileType
impl Unpin for FileType
impl UnwindSafe for FileType
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more