tree_type/
fs.rs

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