Skip to content

Commit

Permalink
Add uri::{Rsync,Https}::path_into_dir. (#302)
Browse files Browse the repository at this point in the history
This PR adds a method to ensure that a URI ends in a slash.
  • Loading branch information
partim authored Jul 9, 2024
1 parent 8e71299 commit 0996832
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ impl Rsync {
&self.bytes[self.path_start..]
}

/// Converts the URI into one representing a directory.
///
/// If the path does not end in a slash or is not empty, appends a slash
/// to it.
pub fn path_into_dir(&mut self) {
if !self.path_is_dir() {
let mut new_bytes = Vec::with_capacity(self.bytes.len() + 1);
new_bytes.extend_from_slice(&self.bytes);
new_bytes.push(b'/');
self.bytes = new_bytes.into();
}
}

/// Returns the parent URI.
///
/// The parent URI is the URI with the last path segment removed. If a
Expand Down Expand Up @@ -598,6 +611,24 @@ impl Https {
&self.as_str()[self.path_idx..]
}

/// Returns whether the URI's path resolves to a directory.
pub fn path_is_dir(&self) -> bool {
self.path().is_empty() || self.path().ends_with('/')
}

/// Converts the URI into one representing a directory.
///
/// If the path does not end in a slash or is not empty, appends a slash
/// to it.
pub fn path_into_dir(&mut self) {
if !self.path_is_dir() {
let mut new_bytes = Vec::with_capacity(self.uri.len() + 1);
new_bytes.extend_from_slice(&self.uri);
new_bytes.push(b'/');
self.uri = new_bytes.into();
}
}

/// This function will join this URI and the given path. If the current
/// URI does not end with a trailing '/', it will be injected.
pub fn join(&self, path: &[u8]) -> Result<Self, Error> {
Expand Down

0 comments on commit 0996832

Please sign in to comment.