path_calculate/
lib.rs

1/*!
2# Path Calculate
3
4This is a library help you to calculate with `Path` or `PathBuf`, such as get absolute path, get the relate root or the relative path between two pathes, get the '~'(home_dir) if it exist.
5
6This is based on path_absolutize library, I've use `as_absolute_path` replace the row `absolutize`, because it do not support '~' which is use recently, at least in UNIX System.
7
8The following examples show the usage about this crate.
9
10## Examples
11
12There are some methods you can use.
13
14### home_dir
15Get the current user's HOME if it exist in your env, or it return an error(I'm lazy, so just put this method in the Calculate trait).
16
17```rust
18extern crate path_calculate;
19
20use std::path::Path;
21
22use path_calculate::*;
23
24let p = Path::new("/tmp");
25
26if let Ok(home_dir) = p.home_dir() {
27    println!("Home path: {:?}", home_dir);
28}
29
30```
31
32### as_absolute_path
33This is almost like a shadow of the `absolutize` in `path-absolutize`, I only add `~`($HOME) support in the method(Unix or Windnows).
34
35```rust
36extern crate path_calculate;
37
38use std::path::Path;
39
40use path_calculate::*;
41
42// If u have $HOME, test `~` support
43let p = Path::new("./whatever");
44if let Ok(home_dir) = p.home_dir() {
45    let p = Path::new("~");
46    assert_eq!(home_dir.to_str().unwrap(), p.as_absolute_path().unwrap().to_str().unwrap());
47}
48
49let p2 = Path::new("/tmp/a/b/c/../../e/f");
50
51assert_eq!("/tmp/a/e/f", p2.as_absolute_path().unwrap().to_str().unwrap());
52```
53
54### relative_root_with
55Sometimes I would use the relative root of pathes, it return an absolutize relative_root path.
56Behold, in Windows, it can not calculate the relative_root on different disks(C:\\, D:\\,,,), when you try to use this method it return an ioerror such as `io::ErrorKind::InvalidInput`.
57
58```rust
59extern crate path_calculate;
60
61use std::path::Path;
62
63use path_calculate::*;
64
65let p1 = Path::new("/home/gits/mkisos");
66let p2 = Path::new("/home/cc/trash");
67
68let relative_root = p1.relative_root_with(&p2);
69
70assert_eq!("/home", relative_root.unwrap().to_str().unwrap())
71```
72
73```rust
74extern crate path_calculate;
75
76use std::io::ErrorKind;
77use std::path::Path;
78
79use path_calculate::*;
80
81// Pass Test when in Unix
82if cfg!(target_os = "windows") {
83    // Windows ok
84    let d1 = Path::new("D:\\Games\\Videos\\Replays");
85    let d2 = Path::new("D:\\Games\\Dota2");
86    
87    assert_eq!("D:\\Games", d1.relative_root_with(&d2).unwrap().to_str().unwrap());
88    
89    // Windows err
90    let c1 = Path::new("~");
91
92    assert_eq!(ErrorKind::InvalidInput, c1.relative_root_with(&d1).unwrap_err().kind());
93}
94
95```
96
97### related_to
98This method is used to calculate the dst_path's relative path from the src_path.
99
100```rust
101extern crate path_calculate;
102
103use std::path::Path;
104
105use path_calculate::*;
106
107// $HOME="/home/chao"
108let dst_path = Path::new("/home/chao/works/demo/src");
109let src_path = Path::new("/home/chao/trash");
110
111assert_eq!("../works/demo/src", dst_path.related_to(&src_path).unwrap().to_str().unwrap());
112```
113*/
114pub extern crate path_absolutize;
115
116pub mod calculate;
117
118pub use calculate::*;