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:
- Allow constructing unsafe paths, allowing for both reading and writing of data to paths that may not be consistently understood or supported
- 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
impl Path
sourcepub fn filename(&self) -> Option<&str>
pub fn filename(&self) -> Option<&str>
Returns the last path segment containing the filename stored in this Path
sourcepub fn extension(&self) -> Option<&str>
pub fn extension(&self) -> Option<&str>
Returns the extension of the file stored in this Path
, if any
sourcepub fn prefix_match(
&self,
prefix: &Self
) -> Option<impl Iterator<Item = PathPart<'_>> + '_>
pub fn prefix_match( &self, prefix: &Self ) -> Option<impl Iterator<Item = PathPart<'_>> + '_>
sourcepub fn prefix_matches(&self, prefix: &Self) -> bool
pub fn prefix_matches(&self, prefix: &Self) -> bool
Returns true if this Path
starts with prefix
Trait Implementations§
source§impl<'a, I> FromIterator<I> for Path
impl<'a, I> FromIterator<I> for Path
source§fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self
source§impl Ord for Path
impl Ord for Path
source§impl PartialEq for Path
impl PartialEq for Path
source§impl PartialOrd for Path
impl PartialOrd for Path
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl Eq for Path
impl StructuralEq for Path
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.