tourist_types/path/
absolute.rs1use std::path::{Path, PathBuf};
2
3#[derive(PartialEq, Eq)]
4pub struct AbsolutePathBuf(PathBuf);
5
6impl AbsolutePathBuf {
7 pub fn new(p: PathBuf) -> Option<Self> {
8 if p.is_absolute() {
9 Some(AbsolutePathBuf(p))
10 } else {
11 None
12 }
13 }
14
15 pub fn new_unchecked(p: PathBuf) -> Self {
16 debug_assert!(p.is_absolute());
17 AbsolutePathBuf(p)
18 }
19
20 pub fn as_path_buf(&self) -> &PathBuf {
21 &self.0
22 }
23
24 pub fn as_absolute_path(&self) -> AbsolutePath<'_> {
25 AbsolutePath(&self.0)
26 }
27}
28
29#[derive(PartialEq, Eq)]
30pub struct AbsolutePath<'a>(&'a Path);
31
32impl<'a> AbsolutePath<'a> {
33 pub fn new(p: &'a Path) -> Option<Self> {
34 if p.is_absolute() {
35 Some(AbsolutePath(p))
36 } else {
37 None
38 }
39 }
40
41 pub fn new_unchecked(p: &'a Path) -> Self {
42 debug_assert!(p.is_absolute());
43 AbsolutePath(p)
44 }
45
46 pub fn as_path(&self) -> &Path {
47 self.0
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::AbsolutePathBuf;
54 use dirs;
55 use std::path::Path;
56
57 #[test]
58 fn create_abs_path() {
59 let abs = dirs::home_dir().unwrap().join("some").join("path");
60 let not_abs = Path::new("some").join("path");
61 assert_eq!(
62 &abs.clone(),
63 AbsolutePathBuf::new(abs).unwrap().as_path_buf()
64 );
65 assert!(AbsolutePathBuf::new(not_abs).is_none());
66 }
67}