Struct path_abs::PathDir[][src]

pub struct PathDir(_);

A PathAbs that is guaranteed to be a directory, with associated methods.

Implementations

impl PathDir[src]

pub fn new<P: AsRef<Path>>(path: P) -> Result<PathDir>[src]

Instantiate a new PathDir. The directory must exist or io::Error will be returned.

Returns io::ErrorKind::InvalidInput if the path exists but is not a directory.

Examples

use path_abs::PathDir;

let src = PathDir::new("src")?;

pub fn new_unchecked<P: Into<Arc<PathBuf>>>(path: P) -> PathDir[src]

Create a PathDir unchecked.

This is mostly used for constructing during tests, or if the path was previously validated. This is effectively the same as a Arc<PathBuf>.

Note: This is memory safe, so is not marked unsafe. However, it could cause panics in some methods if the path was not properly validated.

pub fn current_dir() -> Result<PathDir>[src]

Returns the current working directory from the env as a PathDir.

Examples

use path_abs::PathDir;
let cwd = PathDir::current_dir()?;

pub fn try_from<P: Into<PathAbs>>(path: P) -> Result<PathDir>[src]

Consume the PathAbs validating that the path is a directory and returning PathDir. The directory must exist or io::Error will be returned.

If the path is actually a file returns io::ErrorKind::InvalidInput.

Examples

use path_abs::{PathAbs, PathDir};

let src_abs = PathAbs::new("src")?;
let src_dir = PathDir::try_from(src_abs)?;

pub fn create<P: AsRef<Path>>(path: P) -> Result<PathDir>[src]

Instantiate a new PathDir to a directory, creating the directory if it doesn't exist.

Examples

use path_abs::PathDir;

let example = "example";

let dir = PathDir::create(example)?;

// It can be done twice with no effect.
let _ = PathDir::create(example)?;

pub fn create_all<P: AsRef<Path>>(path: P) -> Result<PathDir>[src]

Instantiate a new PathDir to a directory, recursively recreating it and all of its parent components if they are missing.

Examples

use path_abs::PathDir;

let example = "example/long/path";

let path = PathDir::create_all(example)?;

// It can be done twice with no effect.
let _ = PathDir::create_all(example)?;

pub fn join_abs<P: AsRef<Path>>(&self, path: P) -> Result<PathType>[src]

Join a path onto the PathDir, expecting it to exist. Returns the resulting PathType.

Examples

use path_abs::{PathDir, PathFile, PathInfo};

let src = PathDir::new("src")?;
let lib = src.join_abs("lib.rs")?.unwrap_file();
assert!(lib.is_file());

pub fn list(&self) -> Result<ListDir>[src]

List the contents of the directory, returning an iterator of PathTypes.

Examples

use std::collections::HashSet;
use path_abs::{PathDir, PathFile, PathType, PathOps};

let example = "example";

let example_dir = PathDir::create(example)?;
let foo_dir = PathDir::create(example_dir.concat("foo")?)?;
let bar_file = PathFile::create(example_dir.concat("bar.txt")?)?;

let mut result = HashSet::new();
for p in example_dir.list()? {
    result.insert(p?);
}

let mut expected = HashSet::new();
expected.insert(PathType::Dir(foo_dir));
expected.insert(PathType::File(bar_file));

assert_eq!(expected, result);

pub fn remove(self) -> Result<()>[src]

Remove (delete) the empty directory from the filesystem, consuming self.

Examples

use std::path::Path;
use path_abs::PathDir;

let example = Path::new("example/long/path");

let dir = PathDir::create_all(example)?;
let parent = dir.parent_dir().unwrap();

assert!(example.exists());
dir.remove()?;
// assert!(dir.exists());  <--- COMPILE ERROR
assert!(!example.exists());
parent.remove()?;

pub fn remove_all(self) -> Result<()>[src]

Remove (delete) the directory, after recursively removing its contents. Use carefully!

Examples

use std::path::Path;
use path_abs::PathDir;

let example = Path::new("example/long/path");

let dir = PathDir::create_all(example)?;
let parent = dir.parent_dir().unwrap();

assert!(example.exists());
parent.remove_all()?;
assert!(!example.exists());

Creates a new symbolic link on the filesystem to the dst.

This handles platform specific behavior correctly.

Examples

use path_abs::{PathDir, PathFile, PathOps};
use std::path::Path;

let example = "example";
let example_sym = "example_sym";
let dir = PathDir::create(example)?;
let file = PathFile::create(dir.concat("example.txt")?)?;

let dir_sym = dir.symlink(example_sym)?;

// They have a different "absolute path"
assert_ne!(dir, dir_sym);

// But they can be canonicalized to the same file.
let dir_can = dir_sym.canonicalize()?;
assert_eq!(dir, dir_can);

pub fn as_path(&self) -> &Path[src]

Return a reference to a basic std::path::Path

pub fn canonicalize(&self) -> Result<PathDir>[src]

Returns the canonical form of the path with all intermediate components normalized and symbolic links resolved.

See PathAbs::canonicalize

pub fn parent_dir(&self) -> Option<PathDir>[src]

Get the parent directory of this directory as a PathDir.

This does not make aditional syscalls, as the parent by definition must be a directory and exist.

Examples

use path_abs::PathDir;

let src = PathDir::new("src")?;
let proj = src.parent_dir().unwrap();
assert_eq!(PathDir::new("src/..")?, proj);

Trait Implementations

impl AsRef<OsStr> for PathDir[src]

impl AsRef<Path> for PathDir[src]

impl AsRef<PathAbs> for PathDir[src]

impl AsRef<PathBuf> for PathDir[src]

impl Borrow<Path> for PathDir[src]

impl<'a> Borrow<Path> for &'a PathDir[src]

impl Borrow<PathAbs> for PathDir[src]

impl<'a> Borrow<PathAbs> for &'a PathDir[src]

impl Borrow<PathBuf> for PathDir[src]

impl<'a> Borrow<PathBuf> for &'a PathDir[src]

impl Clone for PathDir[src]

impl Debug for PathDir[src]

impl<'de> Deserialize<'de> for PathDir[src]

impl Eq for PathDir[src]

impl From<PathDir> for PathAbs[src]

impl Hash for PathDir[src]

impl Ord for PathDir[src]

impl PartialEq<PathDir> for PathDir[src]

impl PartialOrd<PathDir> for PathDir[src]

impl PathOps for PathDir[src]

type Output = PathAbs

impl Serialize for PathDir[src]

impl StructuralEq for PathDir[src]

impl StructuralPartialEq for PathDir[src]

Auto Trait Implementations

impl RefUnwindSafe for PathDir

impl Send for PathDir

impl Sync for PathDir

impl Unpin for PathDir

impl UnwindSafe for PathDir

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> PathInfo for T where
    T: Clone + Borrow<PathBuf> + Into<Arc<PathBuf>>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToStfu8 for T where
    T: Borrow<PathBuf>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.