pathbuf_ext/
lib.rs

1// Extend the PathBuf type with a replace method
2
3use std::path::{Path, PathBuf};
4
5pub trait PathExt {
6    fn replace<T: AsRef<str>>(&self, old: T, new: T) -> PathBuf;
7    fn contains<T: AsRef<str>>(&self, path: T) -> bool;
8}
9
10impl PathExt for Path {
11    fn replace<T: AsRef<str>>(&self, old: T, new: T) -> PathBuf {
12        let mut path = self.to_str().unwrap().to_string();
13
14        let new2 = new.as_ref().to_string().replace("\\", "/");
15        let old2 = old.as_ref().to_string().replace("\\", "/");
16
17        let new3 = new.as_ref().to_string().replace("/", "\\");
18        let old3 = old.as_ref().to_string().replace("/", "\\");
19
20        if path.contains(&old2) {
21            path = path.replace(&old2, &new2);
22        } else if path.contains(&old3) {
23            path = path.replace(&old3, &new3);
24        }
25
26        PathBuf::from(path)
27    }
28
29    fn contains<T: AsRef<str>>(&self, s: T) -> bool {
30        let path = self.to_str().unwrap().to_string();
31        
32        let s2 = s.as_ref().to_string().replace("\\", "/");
33        let s3 = s.as_ref().to_string().replace("/", "\\");
34
35        path.contains(&s2) || path.contains(&s3) // || path.contains(s)
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_replace() {
45        let path = Path::new("C:\\Users\\user\\Desktop\\test.txt");
46        let path = path.replace("C:\\Users\\user\\Desktop", "C:\\Users\\user\\Documents");
47        assert_eq!(path, PathBuf::from("C:\\Users\\user\\Documents\\test.txt"));
48
49
50        // Linux
51        let path = Path::new("/home/user/Desktop/test.txt");
52        let path = path.replace("/home/user/Desktop", "/home/user/Documents");
53        assert_eq!(path, PathBuf::from("/home/user/Documents/test.txt"));
54
55        // Mixed
56        let path = Path::new("C:\\Users\\user\\Desktop\\test.txt");
57        let path = path.replace("user/Desktop", "user/Documents");
58        assert_eq!(path, PathBuf::from("C:\\Users\\user\\Documents\\test.txt"));
59    }
60
61    #[test]
62    fn test_contains() {
63        let path = Path::new("C:\\Users\\user\\Desktop\\test.txt");
64        assert!(path.contains("Desktop"));
65        assert!(!path.contains("Documents"));
66        
67        // Windows
68        assert!(path.contains("user\\Desktop"));
69        assert!(!path.contains("user\\Documents"));
70
71        // Linux
72        assert!(path.contains("user/Desktop"));
73        assert!(!path.contains("user/Documents"));
74
75    }
76
77}
78
79/*
80use std::path::{Path, PathBuf};
81
82pub trait PathExt {
83    fn replace<T: ToString>(&self, old: T, new: T) -> PathBuf;
84    fn contains<T: ToString>(&self, path: T) -> bool;
85}
86
87impl PathExt for Path {
88    fn replace<T: ToString>(&self, old: T, new: T) -> PathBuf {
89        let mut path = self.to_str().unwrap().to_string();
90
91        let new = Path::from(new.to_string()).to_str().unwrap();
92        let old = Path::from(old.to_string()).to_str().unwrap();
93
94        path = path.replace(old, new);
95        PathBuf::from(path)
96    }
97
98    fn contains<T: ToString>(&self, s: T) -> bool {
99        let path = self.to_str().unwrap().to_string();
100
101        let s = Path::from(s.to_string()).to_str().unwrap();
102
103        path.contains(s)
104
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn test_replace() {
114        let path = Path::new("C:\\Users\\user\\Desktop\\test.txt");
115        let path = path.replace("C:\\Users\\user\\Desktop", "C:\\Users\\user\\Documents");
116        assert_eq!(path, PathBuf::from("C:\\Users\\user\\Documents\\test.txt"));
117    }
118
119    #[test]
120    fn test_contains() {
121        let path = Path::new("C:\\Users\\user\\Desktop\\test.txt");
122        assert!(path.contains("Desktop"));
123        assert!(!path.contains("Documents"));
124    }
125}
126*/