feat(serde_with_macros): proper handling of cfg_attr
and schemars
#782
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With this commit,
cfg_attr
will be handled properly. The current implementation does not accept e.g.:Because due to https://github.com/jonasbb/serde_with/blob/master/serde_with_macros/src/utils.rs#L91, the parser does not accept
cfg_attr
with multiple arguments, which is allowed, see e.g. https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute. Another issue I’ve found witharbitrary
where some unexpected errors due to this custom parser: https://github.com/jonasbb/serde_with/blob/master/serde_with_macros/src/utils.rs#L191-L218 in combination with this line: https://github.com/jonasbb/serde_with/blob/master/serde_with_macros/src/utils.rs#L240.Line 240 will only
continue
, if the outer-most meta if neithercfg_attr
orschemars
. If however the attribute iscfg_attr
, the code after thematch
-block is reached and the parser will just assume, that the inner meta isschemars
and thus fail to parse any meta with some weird syntax.Even
#[schemars(length(min = 1.0))]
will fail, because the parser expects allschemars
-attributes to be a list of key-value-pairs (foo = bar
), thus failing forlength(min = 1.0)
, even though this is perfectly legal (https://graham.cool/schemars/deriving/attributes/#length).I introduced the type
LazyBool
to simplify dealing with various conditions.