dir_view/
dir_entry_utf8.rs1use crate::{DirViewUtf8, ViewKind};
2use cap_std::fs_utf8::{DirEntry, File, FileType, Metadata, OpenOptions};
3#[cfg(not(windows))]
4use rustix::fs::DirEntryExt;
5use std::{fmt, io};
6
7pub struct DirEntryViewUtf8 {
22 pub(crate) entry: DirEntry,
23 pub(crate) view_kind: ViewKind,
24}
25
26impl DirEntryViewUtf8 {
27 #[inline]
29 pub fn open(&self) -> io::Result<File> {
30 self.entry.open()
31 }
32
33 #[inline]
35 pub fn open_with(&self, options: &OpenOptions) -> io::Result<File> {
36 let mut options = options.clone();
38 match self.view_kind {
39 ViewKind::Full => {}
40 ViewKind::Readonly => {
41 options.append(false);
43 options.truncate(false);
44 options.write(false);
45 options.create(false);
46 options.create_new(false);
47 }
48 }
49 self.entry.open_with(&options)
50 }
51
52 #[inline]
54 pub fn open_dir(&self) -> io::Result<DirViewUtf8> {
55 Ok(DirViewUtf8 {
56 dir: self.entry.open_dir()?,
57 view_kind: self.view_kind,
58 })
59 }
60
61 #[inline]
63 pub fn remove_file(&self) -> io::Result<()> {
64 self.check_mutation()?;
65 self.entry.remove_file()
66 }
67
68 #[inline]
70 pub fn remove_dir(&self) -> io::Result<()> {
71 self.check_mutation()?;
72 self.entry.remove_dir()
73 }
74
75 #[inline]
79 pub fn metadata(&self) -> io::Result<Metadata> {
80 self.entry.metadata()
81 }
82
83 #[inline]
87 pub fn file_type(&self) -> io::Result<FileType> {
88 self.entry.file_type()
89 }
90
91 #[inline]
96 pub fn file_name(&self) -> io::Result<String> {
97 self.entry.file_name()
98 }
99
100 fn check_mutation(&self) -> io::Result<()> {
101 match self.view_kind {
102 ViewKind::Full => Ok(()),
103 ViewKind::Readonly => Err(Self::readonly()),
104 }
105 }
106
107 fn readonly() -> io::Error {
108 io::Error::new(
109 io::ErrorKind::PermissionDenied,
110 "attempt to modify a directory tree through a read-only `DirViewUtf8`",
111 )
112 }
113}
114
115#[cfg(not(windows))]
116impl DirEntryExt for DirEntryViewUtf8 {
117 #[inline]
118 fn ino(&self) -> u64 {
119 self.entry.ino()
120 }
121}
122
123#[cfg(windows)]
124#[doc(hidden)]
125impl cap_primitives::fs::_WindowsDirEntryExt for DirEntryViewUtf8 {
126 #[inline]
127 fn full_metadata(&self) -> io::Result<Metadata> {
128 cap_primitives::fs::_WindowsDirEntryExt::full_metadata(&self.entry)
129 }
130}
131
132impl fmt::Debug for DirEntryViewUtf8 {
133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 self.entry.fmt(f)
135 }
136}
137
138#[cfg(feature = "cap-fs-ext")]
139impl cap_fs_ext::DirEntryExt for DirEntryViewUtf8 {
140 fn full_metadata(&self) -> io::Result<Metadata> {
141 cap_fs_ext::DirEntryExt::full_metadata(&self.entry)
142 }
143}