-
Notifications
You must be signed in to change notification settings - Fork 300
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
StatusCode enum #1
Comments
I think I like the idea of using an opaque struct, perhaps with convenient constructors like |
I tend to prefer opaque structs as well, though an enum has advantages here. I'm leaning towards an opaque struct. It has the advantage of being able to be forwards compatible. Helper fns can be added as @alexcrichton pointed out. One option to consider to improve the readability is to add constants in the if status == status:: Created {
// ...
} Re: Re: |
Yea, I've been leaning towards an opaque struct as well. Perhaps with each current variant translated into a constructor function, like It also makes sense to ditch StatusClass, and just have the status checking methods on StatusCode. The only thing lost is ergonomic matches. The left side of a match arm can't use functions, and when using constants, it's far easier to mistype a constant name and actually have just a renamed catch pattern. With the idea of using associated constants, users could probably still match on something like |
I personally feel like that's an acceptable tradeoff. The purpose here is to provide a maximally implementation flexible (and extensible) type into the future, not to be the most ergonomic to match on (which is also the most restrictive from an implementor's point of view) |
I have a working example of much of this in: https://github.com/seanmonstar/http-rs/blob/6059d2ba8633d9478ea173718b155731b55df07a/src/status.rs Shortened gist is: pub struct StatusCode(u16);
impl StatusCode {
// ... constructors for every registered status
pub fn ok() -> StatusCode {
StatusCode(200)
}
// ... and some methods
pub fn is_success(&self) -> bool {
300 > self.0 && self.0 >= 200
}
} There was 1 conflict when defined the |
I'd also be fine with something like |
I also don't think there needs to be function constructors for all status codes, maybe only the common ones. You can always construct it w/ the actual number:
|
I personally find the constructor names more beneficial for obscure status codes. I prefer Follow up question is if there should be a |
Closed by #6 |
Add rocket support
There's a couple of questions, maybe they should be broken out into separate issues, but I'll start by listing them here:
Should it even be an
enum
? There is theStatusCode::Unregistered(u16)
variant, since really any number between 100 and 599 is a valid status code. The reason for it being an enum originally in hyper is to take advantage ofmatch
statements.if let StatusCode::Created = status
reads better thanif status == 201
in my opinion. The problem with is being an enum is that it's a breaking change any time a new status code is defined, and we want to name it.I had considered for a bit having a newtype around
u16
, but the associated constants feature is required to make it complete. It could look like:Does the
StatusClass
pull its own weight? The various specs declare that the first digit of the code determines its category or class. hyper has a StatusClass enum that allows anyStatusCode
to be check for its class. As mentioned in RFC7321, if a client doesn't recognize a code, it should still be able to recognize its class, such as471
being treated as a client error, like 400.Should converting from a primitive depend on the unstable
TryFrom
feature? Since the specs say that a client should always be able to determine a status class from a code, and there are only classes defined in the specs for 1xx, 2xx, 3xx, 4xx, and 5xx, it can be implied that any other status code is invalid or illegal. Does it make sense then that converting from au16
to aStatusCode
is a fallible action? If so, the best case to do that is probablyTryFrom
, which is currently unstable, but looks super close to stabilization.The text was updated successfully, but these errors were encountered: