json_macros
json_macros copied to clipboard
JSON pattern macro
trafficstars
Feature request: a macro which works like json!, but in a pattern-matching context. Parentheses could be used to insert Rust's regular pattern matching. Example:
if let json_pat!({"isFoo": (Json::Boolean(flag))}) = json_blob {
// matches {"isFoo": true} and {"isFoo": false}, and binds the value of the "isFoo" key to `flag`
if flag { frob(); }
}
For additional convenience, it might also be useful if identifier bindings and the _ and .. syntaxes were available without parentheses.
Yes! I've wanted this since I started writing the library. Thanks for opening the issue.
Note that the syntax extension API tells us whether we're in an expression or pattern context already, so we can call the pattern macro json! as well.
Good point.