Debian Codegraph @

src/path/mod.rs

raw
1 //! # path
2 //!
3 //! Path utility functions and traits.
4 //!
5
6 pub mod as_path;
7 pub mod from_path;
8
9 #[cfg(feature = "temp-path")]
10 mod temp_path;
11
12 #[cfg(test)]
13 #[path = "./mod_test.rs"]
14 mod mod_test;
15
16 use crate::error::FsIOError;
17 use crate::types::FsIOResult;
18 use as_path::AsPath;
19 use from_path::FromPath;
20 use std::fs;
21 use std::time::SystemTime;
22
23 /// Returns a canonicalized string from the provided path value.
24 ///
25 /// # Arguments
26 ///
27 /// * `path` - The path value
28 ///
29 /// # Example
30 ///
31 /// ```
32 /// use fsio::path;
33 /// use fsio::path::as_path::AsPath;
34 /// use std::path::Path;
35 ///
36 /// fn main() {
37 /// let path_obj = Path::new("./src/path/mod.rs");
38 ///
39 /// let path1 = path::canonicalize_as_string(&path_obj);
40 /// let path2 = path::canonicalize_as_string(&"./src/path/mod.rs".to_string());
41 ///
42 /// assert_eq!(path1.unwrap(), path2.unwrap());
43 /// }
44 /// ```
45 pub fn canonicalize_as_string<T: AsPath + ?Sized>(path: &T) -> FsIOResult<String> {
46 let path_obj = path.as_path();
47
48 match path_obj.canonicalize() {
49 Ok(path_buf) => {
50 let path_string: String = FromPath::from_path(&path_buf);
51
52 #[cfg(not(windows))]
53 {
54 Ok(path_string)
55 }
56 #[cfg(windows)]
57 {
58 let win_path_string = match dunce::canonicalize(&path_string) {
59 Ok(value) => FromPath::from_path(&value),
60 Err(_) => path_string,
61 };
62
63 Ok(win_path_string)
64 }
65 }
66 Err(error) => Err(FsIOError::IOError(
67 "Unable to canonicalize path.".to_string(),
68 Some(error),
69 )),
70 }
71 }
72
73 /// Returns a canonicalized string from the provided path value.
74 ///
75 /// # Arguments
76 ///
77 /// * `path` - The path value
78 ///
79 /// # Example
80 ///
81 /// ```
82 /// use fsio::path;
83 /// use fsio::path::as_path::AsPath;
84 /// use std::path::Path;
85 ///
86 /// fn main() {
87 /// let path_obj = Path::new("./src/path/mod.rs");
88 ///
89 /// let path1 = path::canonicalize_as_string(&path_obj);
90 /// let path2 = path::canonicalize_or("./src/path/mod.rs", "/src/path/mod.rs");
91 ///
92 /// assert_eq!(path1.unwrap(), path2);
93 /// }
94 /// ```
95 pub fn canonicalize_or<T: AsPath + ?Sized>(path: &T, or_value: &str) -> String {
96 match canonicalize_as_string(path) {
97 Ok(value) => value,
98 Err(_) => or_value.to_string(),
99 }
100 }
101
102 /// Returns the last path component (file name or last directory name).
103 ///
104 /// # Arguments
105 ///
106 /// * `path` - The path value
107 ///
108 /// # Example
109 ///
110 /// ```
111 /// use fsio::path;
112 /// use fsio::path::as_path::AsPath;
113 /// use std::path::Path;
114 ///
115 /// fn main() {
116 /// let basename = path::get_basename("./src/path/mod.rs");
117 ///
118 /// assert_eq!(basename.unwrap(), "mod.rs");
119 /// }
120 /// ```
121 pub fn get_basename<T: AsPath + ?Sized>(path: &T) -> Option<String> {
122 let path_obj = path.as_path();
123
124 match path_obj.file_name() {
125 Some(name) => Some(name.to_string_lossy().into_owned()),
126 None => None,
127 }
128 }
129
130 /// Returns the parent path.
131 ///
132 /// # Arguments
133 ///
134 /// * `path` - The path value
135 ///
136 /// # Example
137 ///
138 /// ```
139 /// use fsio::path;
140 /// use fsio::path::as_path::AsPath;
141 /// use std::path::Path;
142 ///
143 /// fn main() {
144 /// let dirname = path::get_parent_directory("./src/path/mod.rs");
145 ///
146 /// assert_eq!(dirname.unwrap(), "./src/path");
147 /// }
148 /// ```
149 pub fn get_parent_directory<T: AsPath + ?Sized>(path: &T) -> Option<String> {
150 let path_obj = path.as_path();
151
152 let directory = path_obj.parent();
153 match directory {
154 Some(directory_path) => {
155 let directory_path_string: String = FromPath::from_path(directory_path);
156
157 if directory_path_string.is_empty() {
158 None
159 } else {
160 Some(directory_path_string)
161 }
162 }
163 None => None,
164 }
165 }
166
167 /// Returns the last modified time of the provided path in millies since
168 /// unix epoch time.
169 ///
170 /// # Arguments
171 ///
172 /// * `path` - The path value
173 ///
174 /// # Example
175 ///
176 /// ```
177 /// use fsio::path;
178 ///
179 /// fn main() {
180 /// let time = path::get_last_modified_time("./src/path/mod.rs").unwrap();
181 ///
182 /// assert!(time > 0);
183 /// }
184 /// ```
185 pub fn get_last_modified_time(path: &str) -> FsIOResult<u128> {
186 match fs::metadata(path) {
187 Ok(metadata) => match metadata.modified() {
188 Ok(time) => match time.duration_since(SystemTime::UNIX_EPOCH) {
189 Ok(duration) => Ok(duration.as_millis()),
190 Err(error) => Err(FsIOError::SystemTimeError(
191 "Unable to get last modified duration for path.".to_string(),
192 Some(error),
193 )),
194 },
195 Err(error) => Err(FsIOError::IOError(
196 "Unable to extract modified time for path.".to_string(),
197 Some(error),
198 )),
199 },
200 Err(error) => Err(FsIOError::IOError(
201 "Unable to extract metadata for path.".to_string(),
202 Some(error),
203 )),
204 }
205 }
206
207 /// Returns a temporary file path.
208 /// The file does not exist after this function exists and can be used to create
209 /// a file in the OS temporary directory.
210 ///
211 /// # Arguments
212 ///
213 /// * `extension` - The file extension
214 ///
215 /// # Feature
216 ///
217 /// This function requires that the **temp-path** feature will be used.
218 ///
219 /// # Example
220 ///
221 /// ```
222 /// use fsio::path;
223 ///
224 /// fn main() {
225 /// let temp_file = path::get_temporary_file_path("txt");
226 ///
227 /// assert!(temp_file.ends_with(".txt"));
228 /// }
229 /// ```
230 #[cfg(feature = "temp-path")]
231 pub fn get_temporary_file_path(extension: &str) -> String {
232 temp_path::get(extension)
233 }