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
impl CrossPathBuf
Sourcepub fn new(str_path: &str) -> Result<Self>
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
Sourcepub fn to_path_buf_win(&self) -> PathBuf
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%
Sourcepub fn to_path_buf_nix(&self) -> PathBuf
pub fn to_path_buf_nix(&self) -> PathBuf
Converts crossplatform path into Linux path.
‘~’ will be transformed into home
Sourcepub fn to_path_buf_current_os(&self) -> PathBuf
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%
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the crossplatform str for use in Display and store into config files.
Sourcepub fn is_file(&self) -> bool
pub fn is_file(&self) -> bool
Returns true if the path exists on disk and is pointing at a regular file.
Sourcepub fn is_dir(&self) -> bool
pub fn is_dir(&self) -> bool
Returns true if the path exists on disk and is pointing at a directory.
Sourcepub fn join_relative(&self, str_path: &str) -> Result<Self>
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.
Sourcepub fn read_to_string(&self) -> Result<String>
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
Sourcepub fn write_str_to_file(&self, content: &str) -> Result<()>
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.
Sourcepub fn write_bytes_to_file(&self, content: &[u8]) -> Result<()>
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.
Sourcepub fn create_dir_all(&self) -> Result<()>
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.
Sourcepub fn create_dir_all_for_file(&self) -> Result<()>
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.
Sourcepub fn trim_start_slash(&self) -> Result<Self>
pub fn trim_start_slash(&self) -> Result<Self>
Returns a CrossPathBuf without leading start slash (repeatedly removed).
Sourcepub fn trim_end_slash(&self) -> Result<Self>
pub fn trim_end_slash(&self) -> Result<Self>
Returns a CrossPathBuf without trailing end slash (repeatedly removed).
Sourcepub fn add_start_slash(&self) -> Result<Self>
pub fn add_start_slash(&self) -> Result<Self>
Returns a CrossPathBuf with one leading start slash.
Sourcepub fn add_end_slash(&self) -> Result<Self>
pub fn add_end_slash(&self) -> Result<Self>
Returns a CrossPathBuf with one trailing end slash.
Sourcepub fn file_name(&self) -> Result<String>
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.
Sourcepub fn extension(&self) -> Result<String>
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.
Sourcepub fn file_stem(&self) -> Result<String>
pub fn file_stem(&self) -> Result<String>
Extracts the stem (non-extension) portion of file_name (the final component of the Path).
Sourcepub fn parent(&self) -> Result<Self>
pub fn parent(&self) -> Result<Self>
Returns the Path without its final component, if there is one.
Sourcepub fn replace_extension(&self, extension: &str) -> Result<Self>
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.
Sourcepub fn short_string(&self, max_char: u16) -> Result<String>
pub fn short_string(&self, max_char: u16) -> Result<String>
Shorten the crossplatform path to avoid word-wrap for longer paths.
Sourcepub fn decompress_tar_gz(&self, destination_folder: &CrossPathBuf) -> Result<()>
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.
Sourcepub fn remove_file(&self) -> Result<()>
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).
Sourcepub fn remove_dir_all(&self) -> Result<()>
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.
Sourcepub fn copy_file_to_file(&self, destination_file: &CrossPathBuf) -> Result<()>
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.
Sourcepub fn rename_or_move(&self, destination_file: &CrossPathBuf) -> Result<()>
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
impl Clone for CrossPathBuf
Source§fn clone(&self) -> CrossPathBuf
fn clone(&self) -> CrossPathBuf
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CrossPathBuf
impl Debug for CrossPathBuf
Source§impl Display for CrossPathBuf
Method display() is used in format!(“{}”).
impl Display for CrossPathBuf
Method display() is used in format!(“{}”).
Source§impl From<CrossPathBuf> for PathBuf
CrossPathBuf from() and into() are useful in places where PathBuf is needed.
impl From<CrossPathBuf> for PathBuf
CrossPathBuf from() and into() are useful in places where PathBuf is needed.
Source§fn from(cross_path: CrossPathBuf) -> Self
fn from(cross_path: CrossPathBuf) -> Self
CrossPathBuf from() and into() are useful in places where PathBuf is needed.