pub struct CompilerPath {
pub kind: PathKind,
pub path: RelativePathBuf,
pub expanded_path: PathBuf,
}
Fields§
§kind: PathKind
§path: RelativePathBuf
§expanded_path: PathBuf
Implementations§
Methods from Deref<Target = RelativePathBuf>§
Sourcepub fn push<P>(&mut self, path: P)where
P: AsRef<RelativePath>,
pub fn push<P>(&mut self, path: P)where
P: AsRef<RelativePath>,
Extends self
with path
.
§Examples
use relative_path::RelativePathBuf;
let mut path = RelativePathBuf::new();
path.push("foo");
path.push("bar");
assert_eq!("foo/bar", path);
let mut path = RelativePathBuf::new();
path.push("foo");
path.push("/bar");
assert_eq!("foo/bar", path);
Sourcepub fn set_file_name<S>(&mut self, file_name: S)
pub fn set_file_name<S>(&mut self, file_name: S)
Updates file_name
to file_name
.
If file_name
was None
, this is equivalent to pushing
file_name
.
Otherwise it is equivalent to calling pop
and then pushing
file_name
. The new path will be a sibling of the original path. (That
is, it will have the same parent.)
§Examples
use relative_path::RelativePathBuf;
let mut buf = RelativePathBuf::from("");
assert!(buf.file_name() == None);
buf.set_file_name("bar");
assert_eq!(RelativePathBuf::from("bar"), buf);
assert!(buf.file_name().is_some());
buf.set_file_name("baz.txt");
assert_eq!(RelativePathBuf::from("baz.txt"), buf);
buf.push("bar");
assert!(buf.file_name().is_some());
buf.set_file_name("bar.txt");
assert_eq!(RelativePathBuf::from("baz.txt/bar.txt"), buf);
Sourcepub fn set_extension<S>(&mut self, extension: S) -> bool
pub fn set_extension<S>(&mut self, extension: S) -> bool
Updates extension
to extension
.
Returns false
and does nothing if
file_name
is None
, returns true
and
updates the extension otherwise.
If extension
is None
, the extension is added; otherwise it is
replaced.
§Examples
use relative_path::{RelativePath, RelativePathBuf};
let mut p = RelativePathBuf::from("feel/the");
p.set_extension("force");
assert_eq!(RelativePath::new("feel/the.force"), p);
p.set_extension("dark_side");
assert_eq!(RelativePath::new("feel/the.dark_side"), p);
assert!(p.pop());
p.set_extension("nothing");
assert_eq!(RelativePath::new("feel.nothing"), p);
Sourcepub fn pop(&mut self) -> bool
pub fn pop(&mut self) -> bool
Truncates self
to parent
.
§Examples
use relative_path::{RelativePath, RelativePathBuf};
let mut p = RelativePathBuf::from("test/test.rs");
assert_eq!(true, p.pop());
assert_eq!(RelativePath::new("test"), p);
assert_eq!(true, p.pop());
assert_eq!(RelativePath::new(""), p);
assert_eq!(false, p.pop());
assert_eq!(RelativePath::new(""), p);
Sourcepub fn as_relative_path(&self) -> &RelativePath
pub fn as_relative_path(&self) -> &RelativePath
Coerce to a RelativePath
slice.
Methods from Deref<Target = RelativePath>§
Sourcepub fn display(&self) -> Display<'_>
👎Deprecated: RelativePath implements std::fmt::Display directly
pub fn display(&self) -> Display<'_>
Sourcepub fn join<P>(&self, path: P) -> RelativePathBufwhere
P: AsRef<RelativePath>,
pub fn join<P>(&self, path: P) -> RelativePathBufwhere
P: AsRef<RelativePath>,
Creates an owned RelativePathBuf
with path adjoined to self.
§Examples
use relative_path::RelativePath;
let path = RelativePath::new("foo/bar");
assert_eq!("foo/bar/baz", path.join("baz"));
Sourcepub fn components(&self) -> Components<'_>
pub fn components(&self) -> Components<'_>
Iterate over all components in this relative path.
§Examples
use relative_path::{Component, RelativePath};
let path = RelativePath::new("foo/bar/baz");
let mut it = path.components();
assert_eq!(Some(Component::Normal("foo")), it.next());
assert_eq!(Some(Component::Normal("bar")), it.next());
assert_eq!(Some(Component::Normal("baz")), it.next());
assert_eq!(None, it.next());
Sourcepub fn iter(&self) -> Iter<'_>
pub fn iter(&self) -> Iter<'_>
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 relative_path::RelativePath;
let mut it = RelativePath::new("/tmp/foo.txt").iter();
assert_eq!(it.next(), Some("tmp"));
assert_eq!(it.next(), Some("foo.txt"));
assert_eq!(it.next(), None)
Sourcepub fn to_relative_path_buf(&self) -> RelativePathBuf
pub fn to_relative_path_buf(&self) -> RelativePathBuf
Convert to an owned RelativePathBuf
.
Sourcepub fn to_path<P>(&self, base: P) -> PathBuf
pub fn to_path<P>(&self, base: P) -> PathBuf
Build an owned PathBuf
relative to base
for the current relative
path.
§Examples
use relative_path::RelativePath;
use std::path::Path;
let path = RelativePath::new("foo/bar").to_path(".");
assert_eq!(Path::new("./foo/bar"), path);
let path = RelativePath::new("foo/bar").to_path("");
assert_eq!(Path::new("foo/bar"), path);
§Encoding an absolute path
Absolute paths are, in contrast to when using PathBuf::push
ignored
and will be added unchanged to the buffer.
This is to preserve the probability of a path conversion failing if the relative path contains platform-specific absolute path components.
use relative_path::RelativePath;
use std::path::Path;
if cfg!(windows) {
let path = RelativePath::new("/bar/baz").to_path("foo");
assert_eq!(Path::new("foo\\bar\\baz"), path);
let path = RelativePath::new("c:\\bar\\baz").to_path("foo");
assert_eq!(Path::new("foo\\c:\\bar\\baz"), path);
}
if cfg!(unix) {
let path = RelativePath::new("/bar/baz").to_path("foo");
assert_eq!(Path::new("foo/bar/baz"), path);
let path = RelativePath::new("c:\\bar\\baz").to_path("foo");
assert_eq!(Path::new("foo/c:\\bar\\baz"), path);
}
Sourcepub fn to_logical_path<P>(&self, base: P) -> PathBuf
pub fn to_logical_path<P>(&self, base: P) -> PathBuf
Build an owned PathBuf
relative to base
for the current relative
path.
This is similar to to_path
except that it doesn’t just
unconditionally append one path to the other, instead it performs the
following operations depending on its own components:
Component::CurDir
leaves thebase
unmodified.Component::ParentDir
removes a component frombase
usingpath::PathBuf::pop
.Component::Normal
pushes the given path component ontobase
using the same mechanism asto_path
.
Note that the exact semantics of the path operation is determined by the
corresponding PathBuf
operation. E.g. popping a component off a path
like .
will result in an empty path.
use relative_path::RelativePath;
use std::path::Path;
let path = RelativePath::new("..").to_logical_path(".");
assert_eq!(path, Path::new(""));
§Examples
use relative_path::RelativePath;
use std::path::Path;
let path = RelativePath::new("..").to_logical_path("foo/bar");
assert_eq!(path, Path::new("foo"));
§Encoding an absolute path
Behaves the same as to_path
when encoding
absolute paths.
Absolute paths are, in contrast to when using PathBuf::push
ignored
and will be added unchanged to the buffer.
This is to preserve the probability of a path conversion failing if the relative path contains platform-specific absolute path components.
use relative_path::RelativePath;
use std::path::Path;
if cfg!(windows) {
let path = RelativePath::new("/bar/baz").to_logical_path("foo");
assert_eq!(Path::new("foo\\bar\\baz"), path);
let path = RelativePath::new("c:\\bar\\baz").to_logical_path("foo");
assert_eq!(Path::new("foo\\c:\\bar\\baz"), path);
let path = RelativePath::new("foo/bar").to_logical_path("");
assert_eq!(Path::new("foo\\bar"), path);
}
if cfg!(unix) {
let path = RelativePath::new("/bar/baz").to_logical_path("foo");
assert_eq!(Path::new("foo/bar/baz"), path);
let path = RelativePath::new("c:\\bar\\baz").to_logical_path("foo");
assert_eq!(Path::new("foo/c:\\bar\\baz"), path);
let path = RelativePath::new("foo/bar").to_logical_path("");
assert_eq!(Path::new("foo/bar"), path);
}
Sourcepub fn parent(&self) -> Option<&RelativePath>
pub fn parent(&self) -> Option<&RelativePath>
Returns a relative path, without its final Component
if there is one.
§Examples
use relative_path::RelativePath;
assert_eq!(Some(RelativePath::new("foo")), RelativePath::new("foo/bar").parent());
assert_eq!(Some(RelativePath::new("")), RelativePath::new("foo").parent());
assert_eq!(None, RelativePath::new("").parent());
Sourcepub fn file_name(&self) -> Option<&str>
pub fn file_name(&self) -> Option<&str>
Returns the final component of the RelativePath
, 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 relative_path::RelativePath;
assert_eq!(Some("bin"), RelativePath::new("usr/bin/").file_name());
assert_eq!(Some("foo.txt"), RelativePath::new("tmp/foo.txt").file_name());
assert_eq!(Some("foo.txt"), RelativePath::new("tmp/foo.txt/").file_name());
assert_eq!(Some("foo.txt"), RelativePath::new("foo.txt/.").file_name());
assert_eq!(Some("foo.txt"), RelativePath::new("foo.txt/.//").file_name());
assert_eq!(None, RelativePath::new("foo.txt/..").file_name());
assert_eq!(None, RelativePath::new("/").file_name());
Sourcepub fn strip_prefix<P>(
&self,
base: P,
) -> Result<&RelativePath, StripPrefixError>where
P: AsRef<RelativePath>,
pub fn strip_prefix<P>(
&self,
base: P,
) -> Result<&RelativePath, StripPrefixError>where
P: AsRef<RelativePath>,
Returns a relative path that, when joined onto base
, yields self
.
§Errors
If base
is not a prefix of self
(i.e. starts_with
returns
false
), returns Err
.
§Examples
use relative_path::RelativePath;
let path = RelativePath::new("test/haha/foo.txt");
assert_eq!(path.strip_prefix("test"), Ok(RelativePath::new("haha/foo.txt")));
assert_eq!(path.strip_prefix("test").is_ok(), true);
assert_eq!(path.strip_prefix("haha").is_ok(), false);
Sourcepub fn starts_with<P>(&self, base: P) -> boolwhere
P: AsRef<RelativePath>,
pub fn starts_with<P>(&self, base: P) -> boolwhere
P: AsRef<RelativePath>,
Determines whether base
is a prefix of self
.
Only considers whole path components to match.
§Examples
use relative_path::RelativePath;
let path = RelativePath::new("etc/passwd");
assert!(path.starts_with("etc"));
assert!(!path.starts_with("e"));
Sourcepub fn ends_with<P>(&self, child: P) -> boolwhere
P: AsRef<RelativePath>,
pub fn ends_with<P>(&self, child: P) -> boolwhere
P: AsRef<RelativePath>,
Determines whether child
is a suffix of self
.
Only considers whole path components to match.
§Examples
use relative_path::RelativePath;
let path = RelativePath::new("etc/passwd");
assert!(path.ends_with("passwd"));
Sourcepub fn is_normalized(&self) -> bool
pub fn is_normalized(&self) -> bool
Determines whether self
is normalized.
§Examples
use relative_path::RelativePath;
// These are normalized.
assert!(RelativePath::new("").is_normalized());
assert!(RelativePath::new("baz.txt").is_normalized());
assert!(RelativePath::new("foo/bar/baz.txt").is_normalized());
assert!(RelativePath::new("..").is_normalized());
assert!(RelativePath::new("../..").is_normalized());
assert!(RelativePath::new("../../foo/bar/baz.txt").is_normalized());
// These are not normalized.
assert!(!RelativePath::new(".").is_normalized());
assert!(!RelativePath::new("./baz.txt").is_normalized());
assert!(!RelativePath::new("foo/..").is_normalized());
assert!(!RelativePath::new("foo/../baz.txt").is_normalized());
assert!(!RelativePath::new("foo/.").is_normalized());
assert!(!RelativePath::new("foo/./baz.txt").is_normalized());
assert!(!RelativePath::new("../foo/./bar/../baz.txt").is_normalized());
Sourcepub fn with_file_name<S>(&self, file_name: S) -> RelativePathBuf
pub fn with_file_name<S>(&self, file_name: S) -> RelativePathBuf
Creates an owned RelativePathBuf
like self
but with the given file
name.
See set_file_name
for more details.
§Examples
use relative_path::{RelativePath, RelativePathBuf};
let path = RelativePath::new("tmp/foo.txt");
assert_eq!(path.with_file_name("bar.txt"), RelativePathBuf::from("tmp/bar.txt"));
let path = RelativePath::new("tmp");
assert_eq!(path.with_file_name("var"), RelativePathBuf::from("var"));
Sourcepub fn file_stem(&self) -> Option<&str>
pub fn file_stem(&self) -> Option<&str>
Extracts the stem (non-extension) portion of 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 relative_path::RelativePath;
let path = RelativePath::new("foo.rs");
assert_eq!("foo", path.file_stem().unwrap());
Sourcepub fn extension(&self) -> Option<&str>
pub fn extension(&self) -> Option<&str>
Extracts the extension of 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 relative_path::RelativePath;
assert_eq!(Some("rs"), RelativePath::new("foo.rs").extension());
assert_eq!(None, RelativePath::new(".rs").extension());
assert_eq!(Some("rs"), RelativePath::new("foo.rs/.").extension());
Sourcepub fn with_extension<S>(&self, extension: S) -> RelativePathBuf
pub fn with_extension<S>(&self, extension: S) -> RelativePathBuf
Creates an owned RelativePathBuf
like self
but with the given
extension.
See set_extension
for more details.
§Examples
use relative_path::{RelativePath, RelativePathBuf};
let path = RelativePath::new("foo.rs");
assert_eq!(path.with_extension("txt"), RelativePathBuf::from("foo.txt"));
Sourcepub fn join_normalized<P>(&self, path: P) -> RelativePathBufwhere
P: AsRef<RelativePath>,
pub fn join_normalized<P>(&self, path: P) -> RelativePathBufwhere
P: AsRef<RelativePath>,
Build an owned RelativePathBuf
, joined with the given path and
normalized.
§Examples
use relative_path::RelativePath;
assert_eq!(
RelativePath::new("foo/baz.txt"),
RelativePath::new("foo/bar").join_normalized("../baz.txt").as_relative_path()
);
assert_eq!(
RelativePath::new("../foo/baz.txt"),
RelativePath::new("../foo/bar").join_normalized("../baz.txt").as_relative_path()
);
Sourcepub fn normalize(&self) -> RelativePathBuf
pub fn normalize(&self) -> RelativePathBuf
Return an owned RelativePathBuf
, with all non-normal components
moved to the beginning of the path.
This permits for a normalized representation of different relative components.
Normalization is a destructive operation if the path references an
actual filesystem path. An example of this is symlinks under unix, a
path like foo/../bar
might reference a different location other than
./bar
.
Normalization is a logical operation and does not guarantee that the constructed path corresponds to what the filesystem would do. On Linux for example symbolic links could mean that the logical path doesn’t correspond to the filesystem path.
§Examples
use relative_path::RelativePath;
assert_eq!(
"../foo/baz.txt",
RelativePath::new("../foo/./bar/../baz.txt").normalize()
);
assert_eq!(
"",
RelativePath::new(".").normalize()
);
Sourcepub fn relative<P>(&self, path: P) -> RelativePathBufwhere
P: AsRef<RelativePath>,
pub fn relative<P>(&self, path: P) -> RelativePathBufwhere
P: AsRef<RelativePath>,
Constructs a relative path from the current path, to path
.
This function will return the empty RelativePath
""
if this source
contains unnamed components like ..
that would have to be traversed to
reach the destination path
. This is necessary since we have no way of
knowing what the names of those components are when we’re building the
new relative path.
use relative_path::RelativePath;
// Here we don't know what directories `../..` refers to, so there's no
// way to construct a path back to `bar` in the current directory from
// `../..`.
let from = RelativePath::new("../../foo/relative-path");
let to = RelativePath::new("bar");
assert_eq!("", from.relative(to));
One exception to this is when two paths contains a common prefix at which point there’s no need to know what the names of those unnamed components are.
use relative_path::RelativePath;
let from = RelativePath::new("../../foo/bar");
let to = RelativePath::new("../../foo/baz");
assert_eq!("../baz", from.relative(to));
let from = RelativePath::new("../a/../../foo/bar");
let to = RelativePath::new("../../foo/baz");
assert_eq!("../baz", from.relative(to));
§Examples
use relative_path::RelativePath;
assert_eq!(
"../../e/f",
RelativePath::new("a/b/c/d").relative(RelativePath::new("a/b/e/f"))
);
assert_eq!(
"../bbb",
RelativePath::new("a/../aaa").relative(RelativePath::new("b/../bbb"))
);
let a = RelativePath::new("git/relative-path");
let b = RelativePath::new("git");
assert_eq!("relative-path", b.relative(a));
assert_eq!("..", a.relative(b));
let a = RelativePath::new("foo/bar/bap/foo.h");
let b = RelativePath::new("../arch/foo.h");
assert_eq!("../../../../../arch/foo.h", a.relative(b));
assert_eq!("", b.relative(a));
Trait Implementations§
Source§impl Clone for CompilerPath
impl Clone for CompilerPath
Source§fn clone(&self) -> CompilerPath
fn clone(&self) -> CompilerPath
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for CompilerPath
impl Debug for CompilerPath
Source§impl Default for CompilerPath
impl Default for CompilerPath
Source§fn default() -> CompilerPath
fn default() -> CompilerPath
Source§impl Deref for CompilerPath
impl Deref for CompilerPath
Source§impl DerefMut for CompilerPath
impl DerefMut for CompilerPath
Source§impl<'de> Deserialize<'de> for CompilerPath
impl<'de> Deserialize<'de> for CompilerPath
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for CompilerPath
impl Display for CompilerPath
Source§impl From<&str> for CompilerPath
impl From<&str> for CompilerPath
Source§impl From<CompilerPath> for String
impl From<CompilerPath> for String
Source§fn from(value: CompilerPath) -> String
fn from(value: CompilerPath) -> String
Source§impl From<String> for CompilerPath
impl From<String> for CompilerPath
Source§impl Ord for CompilerPath
impl Ord for CompilerPath
Source§fn cmp(&self, other: &CompilerPath) -> Ordering
fn cmp(&self, other: &CompilerPath) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for CompilerPath
impl PartialEq for CompilerPath
Source§impl PartialOrd for CompilerPath
impl PartialOrd for CompilerPath
impl Eq for CompilerPath
impl StructuralPartialEq for CompilerPath
Auto Trait Implementations§
impl Freeze for CompilerPath
impl RefUnwindSafe for CompilerPath
impl Send for CompilerPath
impl Sync for CompilerPath
impl Unpin for CompilerPath
impl UnwindSafe for CompilerPath
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.