tree_type/
fs.rs

1//! Filesystem operations with feature-gated error handling
2
3use std::path::Path;
4use std::path::PathBuf;
5
6// Re-export types for generated code
7#[cfg(feature = "enhanced-errors")]
8pub use crate::deps::enhanced_errors::ReadDir;
9
10#[cfg(not(feature = "enhanced-errors"))]
11pub use std::fs::ReadDir;
12
13// Metadata is the same for both `fs_err` and `std::fs`
14pub use std::fs::Metadata;
15
16/// Create an empty file if it doesn't already exist
17///
18/// # Errors
19///  
20/// May return any of the same errors as `std::fs::open`
21pub fn create_file<P: Into<PathBuf> + std::convert::AsRef<std::path::Path>>(
22    path: P,
23) -> std::io::Result<()> {
24    #[cfg(feature = "enhanced-errors")]
25    crate::deps::enhanced_errors::File::create(path)?;
26
27    #[cfg(not(feature = "enhanced-errors"))]
28    ::std::fs::File::create(path)?;
29
30    Ok(())
31}
32
33/// Renames a file or directory to a new name, replacing the original file if
34/// `to` already exists.
35///
36/// # Errors
37///  
38/// May return any of the same errors as `std::fs::rename`
39pub fn rename<From: AsRef<Path>, To: AsRef<Path>>(from: From, to: To) -> std::io::Result<()> {
40    #[cfg(feature = "enhanced-errors")]
41    crate::deps::enhanced_errors::rename(from, to)?;
42
43    #[cfg(not(feature = "enhanced-errors"))]
44    ::std::fs::rename(from, to)?;
45
46    Ok(())
47}
48
49/// Changes the permissions found on a file or a directory.
50///
51/// # Errors
52///  
53/// May return any of the same errors as `std::fs::set_permissions`
54pub fn set_permissions<P: AsRef<Path>>(
55    path: P,
56    perms: std::fs::Permissions,
57) -> std::io::Result<()> {
58    #[cfg(feature = "enhanced-errors")]
59    crate::deps::enhanced_errors::set_permissions(path, perms)?;
60
61    #[cfg(not(feature = "enhanced-errors"))]
62    std::fs::set_permissions(path, perms)?;
63
64    Ok(())
65}
66
67/// Creates a new, empty directory at the provided path
68///
69/// # Errors
70///  
71/// May return any of the same errors as `std::fs::create_dir`
72pub fn create_dir<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
73    #[cfg(feature = "enhanced-errors")]
74    {
75        crate::deps::enhanced_errors::create_dir(path)
76    }
77    #[cfg(not(feature = "enhanced-errors"))]
78    {
79        std::fs::create_dir(path)
80    }
81}
82
83/// Recursively create a directory and all of its parent components if they are missing.
84///
85/// # Errors
86///
87/// May return any of the same errors as `std::fs::create_dir_all`
88pub fn create_dir_all<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
89    #[cfg(feature = "enhanced-errors")]
90    {
91        crate::deps::enhanced_errors::create_dir_all(path)
92    }
93    #[cfg(not(feature = "enhanced-errors"))]
94    {
95        std::fs::create_dir_all(path)
96    }
97}
98
99/// Removes an empty directory.
100///
101/// # Errors
102///
103/// May return any of the same errors as `std::fs::remove_all`
104pub fn remove_dir<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
105    #[cfg(feature = "enhanced-errors")]
106    {
107        crate::deps::enhanced_errors::remove_dir(path)
108    }
109    #[cfg(not(feature = "enhanced-errors"))]
110    {
111        std::fs::remove_dir(path)
112    }
113}
114
115/// Removes a directory at this path, after removing all its contents. Use carefully!
116///
117/// # Errors
118///
119/// May return any of the same errors as `std::fs::remove_dir_all`
120pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
121    #[cfg(feature = "enhanced-errors")]
122    {
123        crate::deps::enhanced_errors::remove_dir_all(path)
124    }
125    #[cfg(not(feature = "enhanced-errors"))]
126    {
127        std::fs::remove_dir_all(path)
128    }
129}
130
131/// Returns an iterator over the entries within a directory.
132///
133/// # Errors
134///
135/// May return any of the same errors as `std::fs::read_dir`
136#[cfg(feature = "enhanced-errors")]
137pub fn read_dir<P: AsRef<Path>>(path: P) -> std::io::Result<crate::deps::enhanced_errors::ReadDir> {
138    crate::deps::enhanced_errors::read_dir(path.as_ref())
139}
140
141/// Returns an iterator over the entries within a directory.
142///
143/// # Errors
144///
145/// May return any of the same errors as `std::fs::read_dir`
146#[cfg(not(feature = "enhanced-errors"))]
147pub fn read_dir<P: AsRef<Path>>(path: P) -> std::io::Result<std::fs::ReadDir> {
148    std::fs::read_dir(path)
149}
150
151/// Given a path, queries the file system to get information about a file, directory, etc.
152///
153/// # Errors
154///
155/// May return any of the same errors as `std::fs::metadata`
156pub fn metadata<P: AsRef<Path>>(path: P) -> std::io::Result<std::fs::Metadata> {
157    #[cfg(feature = "enhanced-errors")]
158    {
159        crate::deps::enhanced_errors::metadata(path)
160    }
161    #[cfg(not(feature = "enhanced-errors"))]
162    {
163        std::fs::metadata(path)
164    }
165}
166
167/// Reads the entire contents of a file into a bytes vector.
168///
169/// # Errors
170///
171/// May return any of the same errors as `std::fs::read`
172pub fn read<P: AsRef<Path>>(path: P) -> std::io::Result<Vec<u8>> {
173    #[cfg(feature = "enhanced-errors")]
174    {
175        crate::deps::enhanced_errors::read(path)
176    }
177    #[cfg(not(feature = "enhanced-errors"))]
178    {
179        std::fs::read(path)
180    }
181}
182
183/// Reads the entire contents of a file into a string.
184///
185/// # Errors
186///
187/// May return any of the same errors as `std::fs::read_to_string`
188pub fn read_to_string<P: AsRef<Path>>(path: P) -> std::io::Result<String> {
189    #[cfg(feature = "enhanced-errors")]
190    {
191        crate::deps::enhanced_errors::read_to_string(path)
192    }
193    #[cfg(not(feature = "enhanced-errors"))]
194    {
195        std::fs::read_to_string(path)
196    }
197}
198
199/// Writes a slice as the entire contents of a file.
200///
201/// # Errors
202///
203/// May return any of the same errors as `std::fs::write`
204pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> std::io::Result<()> {
205    #[cfg(feature = "enhanced-errors")]
206    {
207        crate::deps::enhanced_errors::write(path, contents)
208    }
209    #[cfg(not(feature = "enhanced-errors"))]
210    {
211        std::fs::write(path, contents)
212    }
213}
214
215/// Removes a file from the filesystem.
216///
217/// # Errors
218///
219/// May return any of the same errors as `std::fs::remove_file`
220pub fn remove_file<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
221    #[cfg(feature = "enhanced-errors")]
222    {
223        crate::deps::enhanced_errors::remove_file(path)
224    }
225    #[cfg(not(feature = "enhanced-errors"))]
226    {
227        std::fs::remove_file(path)
228    }
229}