-
Notifications
You must be signed in to change notification settings - Fork 18
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
Support accessing the response code as an integer #70
Conversation
The `Status` enum only supports a portion of the response codes defined by RFC 9110. If the server returned a response code other than one of the ones supported by the `Status` enum, the code previously threw away the value. This updates the code to also store the code as an integer, so that users can handle other values. I considered simply changing the `Unknown` variant to use `Unknown(u16)`, but avoided it for now since this would break backwards compatibility, and would also prevent using `status as u16` to convert `Status` values to integers.
I would probably prefer an Other variant on the Status enum to avoid having two apis to convey the same information. |
Sure, I can do that instead if you prefer. I agree that it does make the API a bit nicer. That said, it does have some downsides since it breaks backwards compatibility, and makes the |
This replaces the `Status::Unknown` variant with a `Status::Other(n)` variant that can be used to represent arbitrary status codes. RFC 9110 defines a number of status codes that aren't currently specified as enum variants, and servers may return other arbitrary codes. This change allows `Status` to represent any arbitrary code, rather than collapsing unknown codes to a single value. This does break backwards compatibility, as the `Status` variant can no longer be trivial converted to a u16 (and it now requires more space to store). This also results in having more than one way to represent some status codes. e.g., `Status::Ok` and `Status::Other(200)` are both treated as equal now.
Yes, that can lead to subtle bugs, as every addition of a known status variant is effectively a breaking change. This is actually the reason why I have not already made the change:) @lulf what do you think? |
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.
It's unfortunate it requires breaking, but I also don't think there's been any promises made to not break the API, so might as well do it now. Maybe there should be a split where the StatusCode is just u16 wrapper, and used in the structs, whereas the Status enum is a convenience that can be Into in any input APIs.
The thing is that we will have a breaking change every time that a new known status code is added. Consider this code:
Another option would be to simply expose a |
Sorry for being unclear, I think we agree. This is what I meant in my previous comment as well, through a newtype StatusCode (u16) instead of u16. But using u16 directly is perhaps better. Having an get_status() to attempt the conversion makes sense 👍 |
Ahh yes! |
Sure, I can make those changes.
Did you perhaps mean I did initially take a stab at providing |
This changes the Response `status` field from the existing `Status` enum to a new `StatusCode(u16)` type. This allows arbitrary code values to be reported, including ones beyond the limited set represented by the `Status` enum. This is a breaking API change. For one common conversion case, Code using `match request.status {...}` to match against `Status` enum values will need to be changed to `match request.status.into() {...}`
Looks perfect, thank you! I think that you can merge yourself when you are ready. |
Thanks. It looks like the CI actions are stuck somehow though, so I don't think I can merge it yet. It's reporting the |
I re-ran the action, seemed to do the trick! |
The
Status
enum only supports a portion of the response codes defined by RFC 9110. If the server returned a response code other than one of the ones supported by theStatus
enum, the code previously threw away the value. This updates the code to also store the code as an integer, so that users can handle other values.I considered simply changing the
Unknown
variant to useUnknown(u16)
, but avoided it for now since this would break backwards compatibility, and would also prevent usingstatus as u16
to convertStatus
values to integers.