Skip to main content

PathExt

Trait PathExt 

Source
pub trait PathExt {
Show 39 methods // Required methods fn abs(&self) -> Result<PathBuf, FuError>; fn abs_from<T>(&self, path: T) -> Result<PathBuf, FuError> where T: AsRef<Path>; fn base(&self) -> Result<String, FuError>; fn chmod(&self, mode: u32) -> Result<(), FuError>; fn clean(&self) -> Result<PathBuf, FuError>; fn concat<T>(&self, val: T) -> Result<PathBuf, FuError> where T: AsRef<str>; fn dir(&self) -> Result<PathBuf, FuError>; fn empty(&self) -> bool; fn exists(&self) -> bool; fn expand(&self) -> Result<PathBuf, FuError>; fn ext(&self) -> Result<String, FuError>; fn first(&self) -> Result<Component<'_>, FuError>; fn gid(&self) -> Result<u32, FuError>; fn has<T>(&self, path: T) -> bool where T: AsRef<Path>; fn has_prefix<T>(&self, prefix: T) -> bool where T: AsRef<Path>; fn has_suffix<T>(&self, suffix: T) -> bool where T: AsRef<Path>; fn is_dir(&self) -> bool; fn is_exec(&self) -> bool; fn is_file(&self) -> bool; fn is_readonly(&self) -> bool; fn is_symlink(&self) -> bool; fn is_symlink_dir(&self) -> bool; fn is_symlink_file(&self) -> bool; fn last(&self) -> Result<Component<'_>, FuError>; fn mash<T>(&self, path: T) -> PathBuf where T: AsRef<Path>; fn metadata(&self) -> Result<Metadata, FuError>; fn mode(&self) -> Result<u32, FuError>; fn name(&self) -> Result<String, FuError>; fn perms(&self) -> Result<Permissions, FuError>; fn readlink(&self) -> Result<PathBuf, FuError>; fn relative_from<T>(&self, path: T) -> Result<PathBuf, FuError> where T: AsRef<Path>; fn setperms(&self, perms: Permissions) -> Result<PathBuf, FuError>; fn trim_ext(&self) -> Result<PathBuf, FuError>; fn trim_first(&self) -> PathBuf; fn trim_last(&self) -> PathBuf; fn trim_prefix<T>(&self, prefix: T) -> PathBuf where T: AsRef<Path>; fn trim_protocol(&self) -> PathBuf; fn trim_suffix<T>(&self, suffix: T) -> PathBuf where T: AsRef<Path>; fn uid(&self) -> Result<u32, FuError>;
}

Required Methods§

Source

fn abs(&self) -> Result<PathBuf, FuError>

Return the path in an absolute clean form

§Examples
use fungus::prelude::*;

let home = user::home_dir().unwrap();
assert_eq!(PathBuf::from(&home), sys::abs("~").unwrap());
Source

fn abs_from<T>(&self, path: T) -> Result<PathBuf, FuError>
where T: AsRef<Path>,

Returns a new absolute PathBuf based on the given absolute Path. The last element of the given path will be assumed to be a file name.

§Examples
use fungus::prelude::*;

let home = PathBuf::from("~").abs().unwrap();
assert_eq!(PathBuf::from("foo2").abs_from(home.mash("foo1").abs().unwrap()).unwrap(), home.mash("foo2"));
Source

fn base(&self) -> Result<String, FuError>

Returns the final component of the Path, if there is one.

§Examples
use fungus::prelude::*;

assert_eq!("bar", PathBuf::from("/foo/bar").base().unwrap());
Source

fn chmod(&self, mode: u32) -> Result<(), FuError>

Set the given mode for the Path and return the Path

§Examples
use fungus::prelude::*;

let tmpdir = PathBuf::from("tests/temp").abs().unwrap().mash("pathbuf_doc_chmod");
assert!(sys::remove_all(&tmpdir).is_ok());
let file1 = tmpdir.mash("file1");
assert!(sys::mkdir(&tmpdir).is_ok());
assert!(sys::touch(&file1).is_ok());
assert!(file1.chmod(0o644).is_ok());
assert_eq!(file1.mode().unwrap(), 0o100644);
assert!(file1.chmod(0o555).is_ok());
assert_eq!(file1.mode().unwrap(), 0o100555);
assert!(sys::remove_all(&tmpdir).is_ok());
Source

fn clean(&self) -> Result<PathBuf, FuError>

