file_type_enum 0.11.1

Simple and minimal enum with one variant for each file type.
Documentation

file_type_enum

Crates.io Rust License Docs.rs

A enum with one variant for each file type.

Cross-platform, this crate is made of a single small lib.rs with a very simple enum implementation so that you don't have to rewrite your own.

Enum [FileType]:

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

Examples:

use file_type_enum::FileType;

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!"

Errors:

  • If path does not exist, or
  • Current user don't have permissions to read fs::Metadata from path.

For each variant, there is also a short hand method:

let ft = FileType::from(path);
if ft.is_regular() { ... }
if ft.is_directory() { ... }
if ft.is_symlink() { ... }
if ft.is_block_device() { ... }
if ft.is_char_device() { ... }
if ft.is_fifo() { ... }
if ft.is_socket() { ... }
```rust

```rust
use file_type_enum::FileType;

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.");
}

If path points to a symlink, from_path(path) follows it, so the returned type can never be a symlink.

To avoid this, use FileType::from_symlink_path, this don't follow, and can return a symlink.

use file_type_enum::FileType;

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!"

Conversions

  • From std::fs::FileType.
  • From and into libc::mode_t (enable mode-t-conversion optional feature).

Future versions note:

Changes might occur on std API for Windows (related to symlinks), I personally don't consider this part very stable.

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 an issue or PR in the repository.
  • Leave a star on GitHub.
  • Use it!!!

TODO:

Add example on how to add the crate with the feature to the Cargo.toml.