Skip to content

Commit

Permalink
feat: support giving cwd as an argument
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Sep 11, 2021
1 parent c8c50c0 commit 2fbfa97
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ match cwd_parent {
}
```

### Starting from a given current working directory

With the `absolutize_from` function, you can provide the current working directory that the relative paths should be resolved from.

```rs
extern crate path_absolutize;

use std::path::Path;

use path_absolutize::*;

let p = Path::new("../path/to/123/456");
let cwd = env::current_dir().unwrap();

println!("{}", p.absolutize_from(cwd).unwrap().to_str().unwrap());
```


### absolutize_virtually

Get an absolute path **only under a specific directory**.
Expand Down
3 changes: 3 additions & 0 deletions src/absolutize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub trait Absolutize {
/// Get an absolute path. This works even if the path does not exist.
fn absolutize(&self) -> io::Result<Cow<Path>>;

/// Get an absolute path. This works even if the path does not exist. It gets the current working directory as the second argument.
fn absolutize_from(&self, cwd: &Path) -> io::Result<Cow<'_, Path>>;

/// Get an absolute path. This works even if the path does not exist.
fn absolutize_virtually<P: AsRef<Path>>(&self, virtual_root: P) -> io::Result<Cow<Path>>;
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ impl Absolutize for PathBuf {
self.as_path().absolutize()
}

#[inline]
fn absolutize_from(&self, cwd: &Path) -> io::Result<Cow<'_, Path>> {
self.as_path().absolutize_from(cwd)
}

#[inline]
fn absolutize_virtually<P: AsRef<Path>>(&self, virtual_root: P) -> io::Result<Cow<Path>> {
self.as_path().absolutize_virtually(virtual_root)
Expand Down
9 changes: 6 additions & 3 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ use crate::Absolutize;

impl Absolutize for Path {
fn absolutize(&self) -> io::Result<Cow<Path>> {
let cwd = get_cwd!();
self.absolutize_from(&cwd)
}

fn absolutize_from(&self, cwd: &Path) -> io::Result<Cow<'_, Path>> {
let mut iter = self.components();

let mut has_change = false;

let cwd = get_cwd!();

if let Some(first_component) = iter.next() {
let mut tokens = Vec::new();

Expand Down Expand Up @@ -104,7 +107,7 @@ impl Absolutize for Path {
Ok(Cow::from(self))
}
} else {
Ok(Cow::from(cwd))
Ok(Cow::from(cwd.to_owned()))
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ use crate::Absolutize;

impl Absolutize for Path {
fn absolutize(&self) -> io::Result<Cow<Path>> {
let cwd = get_cwd!();
self.absolutize_from(&cwd)
}

fn absolutize_from(&self, cwd: &Path) -> io::Result<Cow<'_, Path>> {
let mut iter = self.components();

let mut has_change = false;

let cwd = get_cwd!();

if let Some(first_component) = iter.next() {
let mut tokens = Vec::new();

Expand Down Expand Up @@ -182,7 +185,7 @@ impl Absolutize for Path {
Ok(Cow::from(self))
}
} else {
Ok(Cow::from(cwd))
Ok(Cow::from(cwd.to_owned()))
}
}

Expand Down

0 comments on commit 2fbfa97

Please sign in to comment.