Return the shortest path equivalent to the path by purely lexical processing and thus does not handle links correctly in some cases, use canonicalize in those cases. It applies the following rules interatively until no further processing can be done.

  1. Replace multiple slashes with a single
  2. Eliminate each . path name element (the current directory)
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace “/..” by “/” at the beginning of a path.
  5. Leave intact “..” elements that begin a non-rooted path.
  6. Drop trailing ‘/’ unless it is the root

If the result of this process is an empty string, return the string ., representing the current directory.

Source

fn concat<T>(&self, val: T) -> Result<PathBuf, FuError>
where T: AsRef<str>,

Returns the Path with the given string concatenated on.

§Examples
use fungus::prelude::*;

assert_eq!(Path::new("/foo/bar").concat(".rs").unwrap(), PathBuf::from("/foo/bar.rs"));
Source

fn dir(&self) -> Result<PathBuf, FuError>

Returns the Path without its final component, if there is one.

§Examples
use fungus::prelude::*;

let dir = PathBuf::from("/foo/bar").dir().unwrap();
assert_eq!(PathBuf::from("/foo").as_path(), dir);
Source

fn empty(&self) -> bool

Returns true if the Path is empty.

§Examples
use fungus::prelude::*;

assert_eq!(PathBuf::from("").empty(), true);
Source

fn exists(&self) -> bool

Returns true if the Path exists. Handles path expansion.

§Examples
use fungus::prelude::*;

assert_eq!(Path::new("/etc").exists(), true);
Source

fn expand(&self) -> Result<PathBuf, FuError>

Expand the path to include the home prefix if necessary

§Examples
use fungus::prelude::*;

let home = user::home_dir().unwrap();
assert_eq!(PathBuf::from(&home).mash("foo"), PathBuf::from("~/foo").expand().unwrap());
Source

fn ext(&self) -> Result<String, FuError>

Returns the extension of the path or an error.

§Examples
use fungus::prelude::*;

assert_eq!(Path::new("foo.bar").ext().unwrap(), "bar");
Source

fn first(&self) -> Result<Component<'_>, FuError>

Returns the first path component.

§Examples
use fungus::prelude::*;
use std::path::Component;

let first = Component::Normal(OsStr::new("foo"));
assert_eq!(PathBuf::from("foo/bar").first().unwrap(), first);
Source

fn gid(&self) -> Result<u32, FuError>

Returns the group ID of the owner of this file.

§Examples
use fungus::prelude::*;

assert_eq!(Path::new("/etc").gid().unwrap(), 0);
Source

fn has<T>(&self, path: T) -> bool
where T: AsRef<Path>,

Returns true if the Path contains the given path or string.

§Examples
use fungus::prelude::*;

let path = PathBuf::from("/foo/bar");
assert_eq!(path.has("foo"), true);
assert_eq!(path.has("/foo"), true);
Source

fn has_prefix<T>(&self, prefix: T) -> bool
where T: AsRef<Path>,

Returns true if the Path as a String has the given prefix

§Examples
use fungus::prelude::*;

let path = PathBuf::from("/foo/bar");
assert_eq!(path.has_prefix("/foo"), true);
assert_eq!(path.has_prefix("foo"), false);
Source

fn has_suffix<T>(&self, suffix: T) -> bool
where T: AsRef<Path>,

Returns true if the Path as a String has the given suffix

§Examples
use fungus::prelude::*;

let path = PathBuf::from("/foo/bar");
assert_eq!(path.has_suffix("/bar"), true);
assert_eq!(path.has_suffix("foo"), false);
Source

fn is_dir(&self) -> bool

Returns true if the Path exists and is a directory. Handles path expansion.

§Examples
use fungus::prelude::*;

assert_eq!(Path::new("/etc").is_dir(), true);
Source

fn is_exec(&self) -> bool

Returns true if the Path exists and is an executable. Handles path expansion.

§Examples
use fungus::prelude::*;

let tmpdir = PathBuf::from("tests/temp").abs().unwrap().mash("doc_is_exec");
assert!(sys::remove_all(&tmpdir).is_ok());
assert!(sys::mkdir(&tmpdir).is_ok());
let file1 = tmpdir.mash("file1");
assert!(sys::touch_p(&file1, 0o644).is_ok());
assert_eq!(file1.is_exec(), false);
assert!(sys::chmod_p(&file1).unwrap().add_x().chmod().is_ok());
assert_eq!(file1.mode().unwrap(), 0o100755);
assert_eq!(file1.is_exec(), true);
assert!(sys::remove_all(&tmpdir).is_ok());
Source

fn is_file(&self) -> bool

Returns true if the Path exists and is a file. Handles path expansion

§Examples
use fungus::prelude::*;

assert_eq!(Path::new("/etc/hosts").is_file(), true);
Source

