pub struct PathDir(_);
Expand description
A PathAbs
that is guaranteed to be a directory, with associated methods.
Implementations§
source§impl PathDir
impl PathDir
sourcepub fn new<P: AsRef<Path>>(path: P) -> Result<PathDir>
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")?;
sourcepub fn new_unchecked<P: Into<Arc<PathBuf>>>(path: P) -> PathDir
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.
sourcepub fn current_dir() -> Result<PathDir>
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()?;
sourcepub fn try_from<P: Into<PathAbs>>(path: P) -> Result<PathDir>
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)?;
sourcepub fn create<P: AsRef<Path>>(path: P) -> Result<PathDir>
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)?;
sourcepub fn create_all<P: AsRef<Path>>(path: P) -> Result<PathDir>
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)?;
sourcepub fn join_abs<P: AsRef<Path>>(&self, path: P) -> Result<PathType>
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());
sourcepub fn list(&self) -> Result<ListDir>
pub fn list(&self) -> Result<ListDir>
List the contents of the directory, returning an iterator of PathType
s.
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);
sourcepub fn remove(self) -> Result<()>
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()?;
sourcepub fn remove_all(self) -> Result<()>
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());
sourcepub fn symlink<P: AsRef<Path>>(&self, dst: P) -> Result<PathDir>
pub fn symlink<P: AsRef<Path>>(&self, dst: P) -> Result<PathDir>
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);
sourcepub fn canonicalize(&self) -> Result<PathDir>
pub fn canonicalize(&self) -> Result<PathDir>
Returns the canonical form of the path with all intermediate components normalized and symbolic links resolved.
sourcepub fn parent_dir(&self) -> Option<PathDir>
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 Ord for PathDir
impl Ord for PathDir
source§impl PartialEq<PathDir> for PathDir
impl PartialEq<PathDir> for PathDir
source§impl PartialOrd<PathDir> for PathDir
impl PartialOrd<PathDir> for PathDir
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PathOps for PathDir
impl PathOps for PathDir
type Output = PathAbs
source§fn concat<P: AsRef<Path>>(&self, path: P) -> Result<Self::Output>
fn concat<P: AsRef<Path>>(&self, path: P) -> Result<Self::Output>
source§fn join<P: AsRef<Path>>(&self, path: P) -> Self::Output
fn join<P: AsRef<Path>>(&self, path: P) -> Self::Output
std::path::Path::join
with all of its gotchas and pitfalls,, except
returns a more relevant type. Read more