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("/tmp");

if let Ok(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::*;

// If u have $HOME, test ~ support let p = Path::new("./whatever"); if let Ok(homedir) = p.homedir() { let p = Path::new("~"); asserteq!(homedir.tostr().unwrap(), p.asabsolutepath().unwrap().tostr().unwrap()); }

let p2 = Path::new("/tmp/a/b/c/../../e/f");

asserteq!("/tmp/a/e/f", p2.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::*;

let p1 = Path::new("/home/gits/mkisos"); let p2 = Path::new("/home/cc/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::*;

// Pass Test when in Unix if cfg!(target_os = "windows") { // Windows ok let d1 = Path::new("D:\Games\Videos\Replays"); let d2 = Path::new("D:\Games\Dota2");

assert_eq!("D:\\Games", d1.relative_root_with(&d2).unwrap().to_str().unwrap());

// Windows err
let c1 = Path::new("~");

assert_eq!(ErrorKind::InvalidInput, c1.relative_root_with(&d1).unwrap_err().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("/home/chao/trash");

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

add_path

Band patha with pathb(Pathb should be a relative path, can not contain ~, too). It return an abs path, I think u must want this. ```rust extern crate pathcalculate;

use std::path::Path;

use path_calculate::*;

let base_path = Path::new("/home/chao");

let extra_path = Path::new("./works/demo");

asserteq!("/home/chao/works/demo", basepath.addpath(&extrapath).unwrap().to_str().unwrap());

let empty_path = Path::new("works/../../gits/kernel");

asserteq!("/home/gits/kernel", basepath.addpath(&emptypath).unwrap().to_str().unwrap()); ```