Struct SPath

Source
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

Source

pub fn new(path: impl Into<Utf8PathBuf>) -> Self

Constructor for SPath accepting anything that implements Into. IMPORTANT: This will normalize the path (posix style

Source

pub fn from_std_path_buf(path_buf: PathBuf) -> Result<Self>

Constructor from standard PathBuf.

Source

pub fn from_std_path(path: impl AsRef<Path>) -> Result<Self>

Constructor from standard Path and all impl AsRef.

Source

pub fn from_walkdir_entry(wd_entry: DirEntry) -> Result<Self>

Constructor from walkdir::DirEntry

Source

pub fn from_std_path_ok(path: impl AsRef<Path>) -> Option<Self>

Constructor for anything that implements AsRef.

Returns Option. Useful for filter_map.

Source

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.

Source

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.

Source

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 into path

Source

pub fn into_std_path_buf(self) -> PathBuf

Consumes the SPath and returns its PathBuf.

Source

pub fn std_path(&self) -> &Path

Returns a reference to the internal std Path.

Source

pub fn path(&self) -> &Utf8Path

Returns a reference to the internal Utf8Path.

Source§

impl SPath

Public getters

Source

pub fn to_str(&self) -> &str

👎Deprecated: use as_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.

Source

pub fn as_str(&self) -> &str

Returns the &str of the path.

Source

pub fn file_name(&self) -> Option<&str>

Returns the Option<&str> representation of the path.file_name()

Source

pub fn name(&self) -> &str

Returns the &str representation of the path.file_name()

Note: If no file name will be an empty string

Source

pub fn parent_name(&self) -> &str

Returns the parent name, and empty static &str if no present

Source

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

Source

pub fn stem(&self) -> &str

Returns the &str representation of the file_name()

Note: If no stem, will be an empty string

Source

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.

Source

pub fn ext(&self) -> &str

Returns the extension or “” if no extension

Source

pub fn is_dir(&self) -> bool

Returns true if the path represents a directory.

Source

pub fn is_file(&self) -> bool

Returns true if the path represents a file.

Source

pub fn exists(&self) -> bool

Checks if the path exists.

Source

pub fn is_absolute(&self) -> bool

Returns true if the internal path is absolute.

Source

pub fn is_relative(&self) -> bool

Returns true if the internal path is relative.

Source§

impl SPath

Meta

Source

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)

Source

pub fn metadata(&self) -> Result<Metadata>

Returns the std metadata

Source

pub fn modified(&self) -> Result<SystemTime>

👎Deprecated: use spath.meta()

Returns the path.metadata modified SystemTime

Source

pub fn modified_us(&self) -> Result<i64>

👎Deprecated: use spath.meta()

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

Source

pub fn canonicalize(&self) -> Result<SPath>

This perform a OS Canonicalization.

Source

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.

Source

pub fn into_collapsed(self) -> SPath

Same as [collapse] but consume and create a new SPath only if needed

Source

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.

Source

pub fn parent(&self) -> Option<SPath>

Returns the parent directory as an Option.

Source

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 + _backupfoo.rs_backup
Source

pub fn join(&self, leaf_path: impl Into<Utf8PathBuf>) -> SPath

Joins the provided path with the current path and returns an SPath.

Source

pub fn join_std_path(&self, leaf_path: impl AsRef<Path>) -> Result<SPath>

Joins a standard Path to the path of this SPath.

Source

pub fn new_sibling(&self, leaf_path: impl AsRef<str>) -> SPath

Creates a new sibling SPath with the given leaf_path.

Source

pub fn new_sibling_std_path(&self, leaf_path: impl AsRef<Path>) -> Result<SPath>

Creates a new sibling SPath with the given standard path.

Source

pub fn diff(&self, base: impl AsRef<Utf8Path>) -> Option<SPath>

Source

pub fn try_diff(&self, base: impl AsRef<Utf8Path>) -> Result<SPath>

Source

pub fn replace_prefix( &self, base: impl AsRef<str>, with: impl AsRef<str>, ) -> SPath

Source

pub fn into_replace_prefix( self, base: impl AsRef<str>, with: impl AsRef<str>, ) -> SPath

Source§

impl SPath

Path/UTF8Path/Camino passthrough

Source

pub fn as_std_path(&self) -> &Path

Source

pub fn strip_prefix(&self, prefix: impl AsRef<Path>) -> Result<SPath>

Returns a path that, when joined onto base, yields self.

§Errors

If base is not a prefix of self

Source

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

Source

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)
Source

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)
Source

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)
Source§

impl SPath

Other

Source

pub fn dir_before_glob(&self) -> Option<SPath>

Returns a new SPath for the eventual directory before the first glob expression.

If not a glob, will return none

§Examples
  • /some/path/**/src/*.rs/some/path
  • **/src/*.rs""
  • /some/{src,doc}/**/*/some

Trait Implementations§

Source§

impl AsRef<Path> for SPath

Source§

fn as_ref(&self) -> &Path

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<SPath> for SFile

Source§

fn as_ref(&self) -> &SPath

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<SPath> for SPath

Source§

fn as_ref(&self) -> &SPath

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Utf8Path> for SPath

Source§

fn as_ref(&self) -> &Utf8Path

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<str> for SPath

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for SPath

Source§

fn clone(&self) -> SPath

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SPath

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for SPath

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&SFile> for SPath

Source§

fn from(val: &SFile) -> Self

Converts to this type from the input type.
Source§

impl From<&SPath> for PathBuf

Source§

fn from(val: &SPath) -> Self

Converts to this type from the input type.
Source§

impl From<&SPath> for String

Source§

fn from(val: &SPath) -> Self

Converts to this type from the input type.
Source§

impl From<&String> for SPath

Source§

fn from(path: &String) -> Self

Converts to this type from the input type.
Source§

impl From<&Utf8Path> for SPath

Source§

fn from(path: &Utf8Path) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for SPath

Source§

fn from(path: &str) -> Self

Converts to this type from the input type.
Source§

impl From<SFile> for SPath

Source§

fn from(val: SFile) -> Self

Converts to this type from the input type.
Source§

impl From<SPath> for PathBuf

Source§

fn from(val: SPath) -> Self

Converts to this type from the input type.
Source§

impl From<SPath> for String

Source§

fn from(val: SPath) -> Self

Converts to this type from the input type.
Source§

impl From<SPath> for Utf8PathBuf

Source§

fn from(val: SPath) -> Self

Converts to this type from the input type.
Source§

impl From<String> for SPath

Source§

fn from(path: String) -> Self

Converts to this type from the input type.
Source§

impl From<Utf8PathBuf> for SPath

Source§

fn from(path_buf: Utf8PathBuf) -> Self

Converts to this type from the input type.
Source§

impl TryFrom<DirEntry> for SPath

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(fs_entry: DirEntry) -> Result<SPath>

Performs the conversion.
Source§

impl TryFrom<DirEntry> for SPath

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(wd_entry: DirEntry) -> Result<SPath>

Performs the conversion.
Source§

impl TryFrom<PathBuf> for SPath

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(path_buf: PathBuf) -> Result<SPath>

Performs the conversion.
Source§

impl TryFrom<SPath> for SFile

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(path: SPath) -> Result<SFile>

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for SPath

§

impl RefUnwindSafe for SPath

§

impl Send for SPath

§

impl Sync for SPath

§

impl Unpin for SPath

§

impl UnwindSafe for SPath

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.