-
Notifications
You must be signed in to change notification settings - Fork 35
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
feat: Allow bounded numbers to be deserialized from strings #296
base: main
Are you sure you want to change the base?
Conversation
I didn't know that this kind of conversion is possible. |
Oh... sorry, I didn't read the #295 one. I see, so you are saying that this kind of conversion is necessary when HTTP query parameters are received, etc. |
All good! And yup - I'm also open to other ways to implement this too - this just seemed to be the best way to do it based on what I saw :) |
I have no objection to your implementation way. |
Okay, that is a reasonable concern! So I found an alternate mechanism to implement the functionality I needed. The specific requirements on us are: we must implement I've confirmed that this works via testing, but I have not checked in those tests because they would introduce a dev dependency on |
134934b
to
ecaaead
Compare
Hrm. So the tests I write pass in isolation, but For posterity, here is the test I have written that passes: #[test]
fn query_deserialize() {
#[derive(Deserialize)]
struct S {
#[serde(skip_serializing_if = "core::option::Option::is_none")]
pub cursor: core::option::Option<String>,
#[serde(skip_serializing_if = "core::option::Option::is_none")]
pub limit: core::option::Option<crate::types::LimitedNonZeroU16<1000u16>>,
}
let s = serde_urlencoded::from_str::<S>("cursor=abcd&limit=100").unwrap();
assert_eq!(s.limit.unwrap().0, NonZeroU16::new(100).unwrap());
} Edit: Okay, it appears that we should not be wrapping query parameters in This is a separate issue that I'm not going to fix in this pull request :) |
Thank you for continuing to work on this issue. |
So it's actually expected that For json, everything is strongly typed enough that they do not need to implement implicit conversions. TY for the pull request btw! |
|
||
fn from_str(src: &str) -> Result<Self, Self::Err> { | ||
Self::new( | ||
src.parse::<$primitive>().map_err(|_| format!("value is not an integer"))?, |
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'm thinking .map_err(|e| e.to_string())
would be fine, but what do you think?
} | ||
} | ||
// TODO: LimitedNonZeroU8 | ||
// TODO: BoundedU8 |
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've written tests only for LimitedU8
, I would be glad if you could add tests for other types as well. If you decide that the current ones are sufficient, you can just delete the // TODO
.
Fixes #295.
This allows libraries like
serde-html-form
to deserialize bounded integers from strings.