pathkit 1.1.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Core path operations module
//!
//! This module provides the main `Path` struct which wraps `std::path::PathBuf`
//! and provides extended functionality similar to Python's pathlib.

use std::{
    ffi::OsStr,
    fs::canonicalize,
    path::{
        Path as StdPath,
        PathBuf,
    },
};

use anyhow::Result;
use path_absolutize::Absolutize;
use serde::{
    Deserialize,
    Serialize,
};

/// A wrapper around `std::path::PathBuf` that provides extended path operations.
///
/// `Path` is similar to Python's `pathlib.Path`, providing an object-oriented
/// interface for path manipulation. It wraps `std::path::PathBuf` and implements
/// various traits for seamless interoperability with the standard library.
///
/// # Features
///
/// - **Serde Support**: Can be serialized and deserialized
/// - **Trait Implementations**: Implements `AsRef`, `Borrow`, `Deref`, `Display`, `From`
/// - **Path Joining**: Supports the `/` operator via the `Div` trait
///
/// # Example
///
/// ```rust
/// use pathkit::Path;
///
/// // Create a path
/// let path = Path::new("/home/user/project");
///
/// // Join paths
/// let config = path.join("config.json");
///
/// // Use / operator (note: this consumes the path)
/// let nested = Path::new("/home/user") / "project" / "subdir";
///
/// // Get path components
/// let parent = path.parent();
/// let file_name = path.file_name();
/// let extension = path.extension();
/// ```
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(transparent)]
pub struct Path(pub(crate) PathBuf);

impl Path {
    /// Creates a new `Path` from a given path.
    ///
    /// This method accepts any type that can be converted to `PathBuf`,
    /// including `&str`, `String`, `PathBuf`, and `&Path`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use pathkit::Path;
    ///
    /// // From &str
    /// let path = Path::new("/test/path");
    ///
    /// // From String
    /// let path = Path::new(String::from("/test/path"));
    ///
    /// // From PathBuf
    /// use std::path::PathBuf;
    /// let path = Path::new(PathBuf::from("/test/path"));
    /// ```
    pub fn new<P: Into<PathBuf>>(path: P) -> Self {
        Self(path.into())
    }

    /// Converts the path to an absolute path.
    ///
    /// This uses the `path-absolutize` crate which handles edge cases
    /// like converting relative paths to absolute paths.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use pathkit::Path;
    ///
    /// let path = Path::new("relative/path");
    /// let absolute = path.absolutize()?;
    /// assert!(absolute.is_absolute());
    /// ```
    pub fn absolutize(&self) -> Result<Self> {
        Ok(Self::new(self.0.absolutize()?))
    }

    /// Converts the path to an absolute path, using the given directory as base.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use pathkit::Path;
    ///
    /// let path = Path::new("relative/path");
    /// let absolute = path.absolutize_from("/custom/cwd")?;
    /// ```
    pub fn absolutize_from(&self, cwd: impl AsRef<StdPath>) -> Result<Self> {
        Ok(Self::new(self.0.absolutize_from(cwd)?))
    }

    /// Converts a relative path to an absolute path with a virtual root.
    ///
    /// This is useful for testing or sandboxed environments where you want
    /// to treat a directory as the root.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use pathkit::Path;
    ///
    /// let path = Path::new("subdir/file.txt");
    /// let absolute = path.absolutize_virtually("/virtual/root")?;
    /// assert_eq!(absolute.to_str(), Some("/virtual/root/subdir/file.txt"));
    /// ```
    pub fn absolutize_virtually(&self, virtual_root: impl AsRef<StdPath>) -> Result<Self> {
        Ok(Self::new(self.0.absolutize_virtually(virtual_root)?))
    }

    /// Returns a reference to the underlying `std::path::Path`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use pathkit::Path;
    ///
    /// let path = Path::new("/test/path");
    /// let std_path = path.as_path();
    /// assert_eq!(std_path, std::path::Path::new("/test/path"));
    /// ```
    pub fn as_path(&self) -> &StdPath {
        &self.0
    }

    /// Returns the canonical form of the path.
    ///
    /// This resolves symlinks and normalizes the path. Unlike `absolutize`,
    /// this requires the path to exist.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use pathkit::Path;
    ///
    /// let path = Path::new(".");
    /// let canonical = path.canonicalize()?;
    /// assert!(canonical.is_absolute());
    /// ```
    pub fn canonicalize(&self) -> Result<Self, std::io::Error> {
        canonicalize(&self.0).map(Self::new)
    }

    /// Joins this path with another path.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::path::MAIN_SEPARATOR;
    ///
    /// use pathkit::Path;
    ///
    /// let path = Path::new(&format!("{0}base", MAIN_SEPARATOR));
    /// let joined = path.join(&format!("subdir{0}file.txt", MAIN_SEPARATOR));
    /// assert_eq!(joined.to_str(), Some(format!("{0}base{0}subdir{0}file.txt", MAIN_SEPARATOR).as_str()));
    /// ```
    pub fn join(&self, path: impl AsRef<StdPath>) -> Self {
        Self::new(self.0.join(path))
    }

