Struct SFile

Source
pub struct SFile { /* private fields */ }
Expand description

An SFile can be constructed from a Path, io::DirEntry, or walkdir::DirEntry and guarantees the following:

  • The entry is a file (exists).
  • It has a file name.
  • The full path is UTF-8 valid.

Implementations§

Source§

impl SFile

Constructors that guarantee the SFile contract described in the struct

Source

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

Constructor for SFile accepting anything that implements Into.

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>

Constructors for anything that implements AsRef.

Returns Option. Useful for filter_map.

Source

pub fn from_std_path_buf_ok(path_buf: PathBuf) -> Option<Self>

Constructor from PathBuf returning 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 SFile

Public into path

Source

pub fn into_std_path_buf(self) -> PathBuf

Consumes the SFile and returns its PathBuf.

Source

pub fn std_path(&self) -> &Path

Returns a reference to the internal standard Path.

Source

pub fn path(&self) -> &SPath

Returns a reference to the internal Utf8Path.

Source§

impl SFile

Public getters

Source

pub fn to_str(&self) -> &str

👎Deprecated: Use as_str() instead

Returns the &str of the path.

NOTE: We know that this must be Some() since the SFile constructor guarantees that the path.as_str() is valid.

Source

pub fn as_str(&self) -> &str

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().

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 SFile construction.

Source

pub fn ext(&self) -> &str

Same as .extension() but returns “” if no extension.

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 SFile

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() or spath.metadata

Returns the path.metadata modified as 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 not to worry.

Source

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

👎Deprecated: use spath.meta()

Returns the file size in bytes as i64. Note: In the highly unlikely event that the size exceeds i64::MAX, i64::MAX is returned. i64::MAX represents 8,388,607 terabytes, providing ample margin before it becomes a concern.

Source§

impl SFile

Transformers

Source

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

Source

pub fn collapse(&self) -> SFile

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

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 SPath, if available.

Source

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

Joins the current path with the specified leaf_path.

This method creates a new path by joining the existing path with a specified leaf_path and returns the result as 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 SFile.

Source

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

Creates a new sibling path with the specified leaf_path.

Generates a new path in the same parent directory as the current file, appending the leaf_path.

Source

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

Creates a new sibling path with the specified 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 SFile

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"));

Trait Implementations§

Source§

impl AsRef<Path> for SFile

Source§

fn as_ref(&self) -> &Path

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

impl AsRef<SFile> for SFile

Source§

fn as_ref(&self) -> &SFile

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<Utf8Path> for SFile

Source§

fn as_ref(&self) -> &Utf8Path

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

impl AsRef<str> for SFile

Source§

fn as_ref(&self) -> &str

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

impl Clone for SFile

Source§

fn clone(&self) -> SFile

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 SFile

Source§

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

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

impl Display for SFile

Source§

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

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

impl From<&SFile> for PathBuf

Source§

fn from(val: &SFile) -> 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<&SFile> for String

Source§

fn from(val: &SFile) -> Self

Converts to this type from the input type.
Source§

impl From<SFile> for PathBuf

Source§

fn from(val: SFile) -> 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<SFile> for String

Source§

fn from(val: SFile) -> Self

Converts to this type from the input type.
Source§

impl From<SFile> for Utf8PathBuf

Source§

fn from(val: SFile) -> Self

Converts to this type from the input type.
Source§

impl TryFrom<&String> for SFile

Source§

type Error = Error

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

fn try_from(path: &String) -> Result<SFile>

Performs the conversion.
Source§

impl TryFrom<&str> for SFile

Source§

type Error = Error

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

fn try_from(path: &str) -> Result<SFile>

Performs the conversion.
Source§

impl TryFrom<DirEntry> for SFile

Source§

type Error = Error

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

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

Performs the conversion.
Source§

impl TryFrom<DirEntry> for SFile

Source§

type Error = Error

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

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

Performs the conversion.
Source§

impl TryFrom<PathBuf> for SFile

Source§

type Error = Error

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

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

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

impl TryFrom<String> for SFile

Source§

type Error = Error

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

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

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for SFile

§

impl RefUnwindSafe for SFile

§

impl Send for SFile

§

impl Sync for SFile

§

impl Unpin for SFile

§

impl UnwindSafe for SFile

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.