pub struct path { /* private fields */ }Expand description
’' are replaced by ‘/’
Implementations§
Source§impl path
impl path
pub fn is_empty(&self) -> bool
Sourcepub fn extension_or_empty(&self) -> &str
pub fn extension_or_empty(&self) -> &str
Returns the file extension of the path, if it exists.
The returned string slice does not include the dot ..
§Examples
use hexga_file_system::*;
assert_eq!(Path::new("foo/bar.txt").extension(), Some("txt"));
assert_eq!(Path::new("foo/archive.tar.gz").extension(), Some("gz"));
assert_eq!(Path::new("foo/bar").extension(), None);Sourcepub fn have_extension(&self) -> bool
pub fn have_extension(&self) -> bool
Returns true if the path has a file extension.
§Examples
use hexga_file_system::*;
assert_eq!(Path::new("foo/bar.txt").have_extension(), true);
assert_eq!(Path::new("foo/archive.tar.gz").have_extension(), true);
assert_eq!(Path::new("foo/bar").have_extension(), false);
assert_eq!(Path::new("foo/.bar").have_extension(), false);Sourcepub fn without_extension(&self) -> &path
pub fn without_extension(&self) -> &path
Returns a new path with the extension removed.
Equivalent to self.with_extension("").
§Examples
use hexga_file_system::*;
assert_eq!(Path::new("foo/bar.txt").without_extension().as_str(), "foo/bar");
assert_eq!(Path::new("foo/archive.tar.gz").without_extension().as_str(), "foo/archive");
assert_eq!(Path::new("foo/bar").without_extension().as_str(), "foo/bar");Sourcepub fn extension(&self) -> Option<&str>
pub fn extension(&self) -> Option<&str>
Returns the most file extensions of the path, if they exists.
The returned string slice does not include the first dot ..
§Examples
use hexga_file_system::*;
assert_eq!(Path::new("foo/bar.txt").extension(), Some("txt"));
assert_eq!(Path::new("foo/archive.tar.gz").extension(), Some("tar.gz"));
assert_eq!(Path::new("foo/bar").extension(), None);Sourcepub fn extensions(
&self,
) -> impl Iterator<Item = &str> + DoubleEndedIterator + FusedIterator
pub fn extensions( &self, ) -> impl Iterator<Item = &str> + DoubleEndedIterator + FusedIterator
Iter over all extensions, left to right
The returned extension does not include the dot ..
§Examples
use hexga_file_system::*;
assert_eq!(Path::new("foo/bar.txt").extensions().collect(), vec!["txt"]);
assert_eq!(Path::new("foo/archive.tar.gz").extension().collect(), vec!["tar", "gz"]);
assert_eq!(Path::new("foo/bar").extensions().collect(), vec![]);
assert_eq!(Path::new("foo/.bar").extensions().collect(), vec![]);
assert_eq!(Path::new("foo/.bar.txt").extensions().collect(), vec!["txt"]);
assert_eq!(Path::new("foo/.bar.txt").extensions().collect(), vec!["txt"]);
assert_eq!(Path::new("foo.buz/bar").extensions().collect(), vec![]);Sourcepub fn with_extension(&self, extension: &str) -> Path
pub fn with_extension(&self, extension: &str) -> Path
Returns a new Path with its current extension replaced by the given extension.
This method replaces the file extension of the path with the provided one. If the path has no extension, the new one is simply appended.
A leading dot (.) in the provided extension is ignored automatically.
To remove the extension entirely, you can pass an empty string (""),
but the the method Self::without_extension is more appropriate because it return a &path.
This method is a convenience wrapper over Self::with_extensions.
§Examples
use hexga_file_system::Path;
assert_eq!(Path::new("foo/bar.txt").with_extension("md").as_str(), "foo/bar.md");
// Leading dot is optional
assert_eq!(Path::new("foo/bar.txt").with_extension(".md").as_str(), "foo/bar.md");
// Remove the extension entirely
assert_eq!(Path::new("foo/bar.txt").with_extension("").as_str(), "foo/bar");
// Add a new extension if none existed
assert_eq!(Path::new("foo/bar").with_extension("md").as_str(), "foo/bar.md");
// Handle compound extensions
assert_eq!(Path::new("foo/archive").with_extension("tar.gz").as_str(), "foo/archive.tar.gz");
assert_eq!(Path::new("foo/archive").with_extension(".tar.gz").as_str(), "foo/archive.tar.gz");Sourcepub fn with_extensions<'a, E>(&'a self, extensions: E) -> Pathwhere
E: IntoIterator<Item = &'a str>,
pub fn with_extensions<'a, E>(&'a self, extensions: E) -> Pathwhere
E: IntoIterator<Item = &'a str>,
Returns a new Path with its extensions replaced by the given sequence of extensions.
Each extension is appended to the base filename, separated by a dot (.).
This method can be used to build paths with compound extensions,
such as "tar.gz".
§Examples
use hexga_file_system::Path;
assert_eq!(
Path::new("foo/bar.txt").with_extensions(["tar", "gz"]).as_str(),
"foo/bar.tar.gz"
);
assert_eq!(
Path::new("foo/bar").with_extensions(["log"]).as_str(),
"foo/bar.log"
);
assert_eq!(
Path::new("foo/bar.tar.gz").with_extensions([]).as_str(),
"foo/bar"
);Sourcepub fn iter(
&self,
) -> impl Iterator<Item = &path> + DoubleEndedIterator + FusedIterator
pub fn iter( &self, ) -> impl Iterator<Item = &path> + DoubleEndedIterator + FusedIterator
Splits the path into its components using / as separators
§Examples
use hexga_file_system::*;
assert_eq!(Path::new("foo/bar/baz.txt").iter().collect(), vec!["foo", "bar", "baz.txt"]);
assert_eq!(Path::new("foo\\bar\\baz.txt").iter().collect(), vec!["foo", "bar", "baz.txt"]);
assert_eq!(Path::new("file.txt").iter().collect(), vec!["file.txt"]);
assert_eq!(Path::new("").iter().collect(), Vec::<String>::new());
assert_eq!(Path::new("/foo/bar/").iter().collect(), vec!["", "foo", "bar", ""]);Sourcepub fn parent_or_empty(&self) -> &path
pub fn parent_or_empty(&self) -> &path
Returns the parent directory of this path using Self::parent, or an empty path if none exists.
Sourcepub fn parent(&self) -> Option<&path>
pub fn parent(&self) -> Option<&path>
Returns the parent directory of this path, if it exists.
The parent is defined as the portion of the path before the last / separator.
If the path has no /, or if the parent would be empty, this returns None.
§Examples
use hexga_file_system::*;
let path = Path::new("foo/bar/baz.txt");
assert_eq!(path.parent().map(|p| p.as_str()), Some("foo/bar"));
let path = Path::new("foo/bar");
assert_eq!(path.parent().map(|p| p.as_str()), Some("foo"));
let path = Path::new("foo/bar/");
assert_eq!(path.parent().map(|p| p.as_str()), Some("foo/bar"));
let path = Path::new("foo");
assert_eq!(path.parent(), None);
let path = Path::new("/");
assert_eq!(path.parent(), None);pub fn push(&self, right: &path) -> Path
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Returns the name of the file or directory at the end of the path,
without the file extensions (the portion before the most left final . in the filename).
If the path has no file name, returns an empty string "".
§Behavior
- If the file name has no
.: returns the entire file name. - If the file name begins with
.and has no other dots: returns the entire file name (e.g.,.gitignore). - Otherwise: returns the portion of the file name before the final
.. - If the path ends with a directory separator or has no file name: returns
"".
§Examples
use hexga_file_system::*;
assert_eq!(Path::new("foo/bar.txt").name(), "bar");
assert_eq!(Path::new("foo/.hidden").name(), ".hidden");
assert_eq!(Path::new("foo/archive.tar.gz").name(), "archive");
assert_eq!(Path::new("foo/bar/").name(), "");
assert_eq!(Path::new("file").name(), "file");Sourcepub fn with_name(&self, name: &str) -> Path
pub fn with_name(&self, name: &str) -> Path
Returns a new file/folder name with the base name replaced by name,
preserving the original extension(s).
The parent directory is not modified. Only the file/folder name is changed.
§Examples
use hexga_file_system::*;
assert_eq!("foo/bar.txt".with_name("baz"), "foo/baz.txt");
assert_eq!("foo/.hidden".with_name("config"), "foo/config");
assert_eq!("foo/.hidden.txt".with_name("config"), "foo/config.txt");
assert_eq!("foo/archive.tar.gz".with_name("new"), "foo/new.tar.gz");
assert_eq!("file".with_name("newfile"), "newfile");Sourcepub fn fullname(&self) -> &str
pub fn fullname(&self) -> &str
Returns the full file name (including all extensions) of the path.
If the path ends with a directory or has no file name, returns an empty string "".
§Examples
use hexga_file_system::*;
assert_eq!("foo/bar.txt".path_fullname(), "bar.txt");
assert_eq!("foo/.hidden".path_fullname(), ".hidden");
assert_eq!("foo/archive.tar.gz".path_fullname(), "archive.tar.gz");
assert_eq!("foo/bar/".path_fullname(), "");
assert_eq!("file".path_fullname(), "file");