pathkit 1.2.0

Similar to the Path structure provided by python's pathlib, it provides various async/sync versions of file manipulation methods in addition to some of the std::Path built-in methods.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use std::{
    borrow::Borrow,
    ffi::OsStr,
    fmt::{
        Display,
        Formatter,
        Result as FmtResult,
    },
    ops::Deref,
    path::{
        Path as StdPath,
        PathBuf,
    },
};

use super::core::Path;

impl AsRef<StdPath> for Path {
    fn as_ref(&self) -> &StdPath {
        &self.0
    }
}

impl Borrow<StdPath> for Path {
    fn borrow(&self) -> &StdPath {
        &self.0
    }
}

impl Deref for Path {
    type Target = StdPath;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Display for Path {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}", self.to_string_lossy())
    }
}

impl From<&StdPath> for Path {
    fn from(path: &StdPath) -> Self {
        Self(path.to_path_buf())
    }
}

impl From<&str> for Path {
    fn from(path: &str) -> Self {
        Self(PathBuf::from(path))
    }
}

impl AsRef<OsStr> for Path {
    fn as_ref(&self) -> &OsStr {
        self.0.as_os_str()
    }
}

/// Converts a `String` into a `Path`.
///
/// This allows `String` to be used wherever a `Path` is expected,
/// such as in the `Path::new()` constructor or path joining operations.
///
/// # Example
///
/// ```rust
/// use pathkit::Path;
///
/// let path = Path::new("test/path");
/// let from_string: Path = Path::from(String::from("test/path"));
/// assert_eq!(from_string.to_str(), Some("test/path"));
/// ```
impl From<String> for Path {
    fn from(path: String) -> Self {
        Self(PathBuf::from(path))
    }
}

/// Converts a `Path` into a `String`.
///
/// This conversion lossy — it returns the path's UTF-8 representation
/// as a `String`. If the path contains invalid Unicode, non-decodable
/// bytes are replaced with the Unicode replacement character (U+FFFD).
///
/// # Example
///
/// ```rust
/// use pathkit::Path;
///
/// let path = Path::new("/test/path");
/// let s: String = String::from(path);
/// assert_eq!(s, "/test/path");
/// ```
impl From<Path> for String {
    fn from(path: Path) -> Self {
        path.to_string_lossy().into_owned()
    }
}

/// Allows a `Path` to be used as a `&str` via `AsRef<str>`.
///
/// This is useful for APIs that expect `impl AsRef<str>` rather than
/// `impl AsRef<Path>`.
///
/// # Example
///
/// ```rust
/// use pathkit::Path;
///
/// let path = Path::new("/test/path");
/// let s: &str = path.as_ref();
/// assert_eq!(s, "/test/path");
/// ```
impl AsRef<str> for Path {
    fn as_ref(&self) -> &str {
        // to_string_lossy() always returns Some because we don't check for lossy conversion
        // The unwrap is safe: to_string_lossy() never panics, only returns Owned
        self.to_str().unwrap()
    }
}

/// Allows a `Path` to be used as a `PathBuf` via `AsRef<PathBuf>`.
///
/// This is useful for APIs that accept `impl AsRef<PathBuf>`, such as
/// `copy_file_sync`, `hard_link_sync`, and `soft_link_sync`.
///
/// # Example
///
/// ```rust
/// use pathkit::Path;
/// use std::path::PathBuf;
///
/// let path = Path::new("/test/path");
/// let buf: &PathBuf = path.as_ref();
/// assert_eq!(*buf, PathBuf::from("/test/path"));
/// ```
impl AsRef<PathBuf> for Path {
    fn as_ref(&self) -> &PathBuf {
        &self.0
    }
}

impl From<Path> for PathBuf {
    fn from(path: Path) -> Self {
        path.0
    }
}

impl AsRef<Path> for Path {
    fn as_ref(&self) -> &Path {
        self
    }
}

#[cfg(test)]
mod tests {
    use std::{
        borrow::Borrow,
        ffi::OsStr,
        ops::Deref,
        path::{
            Path as StdPath,
            PathBuf,
        },
    };

    use super::Path;

    // Test AsRef<StdPath>
    #[test]
    fn test_as_ref_std_path() {
        let path = Path::new("/test/path");
        let std_path: &StdPath = path.as_ref();
        assert_eq!(std_path, StdPath::new("/test/path"));
    }

    // Test AsRef<Path> - check that as_ref returns the path through deref
    #[test]
    fn test_as_ref_self() {
        let path = Path::new("/test/path");
        // as_ref returns &StdPath through Deref
        let path_ref: &StdPath = path.as_ref();
        assert_eq!(path_ref, StdPath::new("/test/path"));
    }

    // Test Borrow<StdPath>
    #[test]
    fn test_borrow() {
        let path = Path::new("/test/path");
        let borrowed: &StdPath = path.borrow();
        assert_eq!(borrowed, StdPath::new("/test/path"));
    }

    // Test Deref
    #[test]
    fn test_deref() {
        let path = Path::new("/test/path");
        let dereferenced: &StdPath = path.deref();
        assert_eq!(dereferenced, StdPath::new("/test/path"));
    }

    // Skip Deref target test - Path::target doesn't exist
    // Test Display
    #[test]
    fn test_display() {
        let path = Path::new("/test/path");
        let display: String = path.to_string();
        assert_eq!(display, "/test/path");
    }

    // Test Display format
    #[test]
    fn test_display_format() {
        let path = Path::new("/test/path");
        assert_eq!(format!("{}", path), "/test/path");
    }

