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

Path Safety

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

This presents libraries with two options for consistent path handling:

  1. Allow constructing unsafe paths, allowing for both reading and writing of data to paths that may not be consistently understood or supported
  2. Disallow constructing unsafe paths, ensuring data written can be consistently handled by all other systems, but preventing interaction with objects at unsafe paths

This library takes the second approach, in particular:

  • Paths are delimited by /
  • Paths do not start with a /
  • Empty path segments are discarded (e.g. // is treated as though it were /)
  • Relative path segments, i.e. . and .. are percent encoded
  • Unsafe characters are percent encoded, as described by RFC 1738
  • All paths are relative to the root of the object store

In order to provide these guarantees there are two ways to safely construct a Path

Encode

A string containing potentially illegal path segments can be encoded to a Path using Path::from or Path::from_iter.

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_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 created from an existing string, returning an error if it is invalid. Unlike the encoding methods, this will permit valid percent encoded sequences.


assert_eq!(Path::parse("/foo/foo%2Fbar").unwrap().as_ref(), "foo/foo%2Fbar");
Path::parse("..").unwrap_err();
Path::parse("/foo//").unwrap_err();
Path::parse("😀").unwrap_err();

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

source§

impl StructuralPartialEq for Path

Auto Trait Implementations§

§

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
§

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

§

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

Compare self to key and return their ordering.
§

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

§

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

Checks if this value is equivalent to the given key. Read more
§

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

§

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

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

§

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