Path Calculate

This 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.

This 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.

The following examples show the usage about this crate.

Examples

There are some methods you can use.

home_dir

Get 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).

```rust extern crate path_calculate;

use std::path::Path;

use path_calculate::*;

let p = Path::new("/home/cc");

if let Some(homedir) = p.homedir() { println!("Home path: {:?}", home_dir); }

```

asabsolutepath

This is almost like a shadow of the absolutize in path-absolutize, I only add ~($HOME) support in the method(Unix or Windnows).

```rust extern crate path_calculate;

use std::path::Path;

use path_calculate::*;

// I use the user Chao, homd dir is /home/chao let p = Path::new("~/works/path-calculate");

asserteq!("/home/chao/works/path-calculate", p.asabsolutepath().unwrap().tostr().unwrap()); ```

```rust extern crate path_calculate;

use std::path::Path;

use path_calculate::*;

// In my Windows let p = Path::new("~\works\path-calculate");

asserteq!("C:\Users\jiach\works\path-calculate", p.asabsolutepath().unwrap().tostr().unwrap()); ```

relativerootwith

Sometimes I would use the relative root of pathes, it return an absolutize relativeroot path. Behold, in Windows, it can not calculate the relativeroot on different disks(C:\, D:\,,,), when you try to use this method it return an ioerror such as io::ErrorKind::InvalidInput.

```rust extern crate path_calculate;

use std::path::Path;

use path_calculate::*;

// In Linux, run by standard user, HOME in /home/${USERNAME} let p1 = Path::new("/home/gits/mkisos"); let p2 = Path::new("~/trash");

let relativeroot = p1.relativeroot_with(&p2);

asserteq!("/home", relativeroot.unwrap().to_str().unwrap()) ```

```rust extern crate path_calculate;

use std::io::ErrorKind; use std::path::Path;

use path_calculate::*;

// Windows ok let d1 = Path::new("D:\Games\Videos\Replays"); let d2 = Path::new("D:\Games\Dota2");

asserteq!("D:\Games", d1.relativerootwith(&d2).unwrap().tostr().unwrap());

// Windows error let c1 = Path::new("~"); asserteq!(Err(ErrorKind::InvalidInput), c1.relativerootwith(&d1).unwraperr().kind());

```

related_to

This method is used to calculate the dstpath's relative path from the srcpath.

```rust extern crate path_calculate;

use std::path::Path;

use path_calculate::*;

// $HOME="/home/chao" let dstpath = Path::new("/home/chao/works/demo/src"); let srcpath = Path::new("~/trash");

asserteq!("../works/demo/src", dstpath.relatedto(&srcpath).unwrap().to_str().unwrap()); ```