rustypath
Path crate for rust. Managing paths made easy.
Overview
Coverting and Managing paths in rust is now made easy.
Table of contents
Add to your project
Features
In v1.0.0
and above, the code has been divided into features due to growing functionality. The features are listed below:
Creation
: This feature is kind of a default feature as this allows users to create aRPath
struct from these -> [String
,&str
,Path
,PathBuf
].Management
: This feature will be enabled by default and contains methods to manage/manipulate paths.Conversion
: This feature contains all conversion methods fromRPath
to different path types.Boolean
: This feature contains all boolean type methods for checks.
Usage
-
Importing
use RPath; // for all basic functionalities.
Note:
Implementation includes -> [Clone
,Debug
,PartialEq
,Eq
,PartialOrd
,Ord
,Hash
] and a custom trait for printingRPath
.// for printing the path as it is, `Display` trait can be imported. use Display;
-
Creating
RPath
-
From
&str
,Path
andPathBuf
// to create a RPath let rpath_str = from; // NOTE: value can be of type &str, Path (std::path::Path) or PathBuf (std::path::PathBuf) // Examples: let rpath = from; let rpath_ = from; let rpath__ = from;
-
Allocate an empty
RPath
let rpath_empty_or_new = new; // this will allocate an empty RPath
-
-
Path Manipulation
-
Join
// make a RPath let rpath = from; // add childs to it, let new = rpath.join; new.print; // this will print "/temp/abc.txt"
-
Join Multiple
// make a RPath let mut rpath = from; // suppose you want to add multiple components to the path rpath.join_multiple; // here value1, value2 can be any of these types => [&str, Path, or PathBuf] rpath.print; // this will print -> "/temp/value1/value2/..."
-
Basename/Filename
// to get the basename or filename of the path: let rpath: Rpath = from; let basename: &str = rpath.basename;
-
Replace Basename
// suppose there's a RPath -> "/temp/abc.txt" and // you want to just replace "abc.txt" with "xyz.txt" without // typing whole new full paths or rel paths, and just update using // the existing one. let rpath = from; let rpath_ = rpath.with_basename; // this will be "/temp/xyz.txt"
-
Dirname/Parent
let rpath: RPath = from; // to get the parent let parent: RPath = rpath.dirname; // this will be "/temp"
-
Replace Dirname/Parent
// suppose there's a RPath -> "/temp/abc.txt" and // you want to replace "/temp" with something else let rpath: RPath = from; let new = rpath.with_dirname; // this will make it -> "/temp/temp2/abc.txt"
-
Extention
// to get the extension of a file from the RPath let rpath = from; let extension: &str = rpath.extension; // NOTE: this will return either extension (if present) or the basename. // if path is invalid it will print error and exit.
-
Expand
let rpath = from; let expanded = rpath.expand; // this will expand if not full path, else it wont throw error. // remeber that it will only expand the path if the path exits // if "./temp" directory doesnt exist it will return "./temp"
-
Read_Dir (return an iterator with DirEntry)
let rpath = from; for entry in rpath.read_dir.expect
-
Get Current Working Directory
let current_dr: RPath = pwd;
-
Get Home dir of your OS
let home: RPath = gethomedir;
-
Clear the current RPath buffer
let rpath = from; rpath.clear; // this will be equivalent to RPath::new();
-
-
Conversion
-
Convert to string
let rpath = from; let rpath_string = rpath.convert_to_string;
-
Convert to PathBuf
let rpath = from; let rpath_in_pathbuf = rpath.convert_to_pathbuf;
-
-
Boolean
-
exists
let rpath = from; if rpath.exists
-
is_dir/is_file
let rpath = from; if rpath.is_dir else if rpath.is_file
Similarly, the following will work:
-
is_absolute
-
is_symlink
-
is_relative
-
-
Printing
RPath
supports printing as a single string.To use the
.print()
function, importDisplay
traituse Display;
Printing using
.print()
method.let rpath = gethomedir; rpath.print; // this will print `homedir` path in a single line.
To print with other strings:
let rpath = gethomedir; println!; println!;
-
Operators
- Supports
==
and!=
operatorslet rpath = pwd; let rpath_ = gethomedir; if rpath == rpath_ else
- Supports
-
Supports Hash
Issues
If any issues were found kindly add 'em here.
Pull Requests
Pull requests are encouraged and can be added here.