    /// Returns the parent directory of this path.
    ///
    /// # Example
    ///
    /// ```rust
    /// use pathkit::Path;
    ///
    /// let path = Path::new("/base/subdir/file.txt");
    /// assert_eq!(path.parent().unwrap().to_str(), Some("/base/subdir"));
    ///
    /// // Root path has no parent
    /// let root = Path::new("/");
    /// assert!(root.parent().is_none());
    /// ```
    pub fn parent(&self) -> Option<Self> {
        self.0.parent().map(Self::new)
    }

    /// Converts this path to a `PathBuf`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use pathkit::Path;
    /// use std::path::PathBuf;
    ///
    /// let path = Path::new("/test/path");
    /// let buf: PathBuf = path.to_path_buf();
    /// assert_eq!(buf, PathBuf::from("/test/path"));
    /// ```
    pub fn to_path_buf(&self) -> PathBuf {
        self.0.clone()
    }

    /// Returns a new path with a different file extension.
    ///
    /// # Example
    ///
    /// ```rust
    /// use pathkit::Path;
    ///
    /// // Replace extension
    /// let path = Path::new("/path/to/file.txt");
    /// assert_eq!(path.with_extension("md").to_str(), Some("/path/to/file.md"));
    ///
    /// // Add extension to file without one
    /// let path = Path::new("/path/to/file");
    /// assert_eq!(path.with_extension("txt").to_str(), Some("/path/to/file.txt"));
    /// ```
    pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> Self {
        Self::new(self.0.with_extension(extension))
    }
}

#[cfg(test)]
mod tests {
    use std::{
        ffi::OsStr,
        path::MAIN_SEPARATOR,
    };

    use super::*;

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

        // Test with String
        let path = Path::new(String::from("/test/path"));
        assert_eq!(path.to_str(), Some("/test/path"));