fn is_readonly(&self) -> bool

Returns true if the Path exists and is readonly. Handles path expansion.

§Examples
use fungus::prelude::*;

let tmpdir = PathBuf::from("tests/temp").abs().unwrap().mash("doc_is_readonly");
assert!(sys::remove_all(&tmpdir).is_ok());
assert!(sys::mkdir(&tmpdir).is_ok());
let file1 = tmpdir.mash("file1");
assert!(sys::touch_p(&file1, 0o644).is_ok());
assert_eq!(file1.is_readonly(), false);
assert!(sys::chmod_p(&file1).unwrap().readonly().chmod().is_ok());
assert_eq!(file1.mode().unwrap(), 0o100444);
assert_eq!(file1.is_readonly(), true);
assert!(sys::remove_all(&tmpdir).is_ok());

Returns true if the Path exists and is a symlink. Handles path expansion

§Examples
use fungus::prelude::*;

let tmpdir = PathBuf::from("tests/temp").abs().unwrap().mash("pathbuf_doc_is_symlink");
assert!(sys::remove_all(&tmpdir).is_ok());
let file1 = tmpdir.mash("file1");
let link1 = tmpdir.mash("link1");
assert!(sys::mkdir(&tmpdir).is_ok());
assert!(sys::touch(&file1).is_ok());
assert!(sys::symlink(&link1, &file1).is_ok());
assert_eq!(link1.is_symlink(), true);
assert!(sys::remove_all(&tmpdir).is_ok());

Returns true if the Path exists and is a symlinked directory. Handles path expansion

§Examples
use fungus::prelude::*;

let tmpdir = PathBuf::from("tests/temp").abs().unwrap().mash("pathbuf_doc_is_symlink_dir");
assert!(sys::remove_all(&tmpdir).is_ok());
let dir1 = tmpdir.mash("dir1");
let link1 = tmpdir.mash("link1");
assert!(sys::mkdir(&dir1).is_ok());
assert!(sys::symlink(&link1, &dir1).is_ok());
assert_eq!(link1.is_symlink_dir(), true);
assert!(sys::remove_all(&tmpdir).is_ok());

Returns true if the given Path exists and is a symlinked file. Handles path expansion

§Examples
use fungus::prelude::*;

let tmpdir = PathBuf::from("tests/temp").abs().unwrap().mash("pathbuf_doc_is_symlink_file");
assert!(sys::remove_all(&tmpdir).is_ok());
let file1 = tmpdir.mash("file1");
let link1 = tmpdir.mash("link1");
assert!(sys::mkdir(&tmpdir).is_ok());
assert!(sys::touch(&file1).is_ok());
assert!(sys::symlink(&link1, &file1).is_ok());
assert_eq!(link1.is_symlink_file(), true);
assert!(sys::remove_all(&tmpdir).is_ok());
Source

fn last(&self) -> Result<Component<'_>, FuError>

Returns the last path component.

§Examples
use fungus::prelude::*;
use std::path::Component;

let first = Component::Normal(OsStr::new("bar"));
assert_eq!(PathBuf::from("foo/bar").last().unwrap(), first);
Source

fn mash<T>(&self, path: T) -> PathBuf
where T: AsRef<Path>,

Returns a new owned PathBuf from self mashed together with path. Differs from the mash implementation as mash drops root prefix of the given path if it exists and also drops any trailing ‘/’ on the new resulting path. More closely aligns with the Golang implementation of join.

§Examples
use fungus::prelude::*;

assert_eq!(Path::new("/foo").mash("/bar"), PathBuf::from("/foo/bar"));
Source

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

Returns the Metadata object for the Path if it exists else and error

§Examples
use fungus::prelude::*;

let meta = Path::new("/etc").metadata().unwrap();
assert_eq!(meta.is_dir(), true);
Source

fn mode(&self) -> Result<u32, FuError>

Returns the Metadata object for the Path if it exists else and error

§Examples
use fungus::prelude::*;

let tmpdir = PathBuf::from("tests/temp").abs().unwrap().mash("pathbuf_doc_mode");
assert!(sys::remove_all(&tmpdir).is_ok());
let file1 = tmpdir.mash("file1");
assert!(sys::mkdir(&tmpdir).is_ok());
assert!(sys::touch(&file1).is_ok());
assert!(file1.chmod(0o644).is_ok());
assert_eq!(file1.mode().unwrap(), 0o100644);
assert!(sys::remove_all(&tmpdir).is_ok());
Source

fn name(&self) -> Result<String, FuError>

Returns the final component of the Path without an extension if there is one

§Examples
use fungus::prelude::*;

