Enum typed_path::Utf8TypedPath
source · pub enum Utf8TypedPath<'a> {
Unix(&'a Utf8UnixPath),
Windows(&'a Utf8WindowsPath),
}Expand description
Represents a path with a known type that can be one of:
Variants§
Unix(&'a Utf8UnixPath)
Windows(&'a Utf8WindowsPath)
Implementations§
source§impl<'a> Utf8TypedPath<'a>
impl<'a> Utf8TypedPath<'a>
sourcepub fn new<S: AsRef<str> + ?Sized>(s: &'a S, type: PathType) -> Self
pub fn new<S: AsRef<str> + ?Sized>(s: &'a S, type: PathType) -> Self
Creates a new path with the given type as its encoding.
sourcepub fn derive<S: AsRef<str> + ?Sized>(s: &'a S) -> Self
pub fn derive<S: AsRef<str> + ?Sized>(s: &'a S) -> Self
Creates a new typed path from a byte slice by determining if the path represents a Windows
or Unix path. This is accomplished by first trying to parse as a Windows path. If the
resulting path contains a prefix such as C: or begins with a \, it is assumed to be a
Utf8WindowsPath; otherwise, the slice will be represented as a Utf8UnixPath.
Examples
use typed_path::Utf8TypedPath;
assert!(Utf8TypedPath::derive(r#"C:\some\path\to\file.txt"#).is_windows());
assert!(Utf8TypedPath::derive(r#"\some\path\to\file.txt"#).is_windows());
assert!(Utf8TypedPath::derive(r#"/some/path/to/file.txt"#).is_unix());
// NOTE: If we don't start with a backslash, it's too difficult to
// determine and we therefore just assume a Unix/POSIX path.
assert!(Utf8TypedPath::derive(r#"some\path\to\file.txt"#).is_unix());
assert!(Utf8TypedPath::derive("file.txt").is_unix());
assert!(Utf8TypedPath::derive("").is_unix());sourcepub fn to_path_buf(&self) -> Utf8TypedPathBuf
pub fn to_path_buf(&self) -> Utf8TypedPathBuf
Converts a Utf8TypedPath into a Utf8TypedPathBuf.
Examples
use typed_path::{Utf8TypedPath, Utf8TypedPathBuf};
let path_buf = Utf8TypedPath::derive("foo.txt").to_path_buf();
assert_eq!(path_buf, Utf8TypedPathBuf::from("foo.txt"));sourcepub fn is_absolute(&self) -> bool
pub fn is_absolute(&self) -> bool
Returns true if the Utf8TypedPath is absolute, i.e., if it is independent of
the current directory.
-
On Unix (
UnixPath]), a path is absolute if it starts with the root, sois_absoluteandhas_rootare equivalent. -
On Windows (
WindowsPath), a path is absolute if it has a prefix and starts with the root:c:\windowsis absolute, whilec:tempand\tempare not.
Examples
use typed_path::Utf8TypedPath;
assert!(!Utf8TypedPath::derive("foo.txt").is_absolute());sourcepub fn is_relative(&self) -> bool
pub fn is_relative(&self) -> bool
Returns true if the Utf8TypedPath is relative, i.e., not absolute.
See is_absolute’s documentation for more details.
Examples
use typed_path::Utf8TypedPath;
assert!(Utf8TypedPath::derive("foo.txt").is_relative());sourcepub fn has_root(&self) -> bool
pub fn has_root(&self) -> bool
Returns true if the Utf8TypedPath has a root.
-
On Unix (
Utf8UnixPath), a path has a root if it begins with/. -
On Windows (
Utf8WindowsPath), a path has a root if it:- has no prefix and begins with a separator, e.g.,
\windows - has a prefix followed by a separator, e.g.,
c:\windowsbut notc:windows - has any non-disk prefix, e.g.,
\\server\share
- has no prefix and begins with a separator, e.g.,
Examples
use typed_path::Utf8TypedPath;
assert!(Utf8TypedPath::derive("/etc/passwd").has_root());sourcepub fn parent(&self) -> Option<Self>
pub fn parent(&self) -> Option<Self>
Returns the Utf8TypedPath without its final component, if there is one.
Returns None if the path terminates in a root or prefix.
Examples
use typed_path::Utf8TypedPath;
let path = Utf8TypedPath::derive("/foo/bar");
let parent = path.parent().unwrap();
assert_eq!(parent, Utf8TypedPath::derive("/foo"));
let grand_parent = parent.parent().unwrap();
assert_eq!(grand_parent, Utf8TypedPath::derive("/"));
assert_eq!(grand_parent.parent(), None);sourcepub fn ancestors(&self) -> Utf8TypedAncestors<'a> ⓘ
pub fn ancestors(&self) -> Utf8TypedAncestors<'a> ⓘ
Produces an iterator over Utf8TypedPath and its ancestors.
The iterator will yield the Utf8TypedPath that is returned if the parent method is used
zero or more times. That means, the iterator will yield &self, &self.parent().unwrap(),
&self.parent().unwrap().parent().unwrap() and so on. If the parent method returns
None, the iterator will do likewise. The iterator will always yield at least one value,
namely &self.
Examples
use typed_path::Utf8TypedPath;
let mut ancestors = Utf8TypedPath::derive("/foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(Utf8TypedPath::derive("/foo/bar")));
assert_eq!(ancestors.next(), Some(Utf8TypedPath::derive("/foo")));
assert_eq!(ancestors.next(), Some(Utf8TypedPath::derive("/")));
assert_eq!(ancestors.next(), None);
let mut ancestors = Utf8TypedPath::derive("../foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(Utf8TypedPath::derive("../foo/bar")));
assert_eq!(ancestors.next(), Some(Utf8TypedPath::derive("../foo")));
assert_eq!(ancestors.next(), Some(Utf8TypedPath::derive("..")));
assert_eq!(ancestors.next(), Some(Utf8TypedPath::derive("")));
assert_eq!(ancestors.next(), None);sourcepub fn file_name(&self) -> Option<&str>
pub fn file_name(&self) -> Option<&str>
Returns the final component of the Utf8TypedPath, if there is one.
If the path is a normal file, this is the file name. If it’s the path of a directory, this is the directory name.
Returns None if the path terminates in ...
Examples
use typed_path::Utf8TypedPath;
assert_eq!(Some("bin"), Utf8TypedPath::derive("/usr/bin/").file_name());
assert_eq!(Some("foo.txt"), Utf8TypedPath::derive("tmp/foo.txt").file_name());
assert_eq!(Some("foo.txt"), Utf8TypedPath::derive("foo.txt/.").file_name());
assert_eq!(Some("foo.txt"), Utf8TypedPath::derive("foo.txt/.//").file_name());
assert_eq!(None, Utf8TypedPath::derive("foo.txt/..").file_name());
assert_eq!(None, Utf8TypedPath::derive("/").file_name());sourcepub fn strip_prefix(
&self,
base: impl AsRef<str>
) -> Result<Utf8TypedPath<'_>, StripPrefixError>
pub fn strip_prefix( &self, base: impl AsRef<str> ) -> Result<Utf8TypedPath<'_>, StripPrefixError>
Returns a path that, when joined onto base, yields self.
Difference from Path
Unlike Path::strip_prefix, this implementation only supports types that implement
AsRef<str> instead of AsRef<Path>.
Errors
If base is not a prefix of self (i.e., starts_with
returns false), returns Err.
Examples
use typed_path::{Utf8TypedPath, Utf8TypedPathBuf};
let path = Utf8TypedPath::derive("/test/haha/foo.txt");
assert_eq!(path.strip_prefix("/"), Ok(Utf8TypedPath::derive("test/haha/foo.txt")));
assert_eq!(path.strip_prefix("/test"), Ok(Utf8TypedPath::derive("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/"), Ok(Utf8TypedPath::derive("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(Utf8TypedPath::derive("")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Utf8TypedPath::derive("")));
assert!(path.strip_prefix("test").is_err());
assert!(path.strip_prefix("/haha").is_err());
let prefix = Utf8TypedPathBuf::from("/test/");
assert_eq!(path.strip_prefix(prefix), Ok(Utf8TypedPath::derive("haha/foo.txt")));sourcepub fn starts_with(&self, base: impl AsRef<str>) -> bool
pub fn starts_with(&self, base: impl AsRef<str>) -> bool
Determines whether base is a prefix of self.
Only considers whole path components to match.
Difference from Path
Unlike Path::starts_with, this implementation only supports types that implement
AsRef<str> instead of AsRef<Path>.
Examples
use typed_path::Utf8TypedPath;
let path = Utf8TypedPath::derive("/etc/passwd");
assert!(path.starts_with("/etc"));
assert!(path.starts_with("/etc/"));
assert!(path.starts_with("/etc/passwd"));
assert!(path.starts_with("/etc/passwd/")); // extra slash is okay
assert!(path.starts_with("/etc/passwd///")); // multiple extra slashes are okay
assert!(!path.starts_with("/e"));
assert!(!path.starts_with("/etc/passwd.txt"));
assert!(!Utf8TypedPath::derive("/etc/foo.rs").starts_with("/etc/foo"));sourcepub fn ends_with(&self, child: impl AsRef<str>) -> bool
pub fn ends_with(&self, child: impl AsRef<str>) -> bool
Determines whether child is a suffix of self.
Only considers whole path components to match.
Difference from Path
Unlike Path::ends_with, this implementation only supports types that implement
AsRef<str> instead of AsRef<Path>.
Examples
use typed_path::Utf8TypedPath;
let path = Utf8TypedPath::derive("/etc/resolv.conf");
assert!(path.ends_with("resolv.conf"));
assert!(path.ends_with("etc/resolv.conf"));
assert!(path.ends_with("/etc/resolv.conf"));
assert!(!path.ends_with("/resolv.conf"));
assert!(!path.ends_with("conf")); // use .extension() insteadsourcepub fn file_stem(&self) -> Option<&str>
pub fn file_stem(&self) -> Option<&str>
Extracts the stem (non-extension) portion of self.file_name.
The stem is:
None, if there is no file name;- The entire file name if there is no embedded
.; - The entire file name if the file name begins with
.and has no other.s within; - Otherwise, the portion of the file name before the final
.
Examples
use typed_path::Utf8TypedPath;
assert_eq!("foo", Utf8TypedPath::derive("foo.rs").file_stem().unwrap());
assert_eq!("foo.tar", Utf8TypedPath::derive("foo.tar.gz").file_stem().unwrap());sourcepub fn extension(&self) -> Option<&str>
pub fn extension(&self) -> Option<&str>
Extracts the extension of self.file_name, if possible.
The extension is:
None, if there is no file name;None, if there is no embedded.;None, if the file name begins with.and has no other.s within;- Otherwise, the portion of the file name after the final
.
Examples
use typed_path::Utf8TypedPath;
assert_eq!("rs", Utf8TypedPath::derive("foo.rs").extension().unwrap());
assert_eq!("gz", Utf8TypedPath::derive("foo.tar.gz").extension().unwrap());sourcepub fn normalize(&self) -> Utf8TypedPathBuf
pub fn normalize(&self) -> Utf8TypedPathBuf
Returns an owned Utf8TypedPathBuf by resolving .. and . segments.
When multiple, sequential path segment separation characters are found (e.g. / for Unix
and either \ or / on Windows), they are replaced by a single instance of the
platform-specific path segment separator (/ on Unix and \ on Windows).
Examples
use typed_path::{Utf8TypedPath, Utf8TypedPathBuf};
assert_eq!(
Utf8TypedPath::derive("foo/bar//baz/./asdf/quux/..").normalize(),
Utf8TypedPathBuf::from("foo/bar/baz/asdf"),
);When starting with a root directory, any .. segment whose parent is the root directory
will be filtered out:
use typed_path::{Utf8TypedPath, Utf8TypedPathBuf};
// NOTE: A path cannot be created on its own without a defined encoding
assert_eq!(
Utf8TypedPath::derive("/../foo").normalize(),
Utf8TypedPathBuf::from("/foo"),
);If any .. is left unresolved as the path is relative and no parent is found, it is
discarded:
use typed_path::{Utf8TypedPath, Utf8TypedPathBuf};
assert_eq!(
Utf8TypedPath::derive("../foo/..").normalize(),
Utf8TypedPathBuf::from(""),
);
// Windows prefixes also count this way, but the prefix remains
assert_eq!(
Utf8TypedPath::derive(r"C:..\foo\..").normalize(),
Utf8TypedPathBuf::from(r"C:"),
);sourcepub fn absolutize(&self) -> Result<Utf8TypedPathBuf>
pub fn absolutize(&self) -> Result<Utf8TypedPathBuf>
Converts a path to an absolute form by normalizing the path, returning a
Utf8TypedPathBuf.
In the case that the path is relative, the current working directory is prepended prior to normalizing.
Examples
use typed_path::{utils, Utf8TypedPath};
// With an absolute path, it is just normalized
let path = Utf8TypedPath::derive("/a/b/../c/./d");
assert_eq!(path.absolutize().unwrap(), Utf8TypedPath::derive("/a/c/d"));
// With a relative path, it is first joined with the current working directory
// and then normalized
let cwd = utils::current_dir().unwrap().with_unix_encoding().to_typed_path_buf();
let path = cwd.join("a/b/../c/./d");
assert_eq!(path.absolutize().unwrap(), cwd.join("a/c/d"));sourcepub fn join(&self, path: impl AsRef<str>) -> Utf8TypedPathBuf
pub fn join(&self, path: impl AsRef<str>) -> Utf8TypedPathBuf
Creates an owned Utf8TypedPathBuf with path adjoined to self.
See Utf8TypedPathBuf::push for more details on what it means to adjoin a path.
Difference from Path
Unlike Path::join, this implementation only supports types that implement
AsRef<str> instead of AsRef<Path>.
Examples
use typed_path::{Utf8TypedPath, Utf8TypedPathBuf};
assert_eq!(
Utf8TypedPath::derive("/etc").join("passwd"),
Utf8TypedPathBuf::from("/etc/passwd"),
);sourcepub fn with_file_name<S: AsRef<str>>(&self, file_name: S) -> Utf8TypedPathBuf
pub fn with_file_name<S: AsRef<str>>(&self, file_name: S) -> Utf8TypedPathBuf
Creates an owned Utf8TypedPathBuf like self but with the given file name.
See Utf8TypedPathBuf::set_file_name for more details.
Examples
use typed_path::{Utf8TypedPath, Utf8TypedPathBuf};
let path = Utf8TypedPath::derive("/tmp/foo.txt");
assert_eq!(path.with_file_name("bar.txt"), Utf8TypedPathBuf::from("/tmp/bar.txt"));
let path = Utf8TypedPath::derive("/tmp");
assert_eq!(path.with_file_name("var"), Utf8TypedPathBuf::from("/var"));sourcepub fn with_extension<S: AsRef<str>>(&self, extension: S) -> Utf8TypedPathBuf
pub fn with_extension<S: AsRef<str>>(&self, extension: S) -> Utf8TypedPathBuf
Creates an owned Utf8TypedPathBuf like self but with the given extension.
See Utf8TypedPathBuf::set_extension for more details.
Examples
use typed_path::{Utf8TypedPath, Utf8TypedPathBuf};
let path = Utf8TypedPath::derive("foo.rs");
assert_eq!(path.with_extension("txt"), Utf8TypedPathBuf::from("foo.txt"));
let path = Utf8TypedPath::derive("foo.tar.gz");
assert_eq!(path.with_extension(""), Utf8TypedPathBuf::from("foo.tar"));
assert_eq!(path.with_extension("xz"), Utf8TypedPathBuf::from("foo.tar.xz"));
assert_eq!(path.with_extension("").with_extension("txt"), Utf8TypedPathBuf::from("foo.txt"));sourcepub fn components(&self) -> Utf8TypedComponents<'a> ⓘ
pub fn components(&self) -> Utf8TypedComponents<'a> ⓘ
Produces an iterator over the Utf8TypedComponents of the path.
When parsing the path, there is a small amount of normalization:
-
Repeated separators are ignored, so
a/banda//bboth haveaandbas components. -
Occurrences of
.are normalized away, except if they are at the beginning of the path. For example,a/./b,a/b/,a/b/.anda/ball haveaandbas components, but./a/bstarts with an additionalCurDircomponent. -
A trailing slash is normalized away,
/a/band/a/b/are equivalent.
Note that no other normalization takes place; in particular, a/c
and a/b/../c are distinct, to account for the possibility that b
is a symbolic link (so its parent isn’t a).
Examples
use typed_path::{Utf8TypedPath, Utf8TypedComponent};
let mut components = Utf8TypedPath::derive("/tmp/foo.txt").components();
assert!(components.next().unwrap().is_root());
assert_eq!(components.next().unwrap().as_normal_str(), Some("tmp"));
assert_eq!(components.next().unwrap().as_normal_str(), Some("foo.txt"));
assert_eq!(components.next(), None)sourcepub fn iter(&self) -> Utf8TypedIter<'a> ⓘ
pub fn iter(&self) -> Utf8TypedIter<'a> ⓘ
Produces an iterator over the path’s components viewed as str slices.
For more information about the particulars of how the path is separated
into components, see components.
Examples
use typed_path::Utf8TypedPath;
let mut it = Utf8TypedPath::derive("/tmp/foo.txt").iter();
assert_eq!(it.next(), Some(typed_path::constants::unix::SEPARATOR_STR));
assert_eq!(it.next(), Some("tmp"));
assert_eq!(it.next(), Some("foo.txt"));
assert_eq!(it.next(), None)sourcepub fn is_windows(&self) -> bool
pub fn is_windows(&self) -> bool
Returns true if this path represents a Windows path.
sourcepub fn with_unix_encoding(&self) -> Utf8TypedPathBuf
pub fn with_unix_encoding(&self) -> Utf8TypedPathBuf
Converts this Utf8TypedPath into the Unix variant of Utf8TypedPathBuf.
sourcepub fn with_windows_encoding(&self) -> Utf8TypedPathBuf
pub fn with_windows_encoding(&self) -> Utf8TypedPathBuf
Converts this Utf8TypedPath into the Unix variant of Utf8TypedPathBuf.
Trait Implementations§
source§impl AsRef<str> for Utf8TypedPath<'_>
impl AsRef<str> for Utf8TypedPath<'_>
source§impl<'a> Clone for Utf8TypedPath<'a>
impl<'a> Clone for Utf8TypedPath<'a>
source§fn clone(&self) -> Utf8TypedPath<'a>
fn clone(&self) -> Utf8TypedPath<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl<'a> Debug for Utf8TypedPath<'a>
impl<'a> Debug for Utf8TypedPath<'a>
source§impl Display for Utf8TypedPath<'_>
impl Display for Utf8TypedPath<'_>
source§impl<'a> From<&'a str> for Utf8TypedPath<'a>
impl<'a> From<&'a str> for Utf8TypedPath<'a>
source§impl<'a> PartialEq<&'a str> for Utf8TypedPath<'_>
impl<'a> PartialEq<&'a str> for Utf8TypedPath<'_>
source§impl<'a> PartialEq<Utf8TypedPath<'_>> for &'a str
impl<'a> PartialEq<Utf8TypedPath<'_>> for &'a str
source§fn eq(&self, path: &Utf8TypedPath<'_>) -> bool
fn eq(&self, path: &Utf8TypedPath<'_>) -> bool
self and other values to be equal, and is used
by ==.source§impl PartialEq<Utf8TypedPath<'_>> for Utf8TypedPathBuf
impl PartialEq<Utf8TypedPath<'_>> for Utf8TypedPathBuf
source§fn eq(&self, path: &Utf8TypedPath<'_>) -> bool
fn eq(&self, path: &Utf8TypedPath<'_>) -> bool
self and other values to be equal, and is used
by ==.source§impl PartialEq<Utf8TypedPath<'_>> for str
impl PartialEq<Utf8TypedPath<'_>> for str
source§fn eq(&self, path: &Utf8TypedPath<'_>) -> bool
fn eq(&self, path: &Utf8TypedPath<'_>) -> bool
self and other values to be equal, and is used
by ==.source§impl PartialEq<Utf8TypedPathBuf> for Utf8TypedPath<'_>
impl PartialEq<Utf8TypedPathBuf> for Utf8TypedPath<'_>
source§fn eq(&self, path: &Utf8TypedPathBuf) -> bool
fn eq(&self, path: &Utf8TypedPathBuf) -> bool
self and other values to be equal, and is used
by ==.source§impl PartialEq<str> for Utf8TypedPath<'_>
impl PartialEq<str> for Utf8TypedPath<'_>
source§impl<'a> PartialEq for Utf8TypedPath<'a>
impl<'a> PartialEq for Utf8TypedPath<'a>
source§fn eq(&self, other: &Utf8TypedPath<'a>) -> bool
fn eq(&self, other: &Utf8TypedPath<'a>) -> bool
self and other values to be equal, and is used
by ==.