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§
Sourcefn abs(&self) -> Result<PathBuf, FuError>
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());Sourcefn abs_from<T>(&self, path: T) -> Result<PathBuf, FuError>
fn abs_from<T>(&self, path: T) -> Result<PathBuf, FuError>
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"));Sourcefn base(&self) -> Result<String, FuError>
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());Sourcefn chmod(&self, mode: u32) -> Result<(), FuError>
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());Sourcefn clean(&self) -> Result<PathBuf, FuError>
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.
- Replace multiple slashes with a single
- Eliminate each . path name element (the current directory)
- Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
- Eliminate .. elements that begin a rooted path: that is, replace “/..” by “/” at the beginning of a path.
- Leave intact “..” elements that begin a non-rooted path.
- Drop trailing ‘/’ unless it is the root
If the result of this process is an empty string, return the string ., representing the
current directory.
Sourcefn concat<T>(&self, val: T) -> Result<PathBuf, FuError>
fn concat<T>(&self, val: T) -> Result<PathBuf, FuError>
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"));Sourcefn dir(&self) -> Result<PathBuf, FuError>
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);Sourcefn empty(&self) -> bool
fn empty(&self) -> bool
Returns true if the Path is empty.
§Examples
use fungus::prelude::*;
assert_eq!(PathBuf::from("").empty(), true);Sourcefn exists(&self) -> bool
fn exists(&self) -> bool
Returns true if the Path exists. Handles path expansion.
§Examples
use fungus::prelude::*;
assert_eq!(Path::new("/etc").exists(), true);Sourcefn expand(&self) -> Result<PathBuf, FuError>
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());Sourcefn ext(&self) -> Result<String, FuError>
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");Sourcefn first(&self) -> Result<Component<'_>, FuError>
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);Sourcefn gid(&self) -> Result<u32, FuError>
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);Sourcefn has<T>(&self, path: T) -> bool
fn has<T>(&self, path: T) -> bool
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);Sourcefn has_prefix<T>(&self, prefix: T) -> bool
fn has_prefix<T>(&self, prefix: T) -> bool
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);Sourcefn has_suffix<T>(&self, suffix: T) -> bool
fn has_suffix<T>(&self, suffix: T) -> bool
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);Sourcefn is_dir(&self) -> bool
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);Sourcefn is_exec(&self) -> bool
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());Sourcefn is_file(&self) -> bool
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);Sourcefn is_readonly(&self) -> bool
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());Sourcefn is_symlink(&self) -> bool
fn is_symlink(&self) -> bool
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());Sourcefn is_symlink_dir(&self) -> bool
fn is_symlink_dir(&self) -> bool
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());Sourcefn is_symlink_file(&self) -> bool
fn is_symlink_file(&self) -> bool
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());Sourcefn last(&self) -> Result<Component<'_>, FuError>
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);Sourcefn mash<T>(&self, path: T) -> PathBuf
fn mash<T>(&self, path: T) -> PathBuf
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"));Sourcefn metadata(&self) -> Result<Metadata, FuError>
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);Sourcefn mode(&self) -> Result<u32, FuError>
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());Sourcefn name(&self) -> Result<String, FuError>
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");Sourcefn perms(&self) -> Result<Permissions, FuError>
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());Sourcefn readlink(&self) -> Result<PathBuf, FuError>
fn readlink(&self) -> Result<PathBuf, FuError>
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());Sourcefn relative_from<T>(&self, path: T) -> Result<PathBuf, FuError>
fn relative_from<T>(&self, path: T) -> Result<PathBuf, FuError>
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"));Sourcefn setperms(&self, perms: Permissions) -> Result<PathBuf, FuError>
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());Sourcefn trim_first(&self) -> PathBuf
fn trim_first(&self) -> PathBuf
Sourcefn trim_prefix<T>(&self, prefix: T) -> PathBuf
fn trim_prefix<T>(&self, prefix: T) -> PathBuf
Sourcefn trim_protocol(&self) -> PathBuf
fn trim_protocol(&self) -> PathBuf
Sourcefn trim_suffix<T>(&self, suffix: T) -> PathBuf
fn trim_suffix<T>(&self, suffix: T) -> PathBuf
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.