cap_primitives/fs/
file_type.rs1use crate::fs::ImplFileTypeExt;
4
5#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
7enum Inner {
8 Dir,
10
11 File,
13
14 Unknown,
16
17 Ext(ImplFileTypeExt),
19}
20
21#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
30#[repr(transparent)]
31pub struct FileType(Inner);
32
33impl FileType {
34 #[inline]
36 pub const fn dir() -> Self {
37 Self(Inner::Dir)
38 }
39
40 #[inline]
42 pub const fn file() -> Self {
43 Self(Inner::File)
44 }
45
46 #[inline]
48 pub const fn unknown() -> Self {
49 Self(Inner::Unknown)
50 }
51
52 #[inline]
54 pub(crate) const fn ext(ext: ImplFileTypeExt) -> Self {
55 Self(Inner::Ext(ext))
56 }
57
58 #[inline]
62 pub fn is_dir(&self) -> bool {
63 self.0 == Inner::Dir
64 }
65
66 #[inline]
70 pub fn is_file(&self) -> bool {
71 self.0 == Inner::File
72 }
73
74 #[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#[cfg(any(unix, target_os = "vxworks"))]
91pub trait FileTypeExt {
92 fn is_block_device(&self) -> bool;
94 fn is_char_device(&self) -> bool;
96 fn is_fifo(&self) -> bool;
98 fn is_socket(&self) -> bool;
100}
101
102#[cfg(any(unix, target_os = "vxworks"))]
103impl FileTypeExt for FileType {
104 #[inline]
105 fn is_block_device(&self) -> bool {
106 self.0 == Inner::Ext(ImplFileTypeExt::block_device())
107 }
108
109 #[inline]
110 fn is_char_device(&self) -> bool {
111 self.0 == Inner::Ext(ImplFileTypeExt::char_device())
112 }
113
114 #[inline]
115 fn is_fifo(&self) -> bool {
116 self.0 == Inner::Ext(ImplFileTypeExt::fifo())
117 }
118
119 #[inline]
120 fn is_socket(&self) -> bool {
121 self.0 == Inner::Ext(ImplFileTypeExt::socket())
122 }
123}
124
125#[cfg(all(windows, windows_file_type_ext))]
129pub trait FileTypeExt {
130 fn is_symlink_dir(&self) -> bool;
133 fn is_symlink_file(&self) -> bool;
136}
137
138#[cfg(all(windows, windows_file_type_ext))]
139impl FileTypeExt for FileType {
140 #[inline]
141 fn is_symlink_dir(&self) -> bool {
142 self.0 == Inner::Ext(ImplFileTypeExt::symlink_dir())
143 }
144
145 #[inline]
146 fn is_symlink_file(&self) -> bool {
147 self.0 == Inner::Ext(ImplFileTypeExt::symlink_file())
148 }
149}
150
151#[cfg(windows)]
157#[doc(hidden)]
158pub trait _WindowsFileTypeExt {
159 fn is_block_device(&self) -> bool;
160 fn is_char_device(&self) -> bool;
161 fn is_fifo(&self) -> bool;
162 fn is_socket(&self) -> bool;
163}