-
Notifications
You must be signed in to change notification settings - Fork 145
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
Introduces the FromQuery and IntoQuery traits. #364
Conversation
I am not too concerned about sealing History as the ultimate goal is to have a way to provide This is a We have a pending minor release for the next pull request already, i.e.: bumping the MSRV. |
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.
Could you please update changelog.md
, including your change?
c021711
to
73b26c6
Compare
cc2faf0
to
100e725
Compare
I wonder if there is a way to use |
100e725
to
64ada3b
Compare
Do you mean something like extracting key-value pairs from a |
I mean the opposite of that: using serde to serialize into a URLSearchParams |
That should be possible, actually. Although, you would probably not want to couple it so strongly. So ideally, you'd want something that serializes into a |
I 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.
Thanks! LGTM.
I am approving this pull request as I do not have more comments with the current implementation and we can proceed with it after the change log is updated.
These two traits are introduced to allow for customizing the query encoding and decoding behaviour by implementing these traits for custom types. The previous behaviour of using serde_urlencoded for anything that implements Serialize and Deserialize is preserved. Customt wrapper types are added which modify the encoding and decoding behaviour. An optional feature named `query-qs` is introduced which allows for optionally encoding and decoding query strings using serde_qs.
64ada3b
to
4436c69
Compare
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.
Thanks for making this change!
Introduces the
FromQuery
andIntoQuery
traits to allow for customizing how query strings are encoded and decoded. Introduces theBecause this PR introduces quite a few new things, allow me to walk through it an explain what is happening here.
Query Encoding
Previous Behaviour
The
History
trait has some methods that allow specifying a query parameter. This query parameter must implementSerialize
. It is encoded usingserde_urlencoded
. These return aHistoryResult<()>
, which expands to aResult<(), HistoryError>
.New Behaviour
We introduce a helper trait,
IntoQuery
, to help us encode any arbitrary type that implements this into a query string. It has a configurable error (because different strategies might fail in different ways) and a method to encode that takes&self
.We also add a blanket implementation of this type for any type that implements
Serialize
with theError
set toHistoryError
.Now, we change the methods in the
History
trait to constrain on anything that implementsIntoQuery
instead ofSerialize
. Since we have a blanket implementation ofIntoQuery
for anything that implementsSerialize
, these two constraints are identical at this point. These now return aIntoQuery::Error
as an error instead.Before, it was only possible to call these methods with
Q: Serialize
and it returned aResult<(), HistoryError>
. Now, calling it with any type that isQ: Serialize
is still possible, and returns the same value. However, it is now possible to implementIntoQuery
manually to override an encoding scheme. This is done with theQs
type and theRaw
type.Breaking Changes
Because the new
History
trait API is a superset of the previous API, this does not constitute a breaking change for consumers of this trait. It does however change the trait, which means that anyone who manually implements theHistory
trait for a custom type (I don't know why anyone would do that, but you could) needs to adjust their implementation slightly. It might be worth looking into sealing the History trait for future-proofing it, otherwise any change, even addition of a method, is a potential (hypothetical) breaking change.Query Decoding
Previous Behaviour
Query decoding happens in the
Location
struct, which has aquery
method.New Behaviour
We introduce a trait to capture the idea of decoding a query string. This has a
Target
type parameter, so that aMyCustomStrategy
can decode into aString
if it wanted to, and anError
parameter, as well as adecode()
method.Again, we add a blanket implementation for any T that implements
DeserializeOwned
.With this, we change the method to return a
T::Target
and anT::Error
instead.With this change, all previous invocations still work the exact same way. However, we can now implement
FromQuery
for custom types to use a custom decoding strategy.Breaking Changes
Since this is not a trait, and the new definition is a superset of the old, this does not constitute a breaking change.
Tests
Tests have been added for the traits. The test coverage exceeds 80%.
Documentation
Documentation has been added, however it could still be improved with more examples.