Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 55 additions & 14 deletions src/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

//! Path abstraction for Object Storage

use itertools::Itertools;
use percent_encoding::percent_decode;
use std::fmt::Formatter;
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -29,9 +28,12 @@ pub const DELIMITER: &str = "/";
/// The path delimiter as a single byte
pub const DELIMITER_BYTE: u8 = DELIMITER.as_bytes()[0];

/// The path delimiter as a single char
pub const DELIMITER_CHAR: char = DELIMITER_BYTE as char;

mod parts;

pub use parts::{InvalidPart, PathPart};
pub use parts::{InvalidPart, PathPart, PathParts};

/// Error returned by [`Path::parse`]
#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -157,6 +159,11 @@ pub struct Path {
}

impl Path {
/// Create an empty [`Path`], equivalent to `Path::from("/")`.
pub const fn empty() -> Self {
Self { raw: String::new() }
}

/// Parse a string as a [`Path`], returning a [`Error`] if invalid,
/// as defined on the docstring for [`Path`]
///
Expand Down Expand Up @@ -255,14 +262,39 @@ impl Path {
Self::parse(decoded)
}

/// Returns the [`PathPart`] of this [`Path`]
pub fn parts(&self) -> impl Iterator<Item = PathPart<'_>> {
self.raw
.split_terminator(DELIMITER)
.map(|s| PathPart { raw: s.into() })
/// Returns the number of [`PathPart`]s in this [`Path`]
///
/// # Performance
///
/// This operation is `O(n)`, similar to calling `.parts().count()` manually.
pub fn len(&self) -> usize {
self.raw.split_terminator(DELIMITER).count()
}

/// True if this [`Path`] has zero segments, equivalent to `Path::from("/")`
pub fn is_empty(&self) -> bool {
self.raw.is_empty()
}

/// Returns the [`PathPart`]s of this [`Path`]
pub fn parts(&self) -> PathParts<'_> {
PathParts::new(self)
}

/// Returns a copy of this [`Path`] with the last path segment removed
///
/// Returns `None` if this path has zero segments.
pub fn prefix(&self) -> Option<Self> {
let prefix = self.raw.rsplit_once(DELIMITER)?.1;

Some(Self {
raw: prefix.to_string(),
})
}

/// Returns the last path segment containing the filename stored in this [`Path`]
///
/// Returns `None` only if this path has zero segments.
pub fn filename(&self) -> Option<&str> {
match self.raw.is_empty() {
true => None,
Expand Down Expand Up @@ -343,18 +375,27 @@ impl std::fmt::Display for Path {
}
}

impl<'a, I: Into<PathPart<'a>>> Extend<I> for Path {
fn extend<T: IntoIterator<Item = I>>(&mut self, iter: T) {
for s in iter.into_iter() {
let s = s.into();
if s.raw.is_empty() {
continue;
}
self.raw.push_str(DELIMITER);
self.raw.push_str(&s.raw);
}
}
}

impl<'a, I> FromIterator<I> for Path
where
I: Into<PathPart<'a>>,
{
fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
let raw = T::into_iter(iter)
.map(|s| s.into())
.filter(|s| !s.raw.is_empty())
.map(|s| s.raw)
.join(DELIMITER);

Self { raw }
let mut this = Self::empty();
this.extend(iter);
this
}
}

Expand Down
41 changes: 40 additions & 1 deletion src/path/parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
// under the License.

use percent_encoding::{AsciiSet, CONTROLS, percent_encode};
use std::borrow::Cow;
use std::{
borrow::Cow,
iter::{self, FusedIterator},
str::SplitTerminator,
};

use crate::path::DELIMITER_BYTE;

Expand Down Expand Up @@ -131,6 +135,41 @@ impl AsRef<str> for PathPart<'_> {
}
}

/// See [`Path::parts`](super::Path::parts)
#[derive(Debug, Clone)]
pub struct PathParts<'a>(iter::Map<SplitTerminator<'a, char>, fn(&str) -> PathPart<'_>>);

impl<'a> PathParts<'a> {
/// Create an iterator over the parts of the provided [`Path`]
pub fn new(path: &'a super::Path) -> Self {
Self(
path.raw
.split_terminator(super::DELIMITER_CHAR)
.map(path_part_from_raw),
)
}
}

fn path_part_from_raw(s: &str) -> PathPart<'_> {
PathPart { raw: s.into() }
}

impl<'a> Iterator for PathParts<'a> {
type Item = PathPart<'a>;

fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}

impl<'a> FusedIterator for PathParts<'a> {}

impl<'a> DoubleEndedIterator for PathParts<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down