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

source

pub fn new(path: &str, variant: FlexPathVariant) -> Self

Constructs a FlexPath with a given variant. This method will resolve the specified path.

source

pub fn new_common(path: &str) -> Self

Constructs a FlexPath whose variant is Common. This method will resolve the specified path.

source

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.

source

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.

source

pub fn from_n_common<'a, T: IntoIterator<Item = &'a str>>(paths: T) -> Self

Constructs a FlexPath from multiple paths and a Common variant.

source

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.

source

pub fn variant(&self) -> FlexPathVariant

Returns the variant this FlexPath object is based on.

source

pub fn is_absolute(&self) -> bool

Indicates whether the FlexPath is absolute or not.

source

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 solely path2.
  • 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/ and a//b are eliminated.
source

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.

source

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"));
source

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());
source

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.

source

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.

source

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.

source

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());
source

pub fn base_name_without_ext<'a, T>(&self, extensions: T) -> String
where 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"]));
source

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 Clone for FlexPath

source§

fn clone(&self) -> FlexPath

Returns a copy 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 FlexPath

source§

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

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

impl Ord for FlexPath

source§

fn cmp(&self, other: &FlexPath) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for FlexPath

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for FlexPath

source§

fn partial_cmp(&self, other: &FlexPath) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl ToString for FlexPath

source§

fn to_string(&self) -> String

Returns a string representation of the path, always delimiting segments with a forward slash (/).

source§

impl Eq for FlexPath

source§

impl StructuralEq for FlexPath

source§

impl StructuralPartialEq for FlexPath

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> 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,

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.