Skip to content
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

Mime: add From<&str> #179

Merged
merged 1 commit into from
Jul 11, 2020
Merged

Conversation

Fishrock123
Copy link
Member

@@ -139,6 +139,12 @@ impl FromStr for Mime {
}
}

impl<'a> From<&'a str> for Mime {
fn from(value: &'a str) -> Self {
Self::from_str(value).unwrap()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use expect here instead? Probably copy over the message from FromStr's error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow - FromStr here doesn't have an error message itself, the message comes directly from parse, which would probably provide a more useful error than this would.

I suppose this may actually be better as TryFrom, but that does mean updating Tide internals to support it, and also, what would it do with such an error anyways?

I'm actually having a bit of 2nd thoughts on this, given our panic/internal unwrap discussion the other day -- do we really want to encourage people to write e.g. res.set_content_type("application/json")?

Copy link
Member

@yoshuawuyts yoshuawuyts Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really want to encourage people to write (...)

That's the idea; we already support this for status codes, headers, urls, and more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant, shouldn't we encourage people to use res.set_content_type(mime::TYPE), res.set_header(headers::HEADER, ...) instead, since that is more guaranteed to not have a name parsing panic?

Copy link
Member Author

@Fishrock123 Fishrock123 Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That aside though, what would expect() say that wouldn't already be more detailed from parse?

@Fishrock123
Copy link
Member Author

@yoshuawuyts I don't know how this can be improved aside from making it TryFrom. The parse error will always contain the amount of detail the user would actual need, and .expect() would obscure that?

@Fishrock123 Fishrock123 merged commit 1716bc9 into http-rs:master Jul 11, 2020
@Fishrock123 Fishrock123 deleted the mime-from-str branch July 11, 2020 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants