pub struct SystemPathBuf(/* private fields */);Expand description
Implementations§
Source§impl SystemPathBuf
impl SystemPathBuf
pub fn new() -> Self
pub fn from_utf8_path_buf(path: Utf8PathBuf) -> Self
pub fn from_path_buf(path: PathBuf) -> Result<Self, PathBuf>
Sourcepub fn from_path_buf_lossy(path: PathBuf) -> Self
pub fn from_path_buf_lossy(path: PathBuf) -> Self
Try to convert from path directly, falling back to the lossy string representation on
error.
Sourcepub fn push(&mut self, path: impl AsRef<SystemPath>)
pub fn push(&mut self, path: impl AsRef<SystemPath>)
Extends self with path.
If path is absolute, it replaces the current path.
On Windows:
- if
pathhas a root but no prefix (e.g.,\windows), it replaces everything except for the prefix (if any) ofself. - if
pathhas a prefix but no root, it replacesself.
§Examples
Pushing a relative path extends the existing path:
use ruff_db::system::SystemPathBuf;
let mut path = SystemPathBuf::from("/tmp");
path.push("file.bk");
assert_eq!(path, SystemPathBuf::from("/tmp/file.bk"));Pushing an absolute path replaces the existing path:
use ruff_db::system::SystemPathBuf;
let mut path = SystemPathBuf::from("/tmp");
path.push("/etc");
assert_eq!(path, SystemPathBuf::from("/etc"));pub fn into_utf8_path_buf(self) -> Utf8PathBuf
pub fn into_std_path_buf(self) -> PathBuf
pub fn into_string(self) -> String
pub fn as_path(&self) -> &SystemPath
Methods from Deref<Target = SystemPath>§
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.
Trait Implementations§
Source§impl AsRef<Path> for SystemPathBuf
impl AsRef<Path> for SystemPathBuf
Source§impl AsRef<SystemPath> for SystemPathBuf
impl AsRef<SystemPath> for SystemPathBuf
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 SystemPathBuf
impl Clone for SystemPathBuf
Source§fn clone(&self) -> SystemPathBuf
fn clone(&self) -> SystemPathBuf
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SystemPathBuf
impl Debug for SystemPathBuf
Source§impl Default for SystemPathBuf
impl Default for SystemPathBuf
Source§impl Deref for SystemPathBuf
impl Deref for SystemPathBuf
Source§impl Display for SystemPathBuf
impl Display for SystemPathBuf
impl Eq for SystemPathBuf
Source§impl<P: AsRef<SystemPath>> Extend<P> for SystemPathBuf
impl<P: AsRef<SystemPath>> Extend<P> for SystemPathBuf
Source§fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl From<&str> for SystemPathBuf
impl From<&str> for SystemPathBuf
Source§impl From<String> for SystemPathBuf
impl From<String> for SystemPathBuf
Source§impl From<SystemPathBuf> for FilePath
impl From<SystemPathBuf> for FilePath
Source§fn from(value: SystemPathBuf) -> Self
fn from(value: SystemPathBuf) -> 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<P: AsRef<SystemPath>> FromIterator<P> for SystemPathBuf
impl<P: AsRef<SystemPath>> FromIterator<P> for SystemPathBuf
Source§fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> Self
Source§impl GetSize for SystemPathBuf
impl GetSize for SystemPathBuf
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 SystemPathBuf
impl Hash for SystemPathBuf
Source§impl Ord for SystemPathBuf
impl Ord for SystemPathBuf
Source§fn cmp(&self, other: &SystemPathBuf) -> Ordering
fn cmp(&self, other: &SystemPathBuf) -> Ordering
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for SystemPathBuf
impl PartialEq for SystemPathBuf
Source§impl PartialEq<FilePath> for SystemPathBuf
impl PartialEq<FilePath> for SystemPathBuf
Source§impl PartialEq<SystemPathBuf> for FilePath
impl PartialEq<SystemPathBuf> for FilePath
Source§impl PartialOrd for SystemPathBuf
impl PartialOrd for SystemPathBuf
impl StructuralPartialEq for SystemPathBuf
Auto Trait Implementations§
impl Freeze for SystemPathBuf
impl RefUnwindSafe for SystemPathBuf
impl Send for SystemPathBuf
impl Sync for SystemPathBuf
impl Unpin for SystemPathBuf
impl UnsafeUnpin for SystemPathBuf
impl UnwindSafe for SystemPathBuf
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§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> HashEqLike<&T> for T
impl<T> HashEqLike<&T> for T
Source§impl<T> HashEqLike<Cow<'_, T>> for T
impl<T> HashEqLike<Cow<'_, T>> for T
Source§impl<T> HashEqLike<T> for T
impl<T> HashEqLike<T> for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoDiagnosticMessage for Twhere
T: Display,
impl<T> IntoDiagnosticMessage for Twhere
T: Display,
fn into_diagnostic_message(self) -> DiagnosticMessage
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> ToCharString for Twhere
T: Display,
impl<T> ToCharString for Twhere
T: Display,
Source§fn try_to_char_string(&self) -> Result<CharString, ToCharStringError>
fn try_to_char_string(&self) -> Result<CharString, ToCharStringError>
CharString. Read moreSource§fn to_char_string(&self) -> CharString
fn to_char_string(&self) -> CharString
CharString. Read moreSource§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more