Struct object_store::path::Path

source ·
pub struct Path { /* private fields */ }
Expand description

A parsed path representation that can be safely written to object storage

A Path maintains the following invariants:

  • Paths are delimited by /
  • Paths do not contain leading or trailing /
  • Paths do not contain relative path segments, i.e. . or ..
  • Paths do not contain empty path segments
  • Paths do not contain any ASCII control characters

There are no enforced restrictions on path length, however, it should be noted that most object stores do not permit paths longer than 1024 bytes, and many filesystems do not support path segments longer than 255 bytes.

§Encode

In theory object stores support any UTF-8 character sequence, however, certain character sequences cause compatibility problems with some applications and protocols. Additionally some filesystems may impose character restrictions, see LocalFileSystem. As such the naming guidelines for S3, GCS and Azure Blob Storage all recommend sticking to a limited character subset.

A string containing potentially problematic path segments can therefore be encoded to a Path using Path::from or Path::from_iter. This will percent encode any problematic segments according to RFC 1738.

assert_eq!(Path::from("foo/bar").as_ref(), "foo/bar");
assert_eq!(Path::from("foo//bar").as_ref(), "foo/bar");
assert_eq!(Path::from("foo/../bar").as_ref(), "foo/%2E%2E/bar");
assert_eq!(Path::from("/").as_ref(), "");
assert_eq!(Path::from_iter(["foo", "foo/bar"]).as_ref(), "foo/foo%2Fbar");

Note: if provided with an already percent encoded string, this will encode it again

assert_eq!(Path::from("foo/foo%2Fbar").as_ref(), "foo/foo%252Fbar");

§Parse

Alternatively a Path can be parsed from an existing string, returning an error if it is invalid. Unlike the encoding methods above, this will permit arbitrary unicode, including percent encoded sequences.

assert_eq!(Path::parse("/foo/foo%2Fbar").unwrap().as_ref(), "foo/foo%2Fbar");
Path::parse("..").unwrap_err(); // Relative path segments are disallowed
Path::parse("/foo//").unwrap_err(); // Empty path segments are disallowed
Path::parse("\x00").unwrap_err(); // ASCII control characters are disallowed

Implementations§

source§

impl Path

source

pub fn parse(path: impl AsRef<str>) -> Result<Self, Error>

Parse a string as a Path, returning a Error if invalid, as defined on the docstring for Path

Note: this will strip any leading / or trailing /

source

pub fn from_filesystem_path(path: impl AsRef<Path>) -> Result<Self, Error>

Convert a filesystem path to a Path relative to the filesystem root

This will return an error if the path contains illegal character sequences as defined on the docstring for Path or does not exist

Note: this will canonicalize the provided path, resolving any symlinks

source

pub fn from_absolute_path(path: impl AsRef<Path>) -> Result<Self, Error>

Convert an absolute filesystem path to a Path relative to the filesystem root

This will return an error if the path contains illegal character sequences, as defined on the docstring for Path, or base is not an absolute path

source

pub fn from_url_path(path: impl AsRef<str>) -> Result<Self, Error>

Parse a url encoded string as a Path, returning a Error if invalid

This will return an error if the path contains illegal character sequences as defined on the docstring for Path

source

pub fn parts(&self) -> impl Iterator<Item = PathPart<'_>>

Returns the PathPart of this Path

source

pub fn filename(&self) -> Option<&str>

Returns the last path segment containing the filename stored in this Path

source

pub fn extension(&self) -> Option<&str>

Returns the extension of the file stored in this Path, if any

source

pub fn prefix_match( &self, prefix: &Self, ) -> Option<impl Iterator<Item = PathPart<'_>> + '_>

Returns an iterator of the PathPart of this Path after prefix

Returns None if the prefix does not match

source

pub fn prefix_matches(&self, prefix: &Self) -> bool

Returns true if this Path starts with prefix

source

pub fn child<'a>(&self, child: impl Into<PathPart<'a>>) -> Self

Creates a new child of this Path

Trait Implementations§

source§

impl AsRef<str> for Path

source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for Path

source§

fn clone(&self) -> 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 Debug for Path

source§

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

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

impl Default for Path

source§

fn default() -> Path

Returns the “default value” for a type. Read more
source§

impl Display for Path

source§

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

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

impl From<&str> for Path

source§

fn from(path: &str) -> Self

Converts to this type from the input type.
source§

impl From<Path> for String

source§

fn from(path: Path) -> Self

Converts to this type from the input type.
source§

impl From<String> for Path

source§

fn from(path: String) -> Self

Converts to this type from the input type.
source§

impl<'a, I> FromIterator<I> for Path
where I: Into<PathPart<'a>>,

source§

fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl Hash for Path

source§

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

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 Ord for Path

source§

fn cmp(&self, other: &Path) -> 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 Path

source§

fn eq(&self, other: &Path) -> 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 Path

source§

fn partial_cmp(&self, other: &Path) -> 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 Eq for Path

source§

impl StructuralPartialEq for Path

Auto Trait Implementations§

§

impl Freeze for Path

§

impl RefUnwindSafe for Path

§

impl Send for Path

§

impl Sync for Path

§

impl Unpin for Path

§

impl UnwindSafe for 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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

§

type Output = T

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

source§

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

§

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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more