        // Test with PathBuf
        let path = Path::new(PathBuf::from("/test/path"));
        assert_eq!(path.to_str(), Some("/test/path"));
    }

    #[test]
    fn test_as_path() {
        let path = Path::new("/test/path");
        let std_path: &std::path::Path = path.as_path();
        assert_eq!(std_path, std::path::Path::new("/test/path"));
    }

    #[test]
    fn test_to_path_buf() {
        let path = Path::new("/test/path");
        let path_buf = path.to_path_buf();
        assert_eq!(path_buf, PathBuf::from("/test/path"));
    }

    #[test]
    fn test_join() {
        let path = Path::new(format!("{0}base", MAIN_SEPARATOR));
        assert_eq!(
            path.join("subdir").to_str(),
            Some(format!("{0}base{0}subdir", MAIN_SEPARATOR).as_str())
        );

        // On Windows, join doesn't treat "/" as separator, so we use sep for proper path construction
        assert_eq!(
            path.join(format!("subdir{0}file.txt", MAIN_SEPARATOR)).to_str(),
            Some(format!("{0}base{0}subdir{0}file.txt", MAIN_SEPARATOR).as_str())
        );

        // Join with Path
        let subpath = Path::new("subpath");
        assert_eq!(
            path.join(subpath).to_str(),
            Some(format!("{0}base{0}subpath", MAIN_SEPARATOR).as_str())
        );
    }

    #[test]
    fn test_parent() {
        let path = Path::new("/base/subdir/file.txt");
        assert_eq!(path.parent().unwrap().to_str(), Some("/base/subdir"));

        // Test root path
        let path = Path::new("/");
        assert!(path.parent().is_none());

        // Test relative path with no parent
        let path = Path::new("file.txt");
        assert!(path.parent().is_some());
    }

    #[test]
    fn test_with_extension() {
        let path = Path::new("/path/to/file.txt");
        assert_eq!(path.with_extension("md").to_str(), Some("/path/to/file.md"));

        // Test adding extension to file without extension
        let path = Path::new("/path/to/file");
        assert_eq!(path.with_extension("txt").to_str(), Some("/path/to/file.txt"));

        // Test replacing extension
        let path = Path::new("/path/to/file.txt");
        assert_eq!(path.with_extension("json").to_str(), Some("/path/to/file.json"));
    }

    #[test]
    fn test_is_absolute() {
        // On Unix, /absolute/path is absolute; on Windows, only C:\path or \\server\share are absolute
        #[cfg(not(windows))]
        {
            let path = Path::new("/absolute/path");
            assert!(path.is_absolute());
        }

        let relative_path = Path::new("relative/path");
        assert!(!relative_path.is_absolute());

        #[cfg(windows)]
        {
            // Windows-style absolute paths
            let path = Path::new("C:\\absolute\\path");
            assert!(path.is_absolute());
        }
    }

    #[test]
    fn test_is_relative() {
        // On Unix, /absolute/path is absolute; on Windows, /path is treated as relative
        // since it doesn't have a drive letter
        #[cfg(not(windows))]
        {
            let path = Path::new("/absolute/path");
            assert!(!path.is_relative());
        }

        let relative_path = Path::new("relative/path");
        assert!(relative_path.is_relative());

        #[cfg(windows)]
        {
            // Windows-style absolute paths are not relative
            let path = Path::new("C:\\absolute\\path");
            assert!(!path.is_relative());
        }
    }

    #[test]
    fn test_file_name() {
        let path = Path::new("/path/to/file.txt");
        assert_eq!(path.file_name(), Some(OsStr::new("file.txt")));

        // Note: std::path::Path ignores trailing slash and returns the last component
        let path = Path::new("/path/to/");
        assert_eq!(path.file_name(), Some(OsStr::new("to")));

        let path = Path::new("/");
        assert_eq!(path.file_name(), None);
    }

    #[test]
    fn test_file_stem() {
        let path = Path::new("file.txt");
        assert_eq!(path.file_stem(), Some(OsStr::new("file")));

        let path = Path::new(".hidden");
        assert_eq!(path.file_stem(), Some(OsStr::new(".hidden")));

        let path = Path::new("file.tar.gz");
        assert_eq!(path.file_stem(), Some(OsStr::new("file.tar")));
    }

    #[test]
    fn test_extension() {
        let path = Path::new("file.txt");
        assert_eq!(path.extension(), Some(OsStr::new("txt")));

        let path = Path::new("file.tar.gz");
        assert_eq!(path.extension(), Some(OsStr::new("gz")));

        let path = Path::new("file");
        assert_eq!(path.extension(), None);

        let path = Path::new("/path/to.");
        assert_eq!(path.extension(), Some(OsStr::new("")));
    }

    #[test]
    fn test_starts_with() {
        let path = Path::new("/path/to/file");
        // starts_with is part of std::path::Path, available through Deref
        assert!(path.starts_with("/path"));
        assert!(path.starts_with(std::path::Path::new("/path")));
        assert!(!path.starts_with("/other"));
    }

    #[test]
    fn test_ends_with() {
        let path = Path::new("/path/to/file.txt");
        assert!(path.ends_with("file.txt"));
        assert!(path.ends_with(std::path::Path::new("to/file.txt")));
        assert!(!path.ends_with("other.txt"));
    }

    #[test]
    fn test_components() {
        let path = Path::new("/path/to/file.txt");
        let components: Vec<_> = path.components().collect();
        assert!(components.len() >= 3);
    }

    #[test]
    fn test_iter() {
        let path = Path::new("/path/to/file.txt");
        let iter = path.iter();
        let components: Vec<_> = iter.collect();
        assert!(!components.is_empty());
    }

    // Skip contains test - Path doesn't have this method
    // Skip set_file_name test - Path doesn't have this method
    // Skip set_extension test - Path doesn't have this method
    // Skip pop test - Path doesn't have this method
    // Skip push test - Path doesn't have this method

    #[test]
    fn test_absolute_path_functionality() -> Result<()> {
        // Test absolutize
        let path = Path::new(".");
        let absolute = path.absolutize()?;
        assert!(absolute.is_absolute());

        Ok(())
    }

    #[test]
    fn test_absolutize_from() -> Result<()> {
        let path = Path::new("subdir");
        let absolute = path.absolutize_from(format!("{0}base", MAIN_SEPARATOR))?;
        // Check that the result ends with the joined path (handles platform-specific separators)
        assert!(absolute
            .to_str()
            .unwrap()
            .ends_with(&format!("{}subdir", MAIN_SEPARATOR)));

        Ok(())
    }

    #[test]
    fn test_absolutize_virtually() -> Result<()> {
        let path = Path::new("subdir/file.txt");
        let absolute = path.absolutize_virtually("/virtual")?;
        // Check that the result contains the virtual root and subdir (handles platform-specific separators)
        let abs_str = absolute.to_str().unwrap();
        assert!(abs_str.contains("virtual"));
        assert!(abs_str.contains("subdir"));
        assert!(abs_str.contains("file.txt"));

        Ok(())
    }

    #[test]
    fn test_canonicalize() -> Result<()> {
        let path = Path::new(".");
        let canonical = path.canonicalize()?;
        assert!(canonical.is_absolute());

        Ok(())
    }

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

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

    #[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]
    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 mut hasher1 = DefaultHasher::new();
        let mut hasher2 = DefaultHasher::new();

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

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

    #[test]
    fn test_ord() {
        use std::cmp::Ordering;

        let path1 = Path::new("/a/b");
        let path2 = Path::new("/a/c");

        assert_eq!(path1.cmp(&path2), Ordering::Less);
        assert_eq!(path2.cmp(&path1), Ordering::Greater);
        assert_eq!(path1.cmp(&path1), Ordering::Equal);
    }

    #[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]
    fn test_from_std_path() {
        let std_path = std::path::Path::new("/test/path");
        let path = Path::from(std_path);
        assert_eq!(path.to_str(), Some("/test/path"));
    }
}