Skip to main content

CrossPathBuf

Struct CrossPathBuf 

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

CrossPathBuf stores Path in a Neutral Crossplatform format.

The neutral path is limited to valid utf8 strings.
This format can be stored in config files. It is “similar” to the Linux format, but not exactly equal.
When used for file operations, this Neutral format is converted into Linux or Windows format accordingly.
Some limitations exist for paths mostly because of Windows limitations:
forbidden characters < > : “ / \ | ? * 0 (NULL byte) 0-31 (ASCII control characters)
Filenames cannot end in a space or dot.
Separator is always slash. Backslash is replaced. Backslash must never be a part of a name or path component.
Must not contain reserved words con, prn, aux, nul, com1-com9, lpt1-lpt9, . and ..
If starts with windows c: or d: it is converted to /mnt/c or /mnt/d lowercase

Implementations§

Source§

impl CrossPathBuf

Source

pub fn new(str_path: &str) -> Result<Self>

Creates a new CrossPathBuf from &str.

Path must be always utf8. Rust strings are guaranteed to be utf8.
The input path will be tested that is somehow correct.
It will be transformed from Windows into the crossplatform format. Linux format will stay mostly the same.
The neutral path is limited to valid utf8 strings.
This format can be stored in config files. It is “similar” to the Linux format, but not exactly equal.
When used for file operations, this Neutral format is converted into Linux or Windows format accordingly.
Some limitations exist for paths mostly because of Windows limitations:
forbidden characters < > : “ / \ | ? * 0 (NULL byte) 0-31 (ASCII control characters)
Filenames cannot end in a space or dot.
Separator is always slash. Backslash is replaced. Backslash must never be a part of a name or path component.
Must not contain reserved words con, prn, aux, nul, com1-com9, lpt1-lpt9, . and ..
If start with windows c: or d: convert to /mnt/c or /mnt/d lowercase

Source

pub fn from_path(path: &Path) -> Result<Self>

Creates a new CrossPathBuf from &Path.

Source

pub fn to_path_buf_win(&self) -> PathBuf

Converts crossplatform path into Windows path.

‘~’ will be transformed into home
/mnt/c/ will be transformed into c:\
/mnt/d/ will be transformed into d:\
/tmp will be transformed into %TEMP%

Source

pub fn to_path_buf_nix(&self) -> PathBuf

Converts crossplatform path into Linux path.

‘~’ will be transformed into home

Source

pub fn to_path_buf_current_os(&self) -> PathBuf

Converts crossplatform path into current OS path.

‘~’ will be transformed into home
/mnt/c/ will be transformed into c:\
/mnt/d/ will be transformed into d:\
/tmp will be transformed into %TEMP%

Source

pub fn as_str(&self) -> &str

Returns the crossplatform str for use in Display and store into config files.

Source

pub fn exists(&self) -> bool

Returns true if the path points at an existing entity.

Source

pub fn is_file(&self) -> bool

Returns true if the path exists on disk and is pointing at a regular file.

Source

pub fn is_dir(&self) -> bool

Returns true if the path exists on disk and is pointing at a directory.

Source

pub fn join_relative(&self, str_path: &str) -> Result<Self>

Joins two paths and returns a new CrossPathBuf to allow function chaining.

It works differently from the original Rust join() where if the second path is absolute, it overwrites the first path.
Here the second path is always relative and is added to the first path.

Source

pub fn read_to_string(&self) -> Result<String>

Reads the entire contents of a file into a string.

This is a convenience function based on std::fs::read_to_string

Source

pub fn write_str_to_file(&self, content: &str) -> Result<()>

Writes a string slice as the entire contents of a file.

This function will create a file if it does not exist, and will entirely replace its contents if it does.
It creates the full path directory, if path does not exist.

Source

pub fn write_bytes_to_file(&self, content: &[u8]) -> Result<()>

Writes a byte slice as the entire contents of a file.

