pub trait PathExt: AsRef<Path> {
// Provided methods
fn cleanup(&self) -> PathBuf { ... }
fn has_extension(&self, ext: impl AsRef<OsStr>) -> bool { ... }
fn has_extension_ignore_ascii_case(&self, ext: impl AsRef<OsStr>) -> bool { ... }
}
Expand description
Utility methods for std::path::Path[Buf]
Provided Methods§
sourcefn cleanup(&self) -> PathBuf
fn cleanup(&self) -> PathBuf
Cleanup/simplify path as much as possible
Examples
use mmrbi::PathExt;
assert_eq!(Path::new("a/b").cleanup(), Path::new("a/b"));
assert_eq!(Path::new("a/b/..").cleanup(), Path::new("a"));
assert_eq!(Path::new("a/b/../..").cleanup(), Path::new("."));
assert_eq!(Path::new("a/b/../../..").cleanup(), Path::new(".."));
assert_eq!(Path::new("a/b/../../../..").cleanup(), Path::new("../.."));
assert_eq!(Path::new("../../a/b").cleanup(), Path::new("../../a/b"));
assert_eq!(Path::new("../../a/b/..").cleanup(), Path::new("../../a"));
assert_eq!(Path::new("../../a/b/../..").cleanup(), Path::new("../.."));
assert_eq!(Path::new("../../a/b/../../..").cleanup(), Path::new("../../.."));
assert_eq!(Path::new("../../a/b/../../../..").cleanup(), Path::new("../../../.."));
if cfg!(windows) {
assert_eq!(Path::new(r"C:\foo\bar").cleanup(), Path::new(r"C:\foo\bar"));
assert_eq!(Path::new(r"\\?\C:\foo\bar").cleanup(), Path::new(r"C:\foo\bar"));
}
sourcefn has_extension(&self, ext: impl AsRef<OsStr>) -> bool
fn has_extension(&self, ext: impl AsRef<OsStr>) -> bool
Check the extension of a file
Examples
use mmrbi::PathExt;
// The full filename doesn't count as an extension
assert!(!Path::new("foo.tar.gz").has_extension(".foo.tar.gz"));
assert!(!Path::new("foo.tar.gz").has_extension( "foo.tar.gz"));
assert!(!Path::new("foo.tar.gz").has_extension( "oo.tar.gz"));
// Only the full extension (with or without a leading `.`) counts
assert!( Path::new("foo.tar.gz").has_extension(".tar.gz"));
assert!( Path::new("foo.tar.gz").has_extension( "tar.gz"));
assert!(!Path::new("foo.tar.gz").has_extension( "ar.gz"));
// Case sensitive
assert!(!Path::new("foo.tAr.gZ").has_extension(".tar.gz"));
assert!(!Path::new("foo.tAr.gZ").has_extension( "tar.gz"));
assert!(!Path::new("foo.tAr.gZ").has_extension( "ar.gz"));
assert!( Path::new("foo.tar.gz").has_extension(".gz"));
assert!( Path::new("foo.tar.gz").has_extension( "gz"));
assert!(!Path::new("foo.tar.gz").has_extension( "z"));
sourcefn has_extension_ignore_ascii_case(&self, ext: impl AsRef<OsStr>) -> bool
fn has_extension_ignore_ascii_case(&self, ext: impl AsRef<OsStr>) -> bool
Check the extension of a file
Examples
use mmrbi::PathExt;
// The full filename doesn't count as an extension
assert!(!Path::new("foo.tar.gz").has_extension_ignore_ascii_case(".foo.tar.gz"));
assert!(!Path::new("foo.tar.gz").has_extension_ignore_ascii_case( "foo.tar.gz"));
assert!(!Path::new("foo.tar.gz").has_extension_ignore_ascii_case( "oo.tar.gz"));
// Only the full extension (with or without a leading `.`) counts
assert!( Path::new("foo.tar.gz").has_extension_ignore_ascii_case(".tar.gz"));
assert!( Path::new("foo.tar.gz").has_extension_ignore_ascii_case( "tar.gz"));
assert!(!Path::new("foo.tar.gz").has_extension_ignore_ascii_case( "ar.gz"));
// Case insensitive
assert!( Path::new("foo.tAr.gZ").has_extension_ignore_ascii_case(".tar.gz"));
assert!( Path::new("foo.tAr.gZ").has_extension_ignore_ascii_case( "tar.gz"));
assert!(!Path::new("foo.tAr.gZ").has_extension_ignore_ascii_case( "ar.gz"));
assert!( Path::new("foo.tar.gz").has_extension_ignore_ascii_case(".gz"));
assert!( Path::new("foo.tar.gz").has_extension_ignore_ascii_case( "gz"));
assert!(!Path::new("foo.tar.gz").has_extension_ignore_ascii_case( "z"));