Struct Path

Source
pub struct Path<'path> { /* private fields */ }
Expand description

The path component as defined in [RFC3986, Section 3.3].

A path is composed of a sequence of segments. It is also either absolute or relative, where an absolute path starts with a '/'. A URI with an authority always has an absolute path regardless of whether the path was empty (i.e. “http://example.com” has a single empty path segment and is absolute).

Each segment in the path is case-sensitive. Furthermore, percent-encoding plays no role in equality checking for characters in the unreserved character set meaning that "segment" and "s%65gment" are identical. Both of these attributes are reflected in the equality and hash functions.

However, be aware that just because percent-encoding plays no role in equality checking does not mean that either the path or a given segment is normalized. If the path or a segment needs to be normalized, use either the Path::normalize or Segment::normalize functions, respectively.

Implementations§

Source§

impl<'path> Path<'path>

Source

pub fn clear(&mut self)

Clears all segments from the path leaving a single empty segment.

§Examples
use std::convert::TryFrom;

use uriparse::Path;

let mut path = Path::try_from("/my/path").unwrap();
assert_eq!(path, "/my/path");
path.clear();
assert_eq!(path, "/");
Source

pub fn into_owned(self) -> Path<'static>

Converts the Path into an owned copy.

If you construct the path from a source with a non-static lifetime, you may run into lifetime problems due to the way the struct is designed. Calling this function will ensure that the returned value has a static lifetime.

This is different from just cloning. Cloning the path will just copy the references, and thus the lifetime will remain the same.

Source

pub fn is_absolute(&self) -> bool

Returns whether the path is absolute (i.e. it starts with a '/').

Any path following an [Authority] will always be parsed to be absolute.

§Examples
use std::convert::TryFrom;

use uriparse::Path;

let path = Path::try_from("/my/path").unwrap();
assert_eq!(path.is_absolute(), true);
Source

pub fn is_normalized(&self, as_reference: bool) -> bool

Returns whether the path is normalized either as or as not a reference.

See Path::normalize for a full description of what path normalization entails.

Although this function does not operate in constant-time in general, it will be constant-time in the vast majority of cases.

§Examples
use std::convert::TryFrom;

use uriparse::Path;

let path = Path::try_from("/my/path").unwrap();
assert!(path.is_normalized(false));

let path = Path::try_from("/my/p%61th").unwrap();
assert!(!path.is_normalized(false));

let path = Path::try_from("..").unwrap();
assert!(path.is_normalized(true));

let path = Path::try_from("../.././.").unwrap();
assert!(!path.is_normalized(true));
Source

pub fn is_relative(&self) -> bool

Returns whether the path is relative (i.e. it does not start with a '/').

Any path following an [Authority] will always be parsed to be absolute.

§Examples
use std::convert::TryFrom;

use uriparse::Path;

let path = Path::try_from("my/path").unwrap();
assert_eq!(path.is_relative(), true);
Source

pub fn normalize(&mut self, as_reference: bool)

Normalizes the path and all of its segments.

There are two components to path normalization, the normalization of each segment individually and the removal of unnecessary dot segments. It is also guaranteed that whether the path is absolute will not change as a result of normalization.

The normalization of each segment will proceed according to Segment::normalize.

If the path is absolute (i.e., it starts with a '/'), then as_reference will be set to false regardless of its set value.

If as_reference is false, then all dot segments will be removed as they would be if you had called Path::remove_dot_segments. Otherwise, when a dot segment is removed is dependent on whether it’s "." or ".." and its location in the path.

In general, "." dot segments are always removed except for when it is at the beginning of the path and is followed by a segment containing a ':', e.g. "./a:b" stays the same.

For ".." dot segments, they are kept whenever they are at the beginning of the path and removed whenever they are not, e.g. "a/../.." normalizes to "..".

Source

pub fn pop(&mut self)

Pops the last segment off of the path.

If the path only contains one segment, then that segment will become empty.

use std::convert::TryFrom;

use uriparse::Path;

let mut path = Path::try_from("/my/path").unwrap();
path.pop();
assert_eq!(path, "/my");
path.pop();
assert_eq!(path, "/");
Source

pub fn push<TSegment, TSegmentError>( &mut self, segment: TSegment, ) -> Result<(), PathError>
where Segment<'path>: TryFrom<TSegment, Error = TSegmentError>, PathError: From<TSegmentError>,

Pushes a segment onto the path.

If the conversion to a Segment fails, an [InvalidPath] will be returned.

The behavior of this function is different if the current path is just one empty segment. In this case, the pushed segment will replace that empty segment unless the pushed segment is itself empty.

use std::convert::TryFrom;

use uriparse::Path;

let mut path = Path::try_from("/my/path").unwrap();
path.push("test");
assert_eq!(path, "/my/path/test");

let mut path = Path::try_from("/").unwrap();
path.push("test");
assert_eq!(path, "/test");

let mut path = Path::try_from("/").unwrap();
path.push("");
assert_eq!(path, "//");
Source

pub fn remove_dot_segments(&mut self)

Removes all dot segments from the path according to the algorithm described in [RFC3986, Section 5.2.4].

This function will perform no memory allocations during removal of dot segments.

If the path currently has no dot segments, then this function is a no-op.

§Examples
use std::convert::TryFrom;

use uriparse::Path;

let mut path = Path::try_from("/a/b/c/./../../g").unwrap();
path.remove_dot_segments();
assert_eq!(path, "/a/g");
Source

pub fn segments(&self) -> &[Segment<'path>]

Returns the segments of the path.

If you require mutability, use Path::segments_mut.

§Examples
use std::convert::TryFrom;

use uriparse::Path;

let mut path = Path::try_from("/my/path").unwrap();
assert_eq!(path.segments()[1], "path");
Source

pub fn segments_mut(&mut self) -> &mut [Segment<'path>]

Returns the segments of the path mutably.

Due to the required restriction that there must be at least one segment in a path, this mutability only applies to the segments themselves, not the container.

§Examples
use std::convert::TryFrom;

use uriparse::{Path, Segment};

let mut path = Path::try_from("/my/path").unwrap();
let mut segments = path.segments_mut();
segments[1] = Segment::try_from("test").unwrap();

assert_eq!(path, "/my/test");
Source

pub fn set_absolute(&mut self, absolute: bool)

Sets whether the path is absolute (i.e. it starts with a '/').

§Examples
use std::convert::TryFrom;

use uriparse::Path;

let mut path = Path::try_from("/my/path").unwrap();
path.set_absolute(false);
assert_eq!(path, "my/path");
Source

pub fn to_borrowed(&self) -> Path<'_>

Returns a new path which is identical but has a lifetime tied to this path.

This function will perform a memory allocation.

Trait Implementations§

Source§

impl<'path> Clone for Path<'path>

Source§

fn clone(&self) -> Path<'path>

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<'path> Debug for Path<'path>

Source§

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

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

impl Display for Path<'_>

Source§

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

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

impl<'path> From<Path<'path>> for String

Source§

fn from(value: Path<'path>) -> Self

Converts to this type from the input type.
Source§

impl Hash for Path<'_>

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> PartialEq<&'a [u8]> for Path<'_>

Source§

fn eq(&self, other: &&'a [u8]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialEq<&'a str> for Path<'_>

Source§

fn eq(&self, other: &&'a str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<[u8]> for Path<'_>

Source§

fn eq(&self, other: &[u8]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'path> PartialEq<Path<'path>> for &'a [u8]

Source§

fn eq(&self, other: &Path<'path>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'path> PartialEq<Path<'path>> for &'a str

Source§

fn eq(&self, other: &Path<'path>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'path> PartialEq<Path<'path>> for [u8]

Source§

fn eq(&self, other: &Path<'path>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'path> PartialEq<Path<'path>> for str

Source§

fn eq(&self, other: &Path<'path>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<str> for Path<'_>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for Path<'_>

Source§

fn eq(&self, other: &Path<'_>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'path> TryFrom<&'path [u8]> for Path<'path>

Source§

type Error = PathError

The type returned in the event of a conversion error.
Source§

fn try_from(value: &'path [u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'path> TryFrom<&'path str> for Path<'path>

Source§

type Error = PathError

The type returned in the event of a conversion error.
Source§

fn try_from(value: &'path str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'path> Eq for Path<'path>

Auto Trait Implementations§

§

impl<'path> Freeze for Path<'path>

§

impl<'path> RefUnwindSafe for Path<'path>

§

impl<'path> Send for Path<'path>

§

impl<'path> Sync for Path<'path>

§

impl<'path> Unpin for Path<'path>

§

impl<'path> UnwindSafe for Path<'path>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.