Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd examples Url methods #309 #340
Conversation
This comment has been minimized.
This comment has been minimized.
|
cc @dtolnay and @SimonSapin |
dtolnay
reviewed
May 14, 2017
| @@ -861,6 +886,20 @@ impl Url { | |||
| } | |||
|
|
|||
| /// Return this URL’s query string, if any, as a percent-encoded ASCII string. | |||
This comment has been minimized.
This comment has been minimized.
| /// | ||
| /// let url = Url::parse("https://example.com/api/versions?page=2").unwrap(); | ||
| /// | ||
| /// assert_eq!(url.path(), "/api/versions"); |
This comment has been minimized.
This comment has been minimized.
dtolnay
May 14, 2017
I think this would be more readable at a glance if the asserts were grouped with the previous line:
use url::Url;
let url = Url::parse("https://example.com/api/versions?page=2").unwrap();
assert_eq!(url.path(), "/api/versions");
let url = Url::parse("https://example.com").unwrap();
assert_eq!(url.path(), "/");| /// use url::Url; | ||
| /// | ||
| /// let parse_options = Url::options(); | ||
| /// let base = parse_options.parse("https://example.net/api/").unwrap(); |
This comment has been minimized.
This comment has been minimized.
dtolnay
May 14, 2017
Please come up with an example that requires ParseOptions. This one would just use Url::parse.
| /// { | ||
| /// url.set_fragment(Some("cell=4,1-6,2")); | ||
| /// } | ||
| /// assert_eq!(url.fragment(), Some("cell=4,1-6,2")); |
This comment has been minimized.
This comment has been minimized.
dtolnay
May 14, 2017
Please add an assert on url.as_str() after each of these to show what the full url looks like.
| /// { | ||
| /// url.set_query(Some("page=2")); | ||
| /// } | ||
| /// assert_eq!(url.query(), Some("page=2")); |
This comment has been minimized.
This comment has been minimized.
| /// use url::Url; | ||
| /// | ||
| /// let mut url = Url::parse("http://example.com").unwrap(); | ||
| /// { |
This comment has been minimized.
This comment has been minimized.
| /// { | ||
| /// url.set_ip_host("127.0.0.1".parse().unwrap()); | ||
| /// } | ||
| /// assert_eq!(url.host_str(), Some("127.0.0.1")); |
This comment has been minimized.
This comment has been minimized.
| /// | ||
| /// let mut url = Url::parse("mailto:rms@example.com").unwrap(); | ||
| /// let result = url.set_ip_host("127.0.0.1".parse().unwrap()); | ||
| /// assert!(result.is_err()); |
This comment has been minimized.
This comment has been minimized.
dtolnay
May 14, 2017
Since the error type isn't meaningful, please add a brief comment here reminding why this is an error.
| /// | ||
| /// let mut url = Url::parse("mailto:rmz@example.com").unwrap(); | ||
| /// let result = url.set_username("user1"); | ||
| /// assert!(result.is_err()); |
This comment has been minimized.
This comment has been minimized.
| /// let mut url = Url::parse("ftp://:secre1@example.com").unwrap(); | ||
| /// let result = url.set_username("user1"); | ||
| /// assert!(result.is_ok()); | ||
| /// assert_eq!(url.username(), "user1"); |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
dtolnay
commented
May 14, 2017
|
Nice work. One more comment that applies to all of these: the examples should show realistic error handling. In real code you probably would not want any of these |
This comment has been minimized.
This comment has been minimized.
|
Ping @hngnaig |
This comment has been minimized.
This comment has been minimized.
|
I just fix code, but i missed test for method |
This comment has been minimized.
This comment has been minimized.
|
I need help about function use url::Url;
let url = Url::parse("https://example.com/foo/bar/baz").unwrap();
assert_eq!(url.path(), "/foo/bar/baz");I didn't really understand what it is. I think i should assert as bellow. use url::Url;
use url::percent_encoding;
let url = Url::parse("https://example.com/foo%21bar%21baz").unwrap();
let encoding_path = percent_encoding::percent_encode(b"/foo/bar/baz", PATH_SEGMENT_ENCODE_SET);
let path_str = encoding_path.collect::<String>();
assert_eq!(url.path(), path_str);Thanks for help me. |
SimonSapin
reviewed
Jun 13, 2017
| /// | ||
| /// # Examples | ||
| /// | ||
| /// Get default `ParseOption`, then change base url |
This comment has been minimized.
This comment has been minimized.
| /// | ||
| /// let options = Url::options(); | ||
| /// let api = Url::parse("https://api.example.com").ok(); | ||
| /// let base_url = options.base_url(api.as_ref()); |
This comment has been minimized.
This comment has been minimized.
SimonSapin
Jun 13, 2017
Member
Please replace .ok() with .unwrap() and api.as_ref() with Some(&api), to show more typical usage.
This comment has been minimized.
This comment has been minimized.
Enet4
Jun 16, 2017
Contributor
Actually, according to the other examples (and as highlighted by @dtolnay), we should not be using unwrap() either. Let's use ? instead, thus forcing users to handle the errors properly.
This comment has been minimized.
This comment has been minimized.
|
@hngnaig Your examples for let url = Url::parse("https://example.com/Été hiver/").unwrap();
assert_eq!(url.path(), "/%C3%89t%C3%A9%20hiver");No need to use |
This comment has been minimized.
This comment has been minimized.
|
Your However, I’m not sure how it happened, but this PR is now adding 115 new commits. Please squash them into one commit. (Unless you think it’s valuable to split your changes into multiple commits, but they should not duplicate unrelated changes.) As an aside, I recommend this git alias to visualize the commit graph, I use it a lot. In [alias]
graph = log --oneline --graph --all --decorateNow the Now, the usual way to squash is
|
This comment has been minimized.
This comment has been minimized.
|
Thanks @SimonSapin for helping me. I squashed all commit, and reset them at the last commit d19d5d. However, there are still errors, I'm trying to check the code and fix the errors. |
This comment has been minimized.
This comment has been minimized.
|
I can take over the git wrangling if you’d like. Let me know. |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
@hngnaig Unfortunately your PR still contains merge commits and isn't properly squashed. |
This comment has been minimized.
This comment has been minimized.
|
It also has unrelated whitespace changes in |
This comment has been minimized.
This comment has been minimized.
|
cc @KiChjang, @SimonSapin i squashed all commit, and remove all whitespace in |
Enet4
reviewed
Jun 21, 2017
| /// | ||
| /// # fn run() -> Result<(), ParseError> { | ||
| /// let mut url = Url::parse("http://example.com")?; | ||
| /// url.set_ip_host("127.0.0.1".parse().unwrap()); |
This comment has been minimized.
This comment has been minimized.
Enet4
Jun 21, 2017
•
Contributor
Is this unwrap acceptable here (I'm asking because 127.0.0.1 is guaranteed to work)? If not, we could change the return type to Result<(), Box<Error>> and map errors into a &'static str.
This comment has been minimized.
This comment has been minimized.
ghost
Jun 21, 2017
Author
I think it's acceptable because 127.0.0.1 is valid static ip address. But it will be much better if change return type to Result<(), Box<Error>>.
fn run() -> Result<(), Box<Error>> {
url.set_ip_host("127.0.0.1".parse()?);
}
This comment has been minimized.
This comment has been minimized.
|
Thanks you! @bors-servo r+ Reviewed 1 of 1 files at r3. src/lib.rs, line 1605 at r3 (raw file): Previously, Enet4 (Eduardo Pinho) wrote…
In normal code I think it’s fine to use Comments from Reviewable |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
bors-servo
added a commit
that referenced
this pull request
Jun 21, 2017
This comment has been minimized.
This comment has been minimized.
|
|
ghost commentedMay 14, 2017
•
edited by SimonSapin
Fix #309
This change is