[][src]Crate file_type_enum

This crate grants a enum with one variant for each file type.

Cross-platform and small, this crate has a single file with around 150 lines of source code. Simplest implementation. if you want to check file types, here's a enum for you, don't rewrite it.

Enum FileType:

pub enum FileType {
    File,
    Directory,
    Symlink,
    BlockDevice, // unix only
    CharDevice,  // unix only
    Fifo,        // unix only
    Socket,      // unix only
}

Examples:

use file_type_enum::FileType;

fn main() {
    let path = "/tmp";
    let file_type = FileType::from_path(path).unwrap();

    println!("There's a {} at {}!", file_type, path);
    // Outputs: "There's a directory at /tmp!"
}

Note: FileType::from_path(path) returns a io::Error if:

  • Path does not exist.
  • The user lacks permissions to read metadata from path.

For each variant, there's a short hand .is_VARIANT():

file_type.is_file() for FileType::File,
file_type.is_directory() for FileType::Directory,
file_type.is_symlink() for FileType::Symlink,
And so on...

use file_type_enum::FileType;

fn main() {
    let path = ".git";
    let file_type = FileType::from_path(path).unwrap();

    if file_type.is_directory() {
        println!("We are at the root a git repository.");
    }
}

By default, if path points to symlink, then FileType::from_path() considers the path at the symlink's target location (this implies that the returned file type can't be FileType::Symlink).

If you don't want to follow symlinks, use FileType::from_symlink_path instead, this function may return Ok(FileType::Symlink).

use file_type_enum::FileType;

fn main() {
    let path = "/dev/stdout";
    let file_type = FileType::from_symlink_path(path).unwrap();

    println!("There's a {} at {}!", file_type, path);
    // Outputs: "There's a symbolic link at /dev/stdout!"
}

The conversion FileType::from::<fs::FileType> is also available for convenience.

Helping and contributing:

It's easy to contribute to this crate, here are some options:

  • Share it to a friend.
  • Help improve this README.md, even with little details.
  • Open issues to the repository, or send a PR.
  • Leave a star on GitHub.
  • Use it!

TODO:

Add optional feature to transform from and into libc's mode_t

Enums

FileType

Enum with a variant for each file type.