In an effort to avoid some code duplication, I'm trying to move validation closer to the parser. My goal is relatively simple: Allow a string or a number, but if a number is given, only allow values 1-32.
It turns out post-process validation has been been on serde wish-lists for a while, and there's an open issue for it.
The actual issue post contains an example for how to parameter validation can be accomplished using deserialize_with. This works fine, apart that the specific error message gets "lost". I assume it's because the serde just iterates over the enum variants and tries to find a match, and it considers any error it encounters along the way to just mean "doesn't match this variant".
Is there a way to solve this? I.e. support both string and integer values, validate the integer, and return a specific error out-of-bounds error if the integer is out of bounds, and have that error reported back to the user?
You could create an enum with 32 variants and use serde_repr.
Nvm., this is ill-advised. I was just trying to think in ways to avoid writing a custom deserializer, but that's not the way to solve this specific problem.
To be fair, that's sort of what I was looking for -- but @paramagnetic's reply showed that it was far simpler than I had imagined, so I'll go for that.