Crates.io | file_type_enum |
lib.rs | file_type_enum |
version | 2.0.1 |
source | src |
created_at | 2020-09-13 04:58:32.889954 |
updated_at | 2023-10-03 19:40:03.628942 |
description | An enum with a variant for each file type. |
homepage | |
repository | https://github.com/marcospb19/file_type_enum |
max_upload_size | |
id | 288053 |
size | 16,192 |
An enum with a variant for each file type.
pub enum FileType {
Regular,
Directory,
Symlink,
BlockDevice, // unix only
CharDevice, // unix only
Fifo, // unix only
Socket, // unix only
}
fs-tree
.std
instead:
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(())
}
As shown in the example FileType
also implements Display
.
Note that, like std
functions, FileType::read_at
follows symlinks, therefore it is
impossible to get the FileType::Symlink
variant. If you want symlink-awareness, use
FileType::symlink_read_at
instead.
AsRef<Path>
, fs::Metadata
and std's FileType
.libc::mode_t
(via the feature "mode-t-conversion"
).