Struct file_paths::FlexPath
source · pub struct FlexPath(/* private fields */);
Expand description
The FlexPath
structure represents an always-resolved textual file path based
on a [FlexPathVariant].
Implementations§
source§impl FlexPath
impl FlexPath
sourcepub fn new(path: &str, variant: FlexPathVariant) -> Self
pub fn new(path: &str, variant: FlexPathVariant) -> Self
Constructs a FlexPath
with a given variant
. This method
will resolve the specified path.
sourcepub fn new_common(path: &str) -> Self
pub fn new_common(path: &str) -> Self
Constructs a FlexPath
whose variant is Common
. This method
will resolve the specified path.
sourcepub fn new_native(path: &str) -> Self
pub fn new_native(path: &str) -> Self
Constructs a FlexPath
whose variant is chosen according to the target platform.
This method will resolve the specified path.
sourcepub fn from_n<'a, T: IntoIterator<Item = &'a str>>(
paths: T,
variant: FlexPathVariant
) -> Self
pub fn from_n<'a, T: IntoIterator<Item = &'a str>>( paths: T, variant: FlexPathVariant ) -> Self
Constructs a FlexPath
from multiple paths and a given variant
.
sourcepub fn from_n_common<'a, T: IntoIterator<Item = &'a str>>(paths: T) -> Self
pub fn from_n_common<'a, T: IntoIterator<Item = &'a str>>(paths: T) -> Self
Constructs a FlexPath
from multiple paths and a Common
variant.
sourcepub fn from_n_native<'a, T: IntoIterator<Item = &'a str>>(paths: T) -> Self
pub fn from_n_native<'a, T: IntoIterator<Item = &'a str>>(paths: T) -> Self
Constructs a FlexPath
from multiple paths and a variant based on
the target platform.
sourcepub fn variant(&self) -> FlexPathVariant
pub fn variant(&self) -> FlexPathVariant
Returns the variant this FlexPath
object is based on.
sourcepub fn is_absolute(&self) -> bool
pub fn is_absolute(&self) -> bool
Indicates whether the FlexPath
is absolute or not.
sourcepub fn resolve(&self, path2: &str) -> FlexPath
pub fn resolve(&self, path2: &str) -> FlexPath
Resolves path2
relative to path1
.
Behavior:
- Eliminates the segments
..
and.
. - If
path2
is absolute, this function returns a resolution of solelypath2
. - All path separators that are backslashes (
\
) are replaced by forward ones (/
). - If any path is absolute, this function returns an absolute path.
- Any empty segment and trailing path separators, such as in
a/b/
anda//b
are eliminated.
sourcepub fn resolve_n<'a, T: IntoIterator<Item = &'a str>>(
&self,
paths: T
) -> FlexPath
pub fn resolve_n<'a, T: IntoIterator<Item = &'a str>>( &self, paths: T ) -> FlexPath
Resolves multiple paths relative to this path. The
behavior is similiar to [.resolve
]. If the given
set has no items, an empty string is returned.
sourcepub fn relative(&self, to_path: &str) -> String
pub fn relative(&self, to_path: &str) -> String
Finds the relative path from this path to to_path
.
Behavior:
- If the paths refer to the same path, this function returns an empty string.
- The function ensures that both paths are absolute and resolves
any
..
and.
segments inside. - If both paths have different prefix,
to_path
is returned.
Panics
Panics if given paths are not absolute.
Example
use file_paths::FlexPath;
assert_eq!("", FlexPath::new_common("/a/b").relative("/a/b"));
assert_eq!("c", FlexPath::new_common("/a/b").relative("/a/b/c"));
assert_eq!("../../c/d", FlexPath::new_common("/a/b").relative("/c/d"));
assert_eq!("../c", FlexPath::new_common("/a/b").relative("/a/c"));
sourcepub fn change_extension(&self, extension: &str) -> FlexPath
pub fn change_extension(&self, extension: &str) -> FlexPath
Changes the extension of a path and returns a new string.
This method adds any lacking dot (.
) prefix automatically to the
extension
argument.
This method allows multiple dots per extension. If that is not
desired, use [.change_last_extension
].
Example
use file_paths::FlexPath;
assert_eq!("a.y", FlexPath::new_common("a.x").change_extension(".y").to_string());
assert_eq!("a.z", FlexPath::new_common("a.x.y").change_extension(".z").to_string());
assert_eq!("a.z.w", FlexPath::new_common("a.x.y").change_extension(".z.w").to_string());
sourcepub fn change_last_extension(&self, extension: &str) -> FlexPath
pub fn change_last_extension(&self, extension: &str) -> FlexPath
Changes only the last extension of a path and returns a new string.
This method adds any lacking dot (.
) prefix automatically to the
extension
argument.
Panics
Panics if the extension contains more than one dot.
sourcepub fn has_extension(&self, extension: &str) -> bool
pub fn has_extension(&self, extension: &str) -> bool
Checks if a file path has a specific extension.
This method adds any lacking dot (.
) prefix automatically to the
extension
argument.
sourcepub fn has_extensions<'a, T: IntoIterator<Item = &'a str>>(
&self,
extensions: T
) -> bool
pub fn has_extensions<'a, T: IntoIterator<Item = &'a str>>( &self, extensions: T ) -> bool
Checks if a file path has any of multiple specific extensions.
This method adds any lacking dot (.
) prefix automatically to each
extension argument.
sourcepub fn base_name(&self) -> String
pub fn base_name(&self) -> String
Returns the base name of a file path.
Example
use file_paths::FlexPath;
assert_eq!("qux.html", FlexPath::new_common("foo/qux.html").base_name());
sourcepub fn base_name_without_ext<'a, T>(&self, extensions: T) -> Stringwhere
T: IntoIterator<Item = &'a str>,
pub fn base_name_without_ext<'a, T>(&self, extensions: T) -> Stringwhere
T: IntoIterator<Item = &'a str>,
Returns the base name of a file path, removing any of the specified extensions.
This method adds any lacking dot (.
) prefix automatically to each
extension argument.
Example
use file_paths::FlexPath;
assert_eq!("qux", FlexPath::new_common("foo/qux.html").base_name_without_ext([".html"]));
sourcepub fn to_string_with_flex_separator(&self) -> String
pub fn to_string_with_flex_separator(&self) -> String
Returns a string representation of the path,
delimiting segments with either a forward slash (/
) or backward slash (\
)
depending on the path’s FlexPathVariant
.
Trait Implementations§
source§impl Ord for FlexPath
impl Ord for FlexPath
source§impl PartialEq for FlexPath
impl PartialEq for FlexPath
source§impl PartialOrd for FlexPath
impl PartialOrd for FlexPath
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more