pub struct SystemPath(/* private fields */);Expand description
Implementations§
Source§impl SystemPath
impl SystemPath
pub fn new(path: &(impl AsRef<Utf8Path> + ?Sized)) -> &Self
Sourcepub fn simplified(&self) -> &SystemPath
pub fn simplified(&self) -> &SystemPath
Takes any path, and when possible, converts Windows UNC paths to regular paths. If the path can’t be converted, it’s returned unmodified.
On non-Windows this is no-op.
\\?\C:\Windows will be converted to C:\Windows,
but \\?\C:\COM will be left as-is (due to a reserved filename).
Use this to pass arbitrary paths to programs that may not be UNC-aware.
It’s generally safe to pass UNC paths to legacy programs, because these paths contain a reserved prefix, so will gracefully fail if used with legacy APIs that don’t support UNC.
This function does not perform any I/O.
Currently paths with unpaired surrogates aren’t converted even if they
could be, due to limitations of Rust’s OsStr API.
To check if a path remained as UNC, use path.as_os_str().as_encoded_bytes().starts_with(b"\\\\").
Sourcepub fn is_absolute(&self) -> bool
pub fn is_absolute(&self) -> bool
Returns true if the SystemPath is absolute, i.e., if it is independent of
the current directory.
-
On Unix, a path is absolute if it starts with the root, so
is_absoluteandhas_rootare equivalent. -
On Windows, a path is absolute if it has a prefix and starts with the root:
c:\windowsis absolute, whilec:tempand\tempare not.
§Examples
use ruff_db::system::SystemPath;
assert!(!SystemPath::new("foo.txt").is_absolute());Sourcepub fn extension(&self) -> Option<&str>
pub fn extension(&self) -> Option<&str>
Extracts the file extension, 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 ruff_db::system::SystemPath;
assert_eq!("rs", SystemPath::new("foo.rs").extension().unwrap());
assert_eq!("gz", SystemPath::new("foo.tar.gz").extension().unwrap());See Path::extension for more details.
Sourcepub fn starts_with(&self, base: impl AsRef<SystemPath>) -> bool
pub fn starts_with(&self, base: impl AsRef<SystemPath>) -> bool
Determines whether base is a prefix of self.
Only considers whole path components to match.
§Examples
use ruff_db::system::SystemPath;
let path = SystemPath::new("/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!(!SystemPath::new("/etc/foo.rs").starts_with("/etc/foo"));Sourcepub fn ends_with(&self, child: impl AsRef<SystemPath>) -> bool
pub fn ends_with(&self, child: impl AsRef<SystemPath>) -> bool
Determines whether child is a suffix of self.
Only considers whole path components to match.
§Examples
use ruff_db::system::SystemPath;
let path = SystemPath::new("/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 parent(&self) -> Option<&SystemPath>
pub fn parent(&self) -> Option<&SystemPath>
Returns the FileSystemPath without its final component, if there is one.
Returns None if the path terminates in a root or prefix.
§Examples
use ruff_db::system::SystemPath;
let path = SystemPath::new("/foo/bar");
let parent = path.parent().unwrap();
assert_eq!(parent, SystemPath::new("/foo"));
let grand_parent = parent.parent().unwrap();
assert_eq!(grand_parent, SystemPath::new("/"));
assert_eq!(grand_parent.parent(), None);Sourcepub fn ancestors(&self) -> impl Iterator<Item = &SystemPath>
pub fn ancestors(&self) -> impl Iterator<Item = &SystemPath>
Produces an iterator over SystemPath and its ancestors.
The iterator will yield the SystemPath 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 ruff_db::system::SystemPath;
let mut ancestors = SystemPath::new("/foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(SystemPath::new("/foo/bar")));
assert_eq!(ancestors.next(), Some(SystemPath::new("/foo")));
assert_eq!(ancestors.next(), Some(SystemPath::new("/")));
assert_eq!(ancestors.next(), None);
let mut ancestors = SystemPath::new("../foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(SystemPath::new("../foo/bar")));
assert_eq!(ancestors.next(), Some(SystemPath::new("../foo")));
assert_eq!(ancestors.next(), Some(SystemPath::new("..")));
assert_eq!(ancestors.next(), Some(SystemPath::new("")));
assert_eq!(ancestors.next(), None);Sourcepub fn components(&self) -> Utf8Components<'_> ⓘ
pub fn components(&self) -> Utf8Components<'_> ⓘ
Produces an iterator over the camino::Utf8Components 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 camino::{Utf8Component};
use ruff_db::system::SystemPath;
let mut components = SystemPath::new("/tmp/foo.txt").components();
assert_eq!(components.next(), Some(Utf8Component::RootDir));
assert_eq!(components.next(), Some(Utf8Component::Normal("tmp")));
assert_eq!(components.next(), Some(Utf8Component::Normal("foo.txt")));
assert_eq!(components.next(), None)Sourcepub fn file_name(&self) -> Option<&str>
pub fn file_name(&self) -> Option<&str>
Returns the final component of the FileSystemPath, 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 camino::Utf8Path;
use ruff_db::system::SystemPath;
assert_eq!(Some("bin"), SystemPath::new("/usr/bin/").file_name());
assert_eq!(Some("foo.txt"), SystemPath::new("tmp/foo.txt").file_name());
assert_eq!(Some("foo.txt"), SystemPath::new("foo.txt/.").file_name());
assert_eq!(Some("foo.txt"), SystemPath::new("foo.txt/.//").file_name());
assert_eq!(None, SystemPath::new("foo.txt/..").file_name());
assert_eq!(None, SystemPath::new("/").file_name());Sourcepub 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 ruff_db::system::SystemPath;
assert_eq!("foo", SystemPath::new("foo.rs").file_stem().unwrap());
assert_eq!("foo.tar", SystemPath::new("foo.tar.gz").file_stem().unwrap());Sourcepub fn strip_prefix(
&self,
base: impl AsRef<SystemPath>,
) -> Result<&SystemPath, StripPrefixError>
pub fn strip_prefix( &self, base: impl AsRef<SystemPath>, ) -> Result<&SystemPath, StripPrefixError>
Returns a 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 ruff_db::system::{SystemPath, SystemPathBuf};
let path = SystemPath::new("/test/haha/foo.txt");
assert_eq!(path.strip_prefix("/"), Ok(SystemPath::new("test/haha/foo.txt")));
assert_eq!(path.strip_prefix("/test"), Ok(SystemPath::new("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/"), Ok(SystemPath::new("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(SystemPath::new("")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(SystemPath::new("")));
assert!(path.strip_prefix("test").is_err());
assert!(path.strip_prefix("/haha").is_err());
let prefix = SystemPathBuf::from("/test/");
assert_eq!(path.strip_prefix(prefix), Ok(SystemPath::new("haha/foo.txt")));Sourcepub fn join(&self, path: impl AsRef<SystemPath>) -> SystemPathBuf
pub fn join(&self, path: impl AsRef<SystemPath>) -> SystemPathBuf
Creates an owned SystemPathBuf with path adjoined to self.
See std::path::PathBuf::push for more details on what it means to adjoin a path.
§Examples
use ruff_db::system::{SystemPath, SystemPathBuf};
assert_eq!(SystemPath::new("/etc").join("passwd"), SystemPathBuf::from("/etc/passwd"));Sourcepub fn with_extension(&self, extension: &str) -> SystemPathBuf
pub fn with_extension(&self, extension: &str) -> SystemPathBuf
Creates an owned SystemPathBuf like self but with the given extension.
See std::path::PathBuf::set_extension for more details.
§Examples
use ruff_db::system::{SystemPath, SystemPathBuf};
let path = SystemPath::new("foo.rs");
assert_eq!(path.with_extension("txt"), SystemPathBuf::from("foo.txt"));
let path = SystemPath::new("foo.tar.gz");
assert_eq!(path.with_extension(""), SystemPathBuf::from("foo.tar"));
assert_eq!(path.with_extension("xz"), SystemPathBuf::from("foo.tar.xz"));
assert_eq!(path.with_extension("").with_extension("txt"), SystemPathBuf::from("foo.txt"));Sourcepub fn to_path_buf(&self) -> SystemPathBuf
pub fn to_path_buf(&self) -> SystemPathBuf
Converts the path to an owned SystemPathBuf.
Sourcepub fn as_std_path(&self) -> &Path
pub fn as_std_path(&self) -> &Path
Returns the std path for the file.
Sourcepub fn as_utf8_path(&self) -> &Utf8Path
pub fn as_utf8_path(&self) -> &Utf8Path
Returns the Utf8Path for the file.
pub fn from_std_path(path: &Path) -> Option<&SystemPath>
Sourcepub fn absolute(
path: impl AsRef<SystemPath>,
cwd: impl AsRef<SystemPath>,
) -> SystemPathBuf
pub fn absolute( path: impl AsRef<SystemPath>, cwd: impl AsRef<SystemPath>, ) -> SystemPathBuf
Makes a path absolute and normalizes it without accessing the file system.
Adapted from cargo
§Examples
§Posix paths
use ruff_db::system::{SystemPath, SystemPathBuf};
// Relative to absolute
let absolute = SystemPath::absolute("foo/./bar", "/tmp");
assert_eq!(absolute, SystemPathBuf::from("/tmp/foo/bar"));
// Path's going past the root are normalized to the root
let absolute = SystemPath::absolute("../../../", "/tmp");
assert_eq!(absolute, SystemPathBuf::from("/"));
// Absolute to absolute
let absolute = SystemPath::absolute("/foo//test/.././bar.rs", "/tmp");
assert_eq!(absolute, SystemPathBuf::from("/foo/bar.rs"));§Windows paths
use ruff_db::system::{SystemPath, SystemPathBuf};
// Relative to absolute
let absolute = SystemPath::absolute(r"foo\.\bar", r"C:\tmp");
assert_eq!(absolute, SystemPathBuf::from(r"C:\tmp\foo\bar"));
// Path's going past the root are normalized to the root
let absolute = SystemPath::absolute(r"..\..\..\", r"C:\tmp");
assert_eq!(absolute, SystemPathBuf::from(r"C:\"));
// Absolute to absolute
let absolute = SystemPath::absolute(r"C:\foo//test\..\./bar.rs", r"C:\tmp");
assert_eq!(absolute, SystemPathBuf::from(r"C:\foo\bar.rs"));Trait Implementations§
Source§impl AsRef<Path> for SystemPath
impl AsRef<Path> for SystemPath
Source§impl AsRef<SystemPath> for SystemPathBuf
impl AsRef<SystemPath> for SystemPathBuf
Source§fn as_ref(&self) -> &SystemPath
fn as_ref(&self) -> &SystemPath
Source§impl AsRef<SystemPath> for SystemPath
impl AsRef<SystemPath> for SystemPath
Source§fn as_ref(&self) -> &SystemPath
fn as_ref(&self) -> &SystemPath
Source§impl AsRef<SystemPath> for Utf8Path
impl AsRef<SystemPath> for Utf8Path
Source§fn as_ref(&self) -> &SystemPath
fn as_ref(&self) -> &SystemPath
Source§impl AsRef<SystemPath> for Utf8PathBuf
impl AsRef<SystemPath> for Utf8PathBuf
Source§fn as_ref(&self) -> &SystemPath
fn as_ref(&self) -> &SystemPath
Source§impl AsRef<SystemPath> for Utf8Component<'_>
impl AsRef<SystemPath> for Utf8Component<'_>
Source§fn as_ref(&self) -> &SystemPath
fn as_ref(&self) -> &SystemPath
Source§impl AsRef<SystemPath> for str
impl AsRef<SystemPath> for str
Source§fn as_ref(&self) -> &SystemPath
fn as_ref(&self) -> &SystemPath
Source§impl AsRef<SystemPath> for String
impl AsRef<SystemPath> for String
Source§fn as_ref(&self) -> &SystemPath
fn as_ref(&self) -> &SystemPath
Source§impl Borrow<SystemPath> for SystemPathBuf
impl Borrow<SystemPath> for SystemPathBuf
Source§fn borrow(&self) -> &SystemPath
fn borrow(&self) -> &SystemPath
Source§impl Clone for Box<SystemPath>
impl Clone for Box<SystemPath>
Source§impl Debug for SystemPath
impl Debug for SystemPath
Source§impl Display for SystemPath
impl Display for SystemPath
impl Eq for SystemPath
Source§impl From<&SystemPath> for FilePath
impl From<&SystemPath> for FilePath
Source§fn from(value: &SystemPath) -> Self
fn from(value: &SystemPath) -> Self
Source§impl From<&SystemPath> for Box<SystemPath>
impl From<&SystemPath> for Box<SystemPath>
Source§fn from(path: &SystemPath) -> Self
fn from(path: &SystemPath) -> Self
Source§impl From<SystemPathBuf> for Box<SystemPath>
impl From<SystemPathBuf> for Box<SystemPath>
Source§fn from(path: SystemPathBuf) -> Self
fn from(path: SystemPathBuf) -> Self
Source§impl GetSize for Box<SystemPath>
impl GetSize for Box<SystemPath>
Source§fn get_heap_size_with_tracker<T: GetSizeTracker>(
&self,
tracker: T,
) -> (usize, T)
fn get_heap_size_with_tracker<T: GetSizeTracker>( &self, tracker: T, ) -> (usize, T)
tracker. Read moreSource§fn get_stack_size() -> usize
fn get_stack_size() -> usize
Source§fn get_heap_size(&self) -> usize
fn get_heap_size(&self) -> usize
Source§fn get_size_with_tracker<T>(&self, tracker: T) -> (usize, T)where
T: GetSizeTracker,
fn get_size_with_tracker<T>(&self, tracker: T) -> (usize, T)where
T: GetSizeTracker,
tracker. Read moreSource§impl Hash for SystemPath
impl Hash for SystemPath
Source§impl Ord for SystemPath
impl Ord for SystemPath
Source§impl PartialEq for SystemPath
impl PartialEq for SystemPath
Source§impl PartialEq<FilePath> for SystemPath
impl PartialEq<FilePath> for SystemPath
Source§impl PartialEq<SystemPath> for FilePath
impl PartialEq<SystemPath> for FilePath
Source§impl PartialOrd for SystemPath
impl PartialOrd for SystemPath
impl StructuralPartialEq for SystemPath
Source§impl ToOwned for SystemPath
impl ToOwned for SystemPath
Source§type Owned = SystemPathBuf
type Owned = SystemPathBuf
Source§fn to_owned(&self) -> Self::Owned
fn to_owned(&self) -> Self::Owned
1.63.0 · Source§fn clone_into(&self, target: &mut Self::Owned)
fn clone_into(&self, target: &mut Self::Owned)
Auto Trait Implementations§
impl !Sized for SystemPath
impl Freeze for SystemPath
impl RefUnwindSafe for SystemPath
impl Send for SystemPath
impl Sync for SystemPath
impl Unpin for SystemPath
impl UnsafeUnpin for SystemPath
impl UnwindSafe for SystemPath
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<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§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.