Struct path_abs::PathDir

source ·
pub struct PathDir(_);
Expand description

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

Implementations§

source§

impl PathDir

source

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

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")?;
source

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

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.

source

pub fn current_dir() -> Result<PathDir>

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

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

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

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)?;
source

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

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)?;
source

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

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)?;
source

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

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());
source

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

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);
source

pub fn remove(self) -> Result<()>

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()?;
source

pub fn remove_all(self) -> Result<()>

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);
source

pub fn as_path(&self) -> &Path

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

source

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

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

See PathAbs::canonicalize

source

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

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§

source§

impl AsRef<OsStr> for PathDir

source§

fn as_ref(&self) -> &OsStr

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<Path> for PathDir

source§

fn as_ref(&self) -> &Path

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<PathAbs> for PathDir

source§

fn as_ref(&self) -> &PathAbs

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<PathBuf> for PathDir

source§

fn as_ref(&self) -> &PathBuf

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<'a> Borrow<Path> for &'a PathDir

source§

fn borrow(&self) -> &Path

Immutably borrows from an owned value. Read more
source§

impl Borrow<Path> for PathDir

source§

fn borrow(&self) -> &Path

Immutably borrows from an owned value. Read more
source§

impl<'a> Borrow<PathAbs> for &'a PathDir

source§

fn borrow(&self) -> &PathAbs

Immutably borrows from an owned value. Read more
source§

impl Borrow<PathAbs> for PathDir

source§

fn borrow(&self) -> &PathAbs

Immutably borrows from an owned value. Read more
source§

impl<'a> Borrow<PathBuf> for &'a PathDir

source§

fn borrow(&self) -> &PathBuf

Immutably borrows from an owned value. Read more
source§

impl Borrow<PathBuf> for PathDir

source§

fn borrow(&self) -> &PathBuf

Immutably borrows from an owned value. Read more
source§

impl Clone for PathDir

source§

fn clone(&self) -> PathDir

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for PathDir

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<PathDir> for Arc<PathBuf>

source§

fn from(path: PathDir) -> Arc<PathBuf>

Converts to this type from the input type.
source§

impl From<PathDir> for PathAbs

source§

fn from(path: PathDir) -> PathAbs

Converts to this type from the input type.
source§

impl From<PathDir> for PathBuf

source§

fn from(path: PathDir) -> PathBuf

Converts to this type from the input type.
source§

impl Hash for PathDir

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for PathDir

source§

fn cmp(&self, other: &PathDir) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<PathDir> for PathDir

source§

fn eq(&self, other: &PathDir) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<PathDir> for PathDir

source§

fn partial_cmp(&self, other: &PathDir) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PathOps for PathDir

§

type Output = PathAbs

source§

fn concat<P: AsRef<Path>>(&self, path: P) -> Result<Self::Output>

Returns a new value representing the concatenation of two paths. Read more
source§

fn join<P: AsRef<Path>>(&self, path: P) -> Self::Output

An exact replica of std::path::Path::join with all of its gotchas and pitfalls,, except returns a more relevant type. Read more
source§

fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> Self::Output

Creates a new path object like self but with the given file name. Read more
source§

fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> Self::Output

Creates a new path object like self but with the given extension. Read more
source§

impl Eq for PathDir

source§

impl StructuralEq for PathDir

source§

impl StructuralPartialEq for PathDir

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> PathInfo for Twhere T: Clone + Borrow<PathBuf> + Into<Arc<PathBuf>>,

source§

fn as_path(&self) -> &Path

source§

fn to_arc_pathbuf(&self) -> Arc<PathBuf>

source§

fn as_os_str(&self) -> &OsStr

source§

fn to_str(&self) -> Option<&str>

source§

fn to_string_lossy(&self) -> Cow<'_, str>

source§

fn is_absolute(&self) -> bool

source§

fn is_relative(&self) -> bool

source§

fn has_root(&self) -> bool

source§

fn ancestors(&self) -> Ancestors<'_>

source§

fn file_name(&self) -> Option<&OsStr>

source§

fn strip_prefix<P>(&self, base: P) -> Result<&Path, StripPrefixError>where P: AsRef<Path>,

source§

fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool

source§

fn ends_with<P: AsRef<Path>>(&self, base: P) -> bool

source§

fn file_stem(&self) -> Option<&OsStr>

source§

fn extension(&self) -> Option<&OsStr>

source§

fn components(&self) -> Components<'_>

source§

fn iter(&self) -> Iter<'_>

source§

fn display(&self) -> Display<'_>

source§

fn metadata(&self) -> Result<Metadata>

Queries the file system to get information about a file, directory, etc. Read more
Queries the metadata about a file without following symlinks. Read more
source§

fn exists(&self) -> bool

source§

fn is_file(&self) -> bool

source§

fn is_dir(&self) -> bool

Reads a symbolic link, returning the path that the link points to. Read more
source§

fn canonicalize(&self) -> Result<PathAbs>

Returns the canonical, absolute form of the path with all intermediate components normalized and symbolic links resolved. Read more
source§

fn parent(&self) -> Result<&Path>

Returns the path without its final component, if there is one. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.