-
-
Notifications
You must be signed in to change notification settings - Fork 374
add an "append_path" function to url #934
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2637,6 +2637,40 @@ impl Url { | |
| Err(()) | ||
| } | ||
|
|
||
| /// Append path segments to the path of a Url, escaping if necesary. | ||
| /// | ||
| /// This differs from Url::join in that it is insensitive to trailing slashes | ||
| /// in the url and leading slashes in the passed string. See documentation of Url::join for discussion | ||
| /// of this subtlety. Also, this function cannot change any part of the Url other than the path. | ||
| /// | ||
| /// Examples: | ||
| /// | ||
| /// ``` | ||
| /// # use url::Url; | ||
| /// let mut my_url = Url::parse("http://www.example.com/api/v1").unwrap(); | ||
| /// my_url.append_path("system/status").unwrap(); | ||
| /// assert_eq!(my_url.as_str(), "http://www.example.com/api/v1/system/status"); | ||
| /// ``` | ||
| /// | ||
| /// Fails if the Url is cannot-be-a-base. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add # Errors
Fails if the Url is cannot-be-a-base. |
||
| #[allow(clippy::result_unit_err)] | ||
| #[inline] | ||
| pub fn append_path(&mut self, path: impl AsRef<str>) -> Result<(), ()> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be nice if this method could take There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specifically I propose renaming this method to something like |
||
| // This fails if self is cannot-be-a-base but succeeds otherwise. | ||
| let mut path_segments_mut = self.path_segments_mut()?; | ||
|
|
||
| // Remove the last segment if it is empty, this makes our code tolerate trailing `/`'s | ||
| path_segments_mut.pop_if_empty(); | ||
|
|
||
| // Remove any leading `/` from the path we are appending, this makes our code tolerate leading `/`'s | ||
| let path = path.as_ref(); | ||
| let path = path.strip_prefix('/').unwrap_or(path); | ||
| for segment in path.split('/') { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this splitting is based on similar code elsewhere in the library: https://docs.rs/url/2.5.0/src/url/lib.rs.html#1351 |
||
| path_segments_mut.push(segment); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| // Private helper methods: | ||
|
|
||
| #[inline] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo:
necesaryshould benecessary