Skip to content

Add PathSegmentsMut::finish #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
44 changes: 34 additions & 10 deletions src/path_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<'a> Drop for PathSegmentsMut<'a> {
impl<'a> PathSegmentsMut<'a> {
/// Remove all segments in the path, leaving the minimal `url.path() == "/"`.
///
/// Returns `&mut Self` so that method calls can be chained.
/// Returns `mut Self` so that method calls can be chained.
///
/// Example:
///
Expand All @@ -80,7 +80,7 @@ impl<'a> PathSegmentsMut<'a> {
/// # }
/// # run().unwrap();
/// ```
pub fn clear(&mut self) -> &mut Self {
pub fn clear(mut self) -> Self {
self.url.serialization.truncate(self.after_first_slash);
self
}
Expand All @@ -91,7 +91,7 @@ impl<'a> PathSegmentsMut<'a> {
/// In other words, remove one path trailing slash, if any,
/// unless it is also the initial slash (so this does nothing if `url.path() == "/")`.
///
/// Returns `&mut Self` so that method calls can be chained.
/// Returns `mut Self` so that method calls can be chained.
///
/// Example:
///
Expand All @@ -113,7 +113,7 @@ impl<'a> PathSegmentsMut<'a> {
/// # }
/// # run().unwrap();
/// ```
pub fn pop_if_empty(&mut self) -> &mut Self {
pub fn pop_if_empty(mut self) -> Self {
if self.url.serialization[self.after_first_slash..].ends_with('/') {
self.url.serialization.pop();
}
Expand All @@ -124,8 +124,8 @@ impl<'a> PathSegmentsMut<'a> {
///
/// If the path only has one segment, make it empty such that `url.path() == "/"`.
///
/// Returns `&mut Self` so that method calls can be chained.
pub fn pop(&mut self) -> &mut Self {
/// Returns `mut Self` so that method calls can be chained.
pub fn pop(mut self) -> Self {
let last_slash = self.url.serialization[self.after_first_slash..].rfind('/').unwrap_or(0);
self.url.serialization.truncate(self.after_first_slash + last_slash);
self
Expand All @@ -135,8 +135,9 @@ impl<'a> PathSegmentsMut<'a> {
///
/// See the documentation for `.extend()`.
///
/// Returns `&mut Self` so that method calls can be chained.
pub fn push(&mut self, segment: &str) -> &mut Self {
/// Returns `mut Self` so that method calls can be chained.
#[allow(unused_mut)]
pub fn push(mut self, segment: &str) -> Self {
self.extend(Some(segment))
}

Expand All @@ -156,7 +157,7 @@ impl<'a> PathSegmentsMut<'a> {
///
/// To obtain a behavior similar to `Url::join`, call `.pop()` unconditionally first.
///
/// Returns `&mut Self` so that method calls can be chained.
/// Returns `mut Self` so that method calls can be chained.
///
/// Example:
///
Expand Down Expand Up @@ -193,7 +194,7 @@ impl<'a> PathSegmentsMut<'a> {
/// # }
/// # run().unwrap();
/// ```
pub fn extend<I>(&mut self, segments: I) -> &mut Self
pub fn extend<I>(mut self, segments: I) -> Self
where I: IntoIterator, I::Item: AsRef<str> {
let scheme_type = SchemeType::from(self.url.scheme());
let path_start = self.url.path_start as usize;
Expand All @@ -214,4 +215,27 @@ impl<'a> PathSegmentsMut<'a> {
});
self
}

/// Returns the URL reference the PathSegmentsMut was constructed with.
///
/// Example:
///
/// ```rust
/// use url::Url;
/// # use std::error::Error;
///
/// # fn run() -> Result<(), Box<Error>> {
/// let mut url = Url::parse("https://github.com/servo/rust-url/issues/363")?;
/// url.path_segments_mut()
/// .map_err(|_| "cannot be base")?
/// .clear()
/// .finish();
/// assert_eq!(url.as_str(), "https://github.com/");
/// # Ok(())
/// # }
/// # run().unwrap();
/// ```
pub fn finish(self) -> &'a mut ::Url {
self.url
}
}
22 changes: 19 additions & 3 deletions tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ fn issue_241() {
fn append_trailing_slash() {
let mut url: Url = "http://localhost:6767/foo/bar?a=b".parse().unwrap();
url.check_invariants().unwrap();
url.path_segments_mut().unwrap().push("");
url.path_segments_mut().unwrap().push("").finish();
url.check_invariants().unwrap();
assert_eq!(url.to_string(), "http://localhost:6767/foo/bar/?a=b");
}
Expand All @@ -343,7 +343,7 @@ fn extend_query_pairs_then_mutate() {
url.query_pairs_mut().extend_pairs(vec![ ("auth", "my-token") ].into_iter());
url.check_invariants().unwrap();
assert_eq!(url.to_string(), "http://localhost:6767/foo/bar?auth=my-token");
url.path_segments_mut().unwrap().push("some_other_path");
url.path_segments_mut().unwrap().push("some_other_path").finish();
url.check_invariants().unwrap();
assert_eq!(url.to_string(), "http://localhost:6767/foo/bar/some_other_path?auth=my-token");
}
Expand All @@ -353,7 +353,7 @@ fn extend_query_pairs_then_mutate() {
fn append_empty_segment_then_mutate() {
let mut url: Url = "http://localhost:6767/foo/bar?a=b".parse().unwrap();
url.check_invariants().unwrap();
url.path_segments_mut().unwrap().push("").pop();
url.path_segments_mut().unwrap().push("").pop().finish();
url.check_invariants().unwrap();
assert_eq!(url.to_string(), "http://localhost:6767/foo/bar?a=b");
}
Expand Down Expand Up @@ -452,6 +452,22 @@ fn test_origin_hash() {
assert_ne!(hash(&opaque_origin), hash(&other_opaque_origin));
}

#[test]
// https://github.com/servo/rust-url/issues/363
fn chain_path_segments_query_pair() {
let mut url = Url::parse("https://github.com/servo/rust-url/issues/363").unwrap();

url.path_segments_mut()
.expect("path_segments_mut")
.clear()
.finish()
.query_pairs_mut()
.finish();

/// The ? at the end is added by query_pairs_mut
assert_eq!(url.as_str(), "https://github.com/?");
}

#[test]
fn test_windows_unc_path() {
if !cfg!(windows) {
Expand Down