11 个版本 (3 个稳定版)
| 2.0.1 | 2023 年 10 月 3 日 | 
|---|---|
| 1.0.1 | 2022 年 8 月 17 日 | 
| 0.11.1 | 2020 年 10 月 31 日 | 
| 0.10.3 | 2020 年 9 月 14 日 | 
| 0.9.1 | 2020 年 9 月 13 日 | 
#311 在 文件系统
每月 336 次下载
用于 fs-tree
15KB
167 行
file_type_enum
一个针对每种文件类型的变体枚举。
pub enum FileType {
    Regular,
    Directory,
    Symlink,
    BlockDevice, // unix only
    CharDevice,  // unix only
    Fifo,        // unix only
    Socket,      // unix only
}
替代方案
- 如果您想要一个枚举树,请查看 fs-treecrate。
- 如果您不需要枚举,请检查 std中的这些方法
示例
use std::{fs, io, path::Path};
use file_type_enum::FileType;
fn move_file(from: &Path, to: &Path) -> io::Result<()> {
    let from_type = FileType::symlink_read_at(from)?;
    let to_type = FileType::symlink_read_at(to)?;
    use FileType::{Directory, Regular, Symlink};
    match (from_type, to_type) {
        (Directory, Directory) => {
            println!("Replacing directory {to:?} by directory {from:?}.");
        }
        (Regular, Regular) | (Symlink, Symlink) => {
            // Might print:
            //       "Overwriting regular file at PATH."
            //       "Overwriting symbolic link at PATH."
            println!("Overwriting {from_type} at {to:?} by {to:?}.");
        }
        (_, Directory) => {
            println!("Moving file at {from:?} into folder {to:?}.");
            fs::rename(from, to)?;
        }
        (_, _) => {
            // Might print:
            // -   "Cannot overwrite regular file  with a symbolic link."
            // -   "Cannot overwrite directory     with a symbolic link."
            // -   "Cannot overwrite symbolic link with a regular file."
            panic!("Cannot overwrite {to_type}     with a {from_type}.");
        }
    }
    Ok(())
}
如示例 FileType 所示,也实现了 Display。
注意
请注意,与 std 函数一样,FileType::read_at 也遵循符号链接,因此无法获取 FileType::Symlink 变体。如果您想要符号链接感知,请使用 FileType::symlink_read_at 代替。
转换
- 从 AsRef<Path>、fs::Metadata和 std 的FileType。
- 从和到 libc::mode_t(通过功能"mode-t-conversion")。