assert_eq!(PathBuf::from("/foo/bar.foo").name().unwrap(), "bar");
Source

fn perms(&self) -> Result<Permissions, FuError>

Return the permissions for the Path

§Examples
use fungus::prelude::*;

let tmpdir = PathBuf::from("tests/temp").abs().unwrap().mash("pathbuf_doc_perms");
assert!(sys::remove_all(&tmpdir).is_ok());
let file1 = tmpdir.mash("file1");
assert!(sys::mkdir(&tmpdir).is_ok());
assert!(sys::touch(&file1).is_ok());
assert!(file1.chmod(0o644).is_ok());
assert_eq!(file1.perms().unwrap().mode(), 0o100644);
assert!(sys::remove_all(&tmpdir).is_ok());

Returns the absolute path for the link target. Handles path expansion

§Examples
use fungus::prelude::*;

let tmpdir = PathBuf::from("tests/temp").abs().unwrap().mash("pathbuf_doc_readlink");
assert!(sys::remove_all(&tmpdir).is_ok());
let file1 = tmpdir.mash("file1");
let link1 = tmpdir.mash("link1");
assert!(sys::mkdir(&tmpdir).is_ok());
assert!(sys::touch(&file1).is_ok());
assert!(sys::symlink(&link1, &file1).is_ok());
assert_eq!(link1.readlink().unwrap(), file1);
assert!(sys::remove_all(&tmpdir).is_ok());
Source

fn relative_from<T>(&self, path: T) -> Result<PathBuf, FuError>
where T: AsRef<Path>,

Returns the Path relative to the given Path

§Examples
use fungus::prelude::*;

assert_eq!(PathBuf::from("foo/bar1").relative_from("foo/bar2").unwrap(), PathBuf::from("bar1"));
Source

fn setperms(&self, perms: Permissions) -> Result<PathBuf, FuError>

Set the given [Permissions] on the Path and return the Path

§Examples
use fungus::prelude::*;

let tmpdir = PathBuf::from("tests/temp").abs().unwrap().mash("pathbuf_doc_setperms");
assert!(sys::remove_all(&tmpdir).is_ok());
let file1 = tmpdir.mash("file1");
assert!(sys::mkdir(&tmpdir).is_ok());
assert!(sys::touch(&file1).is_ok());
assert!(file1.chmod(0o644).is_ok());
assert_eq!(file1.perms().unwrap().mode(), 0o100644);
assert!(file1.setperms(fs::Permissions::from_mode(0o555)).is_ok());
assert_eq!(file1.perms().unwrap().mode(), 0o100555);
assert!(sys::remove_all(&tmpdir).is_ok());
Source

fn trim_ext(&self) -> Result<PathBuf, FuError>

Returns a new PathBuf with the file extension trimmed off.

§Examples
use fungus::prelude::*;

assert_eq!(Path::new("foo.exe").trim_ext().unwrap(), PathBuf::from("foo"));
Source

fn trim_first(&self) -> PathBuf

Returns a new PathBuf with first Component trimmed off.

§Examples
use fungus::prelude::*;

assert_eq!(PathBuf::from("/foo").trim_first(), PathBuf::from("foo"));
Source

fn trim_last(&self) -> PathBuf

Returns a new PathBuf with last Component trimmed off.

§Examples
use fungus::prelude::*;

assert_eq!(PathBuf::from("/foo").trim_last(), PathBuf::from("/"));
Source

fn trim_prefix<T>(&self, prefix: T) -> PathBuf
where T: AsRef<Path>,

Returns a new PathBuf with the given prefix trimmed off else the original path.

§Examples
use fungus::prelude::*;

assert_eq!(Path::new("/foo/bar").trim_prefix("/foo"), PathBuf::from("/bar"));
Source

fn trim_protocol(&self) -> PathBuf

Returns a new PathBuf with well known protocol prefixes trimmed off else the original path.

§Examples
use fungus::prelude::*;

assert_eq!(PathBuf::from("ftp://foo").trim_protocol(), PathBuf::from("foo"));
Source

fn trim_suffix<T>(&self, suffix: T) -> PathBuf
where T: AsRef<Path>,

Returns a new PathBuf with the given suffix trimmed off else the original path.

§Examples
use fungus::prelude::*;

assert_eq!(PathBuf::from("/foo/bar").trim_suffix("/bar"), PathBuf::from("/foo"));
Source

fn uid(&self) -> Result<u32, FuError>

Returns the user ID of the owner of this file.

§Examples
use fungus::prelude::*;

assert_eq!(Path::new("/etc").uid().unwrap(), 0);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§