-
Notifications
You must be signed in to change notification settings - Fork 345
Add examples Url methods #309 #340
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
Conversation
cc @dtolnay and @SimonSapin |
@@ -861,6 +886,20 @@ impl Url { | |||
} | |||
|
|||
/// Return this URL’s query string, if any, as a percent-encoded ASCII string. |
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.
Let's show a third example where percent-encoding is necessary.
src/lib.rs
Outdated
/// | ||
/// let url = Url::parse("https://example.com/api/versions?page=2").unwrap(); | ||
/// | ||
/// assert_eq!(url.path(), "/api/versions"); |
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.
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(), "/");
src/lib.rs
Outdated
/// use url::Url; | ||
/// | ||
/// let parse_options = Url::options(); | ||
/// let base = parse_options.parse("https://example.net/api/").unwrap(); |
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.
Please come up with an example that requires ParseOptions. This one would just use Url::parse
.
src/lib.rs
Outdated
/// { | ||
/// url.set_fragment(Some("cell=4,1-6,2")); | ||
/// } | ||
/// assert_eq!(url.fragment(), Some("cell=4,1-6,2")); |
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.
Please add an assert on url.as_str()
after each of these to show what the full url looks like.
src/lib.rs
Outdated
/// { | ||
/// url.set_query(Some("page=2")); | ||
/// } | ||
/// assert_eq!(url.query(), Some("page=2")); |
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.
Please add an assert on url.as_str()
after this.
src/lib.rs
Outdated
/// use url::Url; | ||
/// | ||
/// let mut url = Url::parse("http://example.com").unwrap(); | ||
/// { |
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.
This nesting is not needed.
src/lib.rs
Outdated
/// { | ||
/// url.set_ip_host("127.0.0.1".parse().unwrap()); | ||
/// } | ||
/// assert_eq!(url.host_str(), Some("127.0.0.1")); |
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.
Please add an assert on url.as_str()
after this.
src/lib.rs
Outdated
/// | ||
/// let mut url = Url::parse("mailto:[email protected]").unwrap(); | ||
/// let result = url.set_ip_host("127.0.0.1".parse().unwrap()); | ||
/// assert!(result.is_err()); |
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.
Since the error type isn't meaningful, please add a brief comment here reminding why this is an error.
src/lib.rs
Outdated
/// | ||
/// let mut url = Url::parse("mailto:[email protected]").unwrap(); | ||
/// let result = url.set_username("user1"); | ||
/// assert!(result.is_err()); |
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.
Please add a comment saying why this is an error.
src/lib.rs
Outdated
/// let mut url = Url::parse("ftp://:[email protected]").unwrap(); | ||
/// let result = url.set_username("user1"); | ||
/// assert!(result.is_ok()); | ||
/// assert_eq!(url.username(), "user1"); |
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.
Please add an assert on url.as_str()
.
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 |
Ping @hngnaig |
I just fix code, but i missed test for method |
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. |
src/lib.rs
Outdated
/// | ||
/// # Examples | ||
/// | ||
/// Get default `ParseOption`, then change base url |
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.
ParseOptions
is plural. (s
missing.)
src/lib.rs
Outdated
/// | ||
/// let options = Url::options(); | ||
/// let api = Url::parse("https://api.example.com").ok(); | ||
/// let base_url = options.base_url(api.as_ref()); |
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.
Please replace .ok()
with .unwrap()
and api.as_ref()
with Some(&api)
, to show more typical usage.
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.
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.
@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 |
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 --decorate Now the Now, the usual way to squash is
|
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. |
I can take over the git wrangling if you’d like. Let me know. |
💯 After trying to fix the error, pull request has been completely done, cc @SimonSapin 💯 |
@hngnaig Unfortunately your PR still contains merge commits and isn't properly squashed. |
It also has unrelated whitespace changes in |
cc @KiChjang, @SimonSapin i squashed all commit, and remove all whitespace in |
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let mut url = Url::parse("http://example.com")?; | ||
/// url.set_ip_host("127.0.0.1".parse().unwrap()); |
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.
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
.
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.
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()?);
}
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 |
📌 Commit dd1f232 has been approved by |
Add examples Url methods #309 Fix #309 <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/340) <!-- Reviewable:end -->
☀️ Test successful - status-travis |
Fix #309
This change is