    // Test Display with other format specifiers
    #[test]
    fn test_display_format_args() {
        let path = Path::new("file.txt");
        assert_eq!(format!("Path: {}", path), "Path: file.txt");
    }

    // Test From<&StdPath> for Path
    #[test]
    fn test_from_std_path_ref() {
        let std_path = std::path::Path::new("/test/path");
        let path: Path = Path::from(std_path);
        assert_eq!(path.to_str(), Some("/test/path"));
    }

    // Test From<PathBuf> for Path - use Path::new instead
    #[test]
    fn test_from_pathbuf() {
        let pathbuf = PathBuf::from("/test/path");
        let path = Path::new(pathbuf);
        assert_eq!(path.to_path_buf(), PathBuf::from("/test/path"));
    }

    // Test From<Path> for PathBuf
    #[test]
    fn test_from_path_to_pathbuf() {
        let path = Path::new("/test/path");
        let pathbuf: PathBuf = PathBuf::from(path);
        assert_eq!(pathbuf, PathBuf::from("/test/path"));
    }

    // Skip test_from_string - Path doesn't implement From<String>
    // Skip test_from_str - Path doesn't implement From<&str>

    // Test to_string_lossy
    #[test]
    fn test_to_string_lossy() {
        let path = Path::new("/test/path");
        let lost = path.to_string_lossy();
        assert_eq!(lost, "/test/path");
    }

    // Test to_str
    #[test]
    fn test_to_str() {
        let path = Path::new("/test/path");
        assert_eq!(path.to_str(), Some("/test/path"));
    }

    // Test to_str with invalid unicode - simplified
    #[test]
    fn test_to_str_unicode() {
        // Test normal unicode path
        let path = Path::new("/test/文件.txt");
        assert_eq!(path.to_str(), Some("/test/文件.txt"));
    }

    // Test clone
    #[test]
    fn test_clone() {
        let path = Path::new("/test/path");
        let cloned = path.clone();
        assert_eq!(path, cloned);
    }

    // Test clone is independent
    #[test]
    fn test_clone_independence() {
        let mut path1 = Path::new("/test/path");
        let path2 = path1.clone();
        path1 = Path::new("/other/path");
        assert_eq!(path2.to_str(), Some("/test/path"));
        assert_eq!(path1.to_str(), Some("/other/path"));
    }

    // Test equality
    #[test]
    fn test_eq() {
        let path1 = Path::new("/test/path");
        let path2 = Path::new("/test/path");
        let path3 = Path::new("/other/path");

        assert_eq!(path1, path2);
        assert_ne!(path1, path3);
    }

    // Test equality with different types
    #[test]
    fn test_eq_with_std_path() {
        let path = Path::new("/test/path");
        let std_path = StdPath::new("/test/path");
        // Can't directly compare different types without explicit conversion
        assert_eq!(path.as_path(), std_path);
    }

    // Test Debug
    #[test]
    fn test_debug() {
        let path = Path::new("/test/path");
        let debug_str = format!("{:?}", path);
        assert!(debug_str.contains("Path"));
        assert!(debug_str.contains("/test/path"));
    }

    // Test Hash
    #[test]
    fn test_hash() {
        use std::{
            collections::hash_map::DefaultHasher,
            hash::{
                Hash,
                Hasher,
            },
        };

        let path1 = Path::new("/test/path");
        let path2 = Path::new("/test/path");
        let path3 = Path::new("/other/path");

        let mut hasher1 = DefaultHasher::new();
        let mut hasher2 = DefaultHasher::new();
        let mut hasher3 = DefaultHasher::new();

        path1.hash(&mut hasher1);
        path2.hash(&mut hasher2);
        path3.hash(&mut hasher3);

        assert_eq!(hasher1.finish(), hasher2.finish());
        assert_ne!(hasher1.finish(), hasher3.finish());
    }

    // Test PartialEq with PathBuf
    #[test]
    fn test_partial_eq_pathbuf() {
        let path = Path::new("/test/path");
        let pathbuf = PathBuf::from("/test/path");

        assert_eq!(path.as_path(), pathbuf.as_path());
    }

    // Test as_os_str
    #[test]
    fn test_as_os_str() {
        let path = Path::new("/test/path");
        let os_str = path.as_os_str();
        assert_eq!(os_str, std::ffi::OsStr::new("/test/path"));
    }

    // Test AsRef<OsStr>
    #[test]
    fn test_as_ref_os_str() {
        let path = Path::new("/test/path");
        let os_str: &OsStr = path.as_ref();
        assert_eq!(os_str, OsStr::new("/test/path"));
    }

    // Test From<&str> for Path
    #[test]
    fn test_from_str() {
        let path: Path = Path::from("/test/path");
        assert_eq!(path.to_str(), Some("/test/path"));
    }

    // Test From<String> for Path
    #[test]
    fn test_from_string() {
        let s = String::from("/test/path");
        let path: Path = Path::from(s);
        assert_eq!(path.to_str(), Some("/test/path"));
    }

    // Test From<Path> for String
    #[test]
    fn test_to_string() {
        let path = Path::new("/test/path");
        let s: String = String::from(path);
        assert_eq!(s, "/test/path");
    }

    // Test AsRef<str>
    #[test]
    fn test_as_ref_str() {
        let path = Path::new("/test/path");
        let s: &str = path.as_ref();
        assert_eq!(s, "/test/path");
    }

    // Test that path has content (skip is_empty - it's unstable)
    #[test]
    fn test_path_has_content() {
        let path = Path::new("/test/path");
        // Just verify the path string is not empty
        assert!(!path.to_str().unwrap().is_empty());
    }
}