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

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 /

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 by Path::parse or does not exist

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

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 by Path::parse, or base is not an absolute path

Returns the PathPart of this Path

Returns an iterator of the PathPart of this Path after prefix

Returns None if the prefix does not match

Returns true if this Path starts with prefix

Creates a new child of this Path

Trait Implementations

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

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

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

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

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

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

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

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

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

Returns the argument unchanged.

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

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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