|
| 1 | +//! The `FileType` struct. |
| 2 | +
|
| 3 | +use crate::filesystem::primitives::ImplFileTypeExt; |
| 4 | + |
| 5 | +/// `FileType`'s inner state. |
| 6 | +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
| 7 | +enum Inner { |
| 8 | + /// A directory. |
| 9 | + Dir, |
| 10 | + |
| 11 | + /// A file. |
| 12 | + File, |
| 13 | + |
| 14 | + /// An unknown entity. |
| 15 | + Unknown, |
| 16 | + |
| 17 | + /// A `FileTypeExt` type. |
| 18 | + Ext(ImplFileTypeExt), |
| 19 | +} |
| 20 | + |
| 21 | +/// A structure representing a type of file with accessors for each file type. |
| 22 | +/// |
| 23 | +/// This corresponds to [`std::fs::FileType`]. |
| 24 | +/// |
| 25 | +/// <details> |
| 26 | +/// We need to define our own version because the libstd `FileType` doesn't |
| 27 | +/// have a public constructor that we can use. |
| 28 | +/// </details> |
| 29 | +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
| 30 | +#[repr(transparent)] |
| 31 | +pub struct FileType(Inner); |
| 32 | + |
| 33 | +impl FileType { |
| 34 | + /// Creates a `FileType` for which `is_dir()` returns `true`. |
| 35 | + #[inline] |
| 36 | + pub const fn dir() -> Self { |
| 37 | + Self(Inner::Dir) |
| 38 | + } |
| 39 | + |
| 40 | + /// Creates a `FileType` for which `is_file()` returns `true`. |
| 41 | + #[inline] |
| 42 | + pub const fn file() -> Self { |
| 43 | + Self(Inner::File) |
| 44 | + } |
| 45 | + |
| 46 | + /// Creates a `FileType` for which `is_unknown()` returns `true`. |
| 47 | + #[inline] |
| 48 | + pub const fn unknown() -> Self { |
| 49 | + Self(Inner::Unknown) |
| 50 | + } |
| 51 | + |
| 52 | + /// Creates a `FileType` from extension type. |
| 53 | + #[inline] |
| 54 | + pub(crate) const fn ext(ext: ImplFileTypeExt) -> Self { |
| 55 | + Self(Inner::Ext(ext)) |
| 56 | + } |
| 57 | + |
| 58 | + /// Tests whether this file type represents a directory. |
| 59 | + /// |
| 60 | + /// This corresponds to [`std::fs::FileType::is_dir`]. |
| 61 | + #[inline] |
| 62 | + pub fn is_dir(&self) -> bool { |
| 63 | + self.0 == Inner::Dir |
| 64 | + } |
| 65 | + |
| 66 | + /// Tests whether this file type represents a regular file. |
| 67 | + /// |
| 68 | + /// This corresponds to [`std::fs::FileType::is_file`]. |
| 69 | + #[inline] |
| 70 | + pub fn is_file(&self) -> bool { |
| 71 | + self.0 == Inner::File |
| 72 | + } |
| 73 | + |
| 74 | + /// Tests whether this file type represents a symbolic link. |
| 75 | + /// |
| 76 | + /// This corresponds to [`std::fs::FileType::is_symlink`]. |
| 77 | + #[inline] |
| 78 | + pub fn is_symlink(&self) -> bool { |
| 79 | + if let Inner::Ext(ext) = self.0 { |
| 80 | + ext.is_symlink() |
| 81 | + } else { |
| 82 | + false |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Unix-specific extensions for [`FileType`]. |
| 88 | +/// |
| 89 | +/// This corresponds to [`std::os::unix::fs::FileTypeExt`]. |
| 90 | +#[cfg(any(unix, target_os = "vxworks"))] |
| 91 | +pub trait FileTypeExt { |
| 92 | + /// Returns `true` if this file type is a block device. |
| 93 | + fn is_block_device(&self) -> bool; |
| 94 | + /// Returns `true` if this file type is a character device. |
| 95 | + fn is_char_device(&self) -> bool; |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(any(unix, target_os = "vxworks"))] |
| 99 | +impl FileTypeExt for FileType { |
| 100 | + #[inline] |
| 101 | + fn is_block_device(&self) -> bool { |
| 102 | + self.0 == Inner::Ext(ImplFileTypeExt::block_device()) |
| 103 | + } |
| 104 | + |
| 105 | + #[inline] |
| 106 | + fn is_char_device(&self) -> bool { |
| 107 | + self.0 == Inner::Ext(ImplFileTypeExt::char_device()) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/// Extension trait to allow `is_block_device` etc. to be exposed by |
| 112 | +/// the `cap-fs-ext` crate. |
| 113 | +/// |
| 114 | +/// This is hidden from the main API since this functionality isn't present in |
| 115 | +/// `std`. Use `cap_fs_ext::FileTypeExt` instead of calling this directly. |
| 116 | +#[cfg(windows)] |
| 117 | +#[doc(hidden)] |
| 118 | +pub trait _WindowsFileTypeExt { |
| 119 | + fn is_block_device(&self) -> bool; |
| 120 | + fn is_char_device(&self) -> bool; |
| 121 | + fn is_fifo(&self) -> bool; |
| 122 | + fn is_socket(&self) -> bool; |
| 123 | +} |
0 commit comments