diskit/metadata.rs
1//! Transparent replica of [`Metadata`](std::fs::Metadata)
2//!
3//! For more information see the [struct level documentation](Metadata).
4
5use std::fs;
6
7/// Type of a file on disk
8///
9/// There are currently there forms of files supported by this
10/// library: [`File`](Self::File), [`Dir`](Self::Dir) and
11/// [`Symlink`](Self::Symlink) (please notice that
12/// [`VirtualDiskit`](crate::VirtualDiskit) does not support
13/// symlinks).
14#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
15pub enum FileType
16{
17 /// A normal file
18 File,
19 /// A directory
20 Dir,
21 /// A symbolic link
22 Symlink,
23}
24
25/// Transparent replica of [`Metadata`](std::fs::Metadata)
26///
27/// This is a [replica](crate#making-your-own-diskit) of
28/// [`Metadata`](std::fs::Metadata). It stores just the [`FileType`]
29/// and the length of the file.
30// See lib.rs for justification.
31#[allow(missing_docs)]
32#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
33pub struct Metadata
34{
35 pub file_type: FileType,
36 pub len: u64,
37}
38
39impl FileType
40{
41 /// Returns whether the file is a directory
42 ///
43 /// This function returns whether the file is a directory.
44 #[must_use]
45 pub fn is_dir(&self) -> bool
46 {
47 *self == Self::Dir
48 }
49
50 /// Returns whether the file is a normal file
51 ///
52 /// This function returns whether the file is a normal file.
53 #[must_use]
54 pub fn is_file(&self) -> bool
55 {
56 *self == Self::File
57 }
58
59 /// Returns whether the file is a symlink
60 ///
61 /// This function returns whether the file is a symlink.
62 #[must_use]
63 pub fn is_symlink(&self) -> bool
64 {
65 *self == Self::Symlink
66 }
67}
68
69impl Metadata
70{
71 /// Returns the file type
72 ///
73 /// This function returns the file type.
74 #[must_use]
75 pub const fn file_type(&self) -> FileType
76 {
77 self.file_type
78 }
79
80 /// Returns whether the file is a directory
81 ///
82 /// This function returns whether the file is a directory.
83 #[must_use]
84 pub fn is_dir(&self) -> bool
85 {
86 self.file_type == FileType::Dir
87 }
88
89 /// Returns whether the file is a normal file
90 ///
91 /// This function returns whether the file is a normal file.
92 #[must_use]
93 pub fn is_file(&self) -> bool
94 {
95 self.file_type == FileType::File
96 }
97
98 /// Returns whether the file is a symlink
99 ///
100 /// This function returns whether the file is a symlink.
101 #[must_use]
102 pub fn is_symlink(&self) -> bool
103 {
104 self.file_type == FileType::Symlink
105 }
106
107 /// Returns the length of the file
108 ///
109 /// This function returns the length of the file (in bytes). The
110 /// length of a directory is always `0`, not `4096` or the number of
111 /// entries.
112 #[must_use]
113 pub const fn len(&self) -> u64
114 {
115 self.len
116 }
117
118 /// Returns whether the file is empty
119 ///
120 /// This function returns whether the length of this file as
121 /// returned by [`Metadata::len`] is zero or not.
122 #[must_use]
123 pub const fn is_empty(&self) -> bool
124 {
125 self.len == 0
126 }
127}
128
129impl From<fs::Metadata> for Metadata
130{
131 fn from(metadata: fs::Metadata) -> Self
132 {
133 Self {
134 file_type: if metadata.is_dir()
135 {
136 FileType::Dir
137 }
138 else if metadata.is_file()
139 {
140 FileType::File
141 }
142 else
143 {
144 FileType::Symlink
145 },
146 len: metadata.len(),
147 }
148 }
149}