This function will create a file if it does not exist, and will entirely replace its contents if it does.
It creates the full path directory, if path does not exist.

Source

pub fn create_dir_all(&self) -> Result<()>

Recursively create this path as directory and all of its parent components if they are missing.

The cross_path must represent a directory and not a file for this command.
This function is not atomic. If it returns an error, any parent components it was able to create will remain.

Source

pub fn create_dir_all_for_file(&self) -> Result<()>

Recursively create the parent directory of a file and all of its parent components if they are missing.

The cross_path must represent a file. The parent directory will be created.
This function is not atomic. If it returns an error, any parent components it was able to create will remain.

Source

pub fn trim_start_slash(&self) -> Result<Self>

Returns a CrossPathBuf without leading start slash (repeatedly removed).

Source

pub fn trim_end_slash(&self) -> Result<Self>

Returns a CrossPathBuf without trailing end slash (repeatedly removed).

Source

pub fn add_start_slash(&self) -> Result<Self>

Returns a CrossPathBuf with one leading start slash.

Source

pub fn add_end_slash(&self) -> Result<Self>

Returns a CrossPathBuf with one trailing end slash.

Source

pub fn file_name(&self) -> Result<String>

Returns the final component of the Path, 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.

Source

pub fn extension(&self) -> Result<String>

Extracts the extension (without the leading dot), if possible.

It is different from the std::fs extension() because
it returns an empty string if there is no extension.
It returns Error only if there is no file_name.

Source

pub fn file_stem(&self) -> Result<String>

Extracts the stem (non-extension) portion of file_name (the final component of the Path).

Source

pub fn parent(&self) -> Result<Self>

Returns the Path without its final component, if there is one.

Source

pub fn replace_extension(&self, extension: &str) -> Result<Self>

Returns new object where the extension is replaced.

If the extension did not exist, it is added.

Source

pub fn short_string(&self, max_char: u16) -> Result<String>

Shorten the crossplatform path to avoid word-wrap for longer paths.

Source

pub fn decompress_tar_gz(&self, destination_folder: &CrossPathBuf) -> Result<()>

Decompress tar.gz into destination folder.

It creates the full path destination folder, if path does not exist.

Source

pub fn remove_file(&self) -> Result<()>

Removes a file from the filesystem.

Note that there is no guarantee that the file is immediately deleted (e.g., depending on platform, other open file descriptors may prevent immediate removal).

Source

pub fn remove_dir_all(&self) -> Result<()>

Removes a directory at this path, after removing all its contents. Use carefully!

This function does not follow symbolic links and it will simply remove the symbolic link itself.
DIFFERENCE from std::fs::remove_dir_all: The directory you are deleting does not need to exist.

Source

pub fn copy_file_to_file(&self, destination_file: &CrossPathBuf) -> Result<()>

Copies the contents of one file to another file.

This function will also copy the permission bits of the original file to the destination file.
It creates the full path destination folder, if path does not exist.
DIFFERENCE from std::fs::copy If the source and destination is the same nothing happens.

Source

pub fn rename_or_move(&self, destination_file: &CrossPathBuf) -> Result<()>

Renames a file or directory to a new name, replacing the original file if to already exists.

Trait Implementations§

Source§

impl Clone for CrossPathBuf

Source§

fn clone(&self) -> CrossPathBuf

Returns a duplicate 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 CrossPathBuf

Source§

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

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

impl Display for CrossPathBuf

Method display() is used in format!(“{}”).

Source§

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

Method display() is used in format!(“{}”).

Source§

impl From<CrossPathBuf> for PathBuf

CrossPathBuf from() and into() are useful in places where PathBuf is needed.

Source§

fn from(cross_path: CrossPathBuf) -> Self

CrossPathBuf from() and into() are useful in places where PathBuf is needed.

Source§

impl PartialEq for CrossPathBuf

Source§

fn eq(&self, other: &CrossPathBuf) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for CrossPathBuf

Auto Trait Implementations§

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.