pub struct SPath { /* private fields */ }
Expand description
An SPath is a posix normalized Path using camino Utf8PathBuf as strogate. It can be constructed from a String, Path, io::DirEntry, or walkdir::DirEntry
- It’s Posix normalized
/
, all redundant//
and/./
are removed - Garanteed to be UTF8
Implementations§
Source§impl SPath
Constructors that guarantee the SPath contract described in the struct
impl SPath
Constructors that guarantee the SPath contract described in the struct
Sourcepub fn new(path: impl Into<Utf8PathBuf>) -> Self
pub fn new(path: impl Into<Utf8PathBuf>) -> Self
Constructor for SPath accepting anything that implements Into
Sourcepub fn from_std_path_buf(path_buf: PathBuf) -> Result<Self>
pub fn from_std_path_buf(path_buf: PathBuf) -> Result<Self>
Constructor from standard PathBuf.
Sourcepub fn from_std_path(path: impl AsRef<Path>) -> Result<Self>
pub fn from_std_path(path: impl AsRef<Path>) -> Result<Self>
Constructor from standard Path and all impl AsRef
Sourcepub fn from_walkdir_entry(wd_entry: DirEntry) -> Result<Self>
pub fn from_walkdir_entry(wd_entry: DirEntry) -> Result<Self>
Constructor from walkdir::DirEntry
Sourcepub fn from_std_path_ok(path: impl AsRef<Path>) -> Option<Self>
pub fn from_std_path_ok(path: impl AsRef<Path>) -> Option<Self>
Constructor for anything that implements AsRef
Returns Option
Sourcepub fn from_std_path_buf_ok(path_buf: PathBuf) -> Option<Self>
pub fn from_std_path_buf_ok(path_buf: PathBuf) -> Option<Self>
Constructed from PathBuf returns an Option, none if validation fails. Useful for filter_map.
Sourcepub fn from_fs_entry_ok(fs_entry: DirEntry) -> Option<Self>
pub fn from_fs_entry_ok(fs_entry: DirEntry) -> Option<Self>
Constructor from fs::DirEntry returning an Option, none if validation fails. Useful for filter_map.
Sourcepub fn from_walkdir_entry_ok(wd_entry: DirEntry) -> Option<Self>
pub fn from_walkdir_entry_ok(wd_entry: DirEntry) -> Option<Self>
Constructor from walkdir::DirEntry returning an Option, none if validation fails. Useful for filter_map.
Source§impl SPath
Public getters
impl SPath
Public getters
Sourcepub fn to_str(&self) -> &str
👎Deprecated: use as_str()
pub fn to_str(&self) -> &str
Returns the &str of the path.
NOTE: We know that this must be Some() since the SPath constructor guarantees that the path.as_str() is valid.
Sourcepub fn file_name(&self) -> Option<&str>
pub fn file_name(&self) -> Option<&str>
Returns the Option<&str> representation of the path.file_name()
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Returns the &str representation of the path.file_name()
Note: If no file name will be an empty string
Sourcepub fn parent_name(&self) -> &str
pub fn parent_name(&self) -> &str
Returns the parent name, and empty static &str if no present
Sourcepub fn file_stem(&self) -> Option<&str>
pub fn file_stem(&self) -> Option<&str>
Returns the Option<&str> representation of the file_stem()
Note: if the OsStr
cannot be made into utf8 will be None
Sourcepub fn stem(&self) -> &str
pub fn stem(&self) -> &str
Returns the &str representation of the file_name()
Note: If no stem, will be an empty string
Sourcepub fn extension(&self) -> Option<&str>
pub fn extension(&self) -> Option<&str>
Returns the Option<&str> representation of the extension().
NOTE: This should never be a non-UTF-8 string as the path was validated during SPath construction.
Sourcepub fn is_absolute(&self) -> bool
pub fn is_absolute(&self) -> bool
Returns true if the internal path is absolute.
Sourcepub fn is_relative(&self) -> bool
pub fn is_relative(&self) -> bool
Returns true if the internal path is relative.
Source§impl SPath
Meta
impl SPath
Meta
Sourcepub fn meta(&self) -> Result<SMeta>
pub fn meta(&self) -> Result<SMeta>
Get a Simple Metadata structure SMeta
with
created_epoch_us, modified_epoch_us, and size (all i64)
(size will be ‘0’ for any none file)
Sourcepub fn modified(&self) -> Result<SystemTime>
👎Deprecated: use spath.meta()
pub fn modified(&self) -> Result<SystemTime>
Returns the path.metadata modified SystemTime
Sourcepub fn modified_us(&self) -> Result<i64>
👎Deprecated: use spath.meta()
pub fn modified_us(&self) -> Result<i64>
Returns the epoch duration in microseconds.
Note: The maximum UTC date would be approximately 2262-04-11
.
Thus, for all intents and purposes, it is far enough to not worry.
Source§impl SPath
Transformers
impl SPath
Transformers
Sourcepub fn canonicalize(&self) -> Result<SPath>
pub fn canonicalize(&self) -> Result<SPath>
This perform a OS Canonicalization.
Sourcepub fn collapse(&self) -> SPath
pub fn collapse(&self) -> SPath
Collapse a path without performing I/O.
All redundant separator and up-level references are collapsed.
However, this does not resolve links.
Sourcepub fn into_collapsed(self) -> SPath
pub fn into_collapsed(self) -> SPath
Same as [collapse
] but consume and create a new SPath only if needed
Sourcepub fn is_collapsed(&self) -> bool
pub fn is_collapsed(&self) -> bool
Return true
if the path is collapsed.
§Quirk
If the path does not start with ./
but contains ./
in the middle,
then this function might returns true
.
Sourcepub fn append_suffix(&self, suffix: &str) -> SPath
pub fn append_suffix(&self, suffix: &str) -> SPath
Returns a new SPath with the given suffix appended to the filename (after the eventual extension)
Use [join
] to join path segments.
Example:
foo.rs
+_backup
→foo.rs_backup
Sourcepub fn join(&self, leaf_path: impl Into<Utf8PathBuf>) -> SPath
pub fn join(&self, leaf_path: impl Into<Utf8PathBuf>) -> SPath
Joins the provided path with the current path and returns an SPath.
Sourcepub fn join_std_path(&self, leaf_path: impl AsRef<Path>) -> Result<SPath>
pub fn join_std_path(&self, leaf_path: impl AsRef<Path>) -> Result<SPath>
Joins a standard Path to the path of this SPath.
Sourcepub fn new_sibling(&self, leaf_path: impl AsRef<str>) -> SPath
pub fn new_sibling(&self, leaf_path: impl AsRef<str>) -> SPath
Creates a new sibling SPath with the given leaf_path.
Sourcepub fn new_sibling_std_path(&self, leaf_path: impl AsRef<Path>) -> Result<SPath>
pub fn new_sibling_std_path(&self, leaf_path: impl AsRef<Path>) -> Result<SPath>
Creates a new sibling SPath with the given standard path.
pub fn diff(&self, base: impl AsRef<Utf8Path>) -> Option<SPath>
pub fn try_diff(&self, base: impl AsRef<Utf8Path>) -> Result<SPath>
pub fn replace_prefix( &self, base: impl AsRef<str>, with: impl AsRef<str>, ) -> SPath
pub fn into_replace_prefix( self, base: impl AsRef<str>, with: impl AsRef<str>, ) -> SPath
Source§impl SPath
Path/UTF8Path/Camino passthrough
impl SPath
Path/UTF8Path/Camino passthrough
pub fn as_std_path(&self) -> &Path
Sourcepub fn starts_with(&self, base: impl AsRef<Path>) -> bool
pub fn starts_with(&self, base: impl AsRef<Path>) -> bool
Determines whether base
is a prefix of self
.
Only considers whole path components to match.
§Examples
use camino::Utf8Path;
let path = Utf8Path::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!(!Utf8Path::new("/etc/foo.rs").starts_with("/etc/foo"));
Source§impl SPath
Extensions
impl SPath
Extensions
Sourcepub fn into_ensure_extension(self, ext: &str) -> Self
pub fn into_ensure_extension(self, ext: &str) -> Self
Consumes the SPath and returns one with the given extension ensured:
- Sets the extension if not already equal.
- Returns self if the extension is already present.
§Params
ext
e.g.html
(not . prefixed)
Sourcepub fn ensure_extension(&self, ext: &str) -> Self
pub fn ensure_extension(&self, ext: &str) -> Self
Returns a new SPath with the given extension ensured.
- Since this takes a reference, it will return a Clone no matter what.
- Use [
into_ensure_extension
] to consume and create a new SPath only if needed.
Delegates to into_ensure_extension
.
§Params
ext
e.g.html
(not . prefixed)
Sourcepub fn append_extension(&self, ext: &str) -> Self
pub fn append_extension(&self, ext: &str) -> Self
Appends the extension, even if one already exists or is the same.
§Params
ext
e.g.html
(not . prefixed)