json_macros icon indicating copy to clipboard operation
json_macros copied to clipboard

JSON pattern macro

Open fenhl opened this issue 10 years ago • 3 comments
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.

fenhl avatar Mar 18 '15 22:03 fenhl

Yes! I've wanted this since I started writing the library. Thanks for opening the issue.

tomjakubowski avatar Mar 18 '15 23:03 tomjakubowski

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.

lambda-fairy avatar Apr 03 '15 08:04 lambda-fairy

Good point.

fenhl avatar Apr 03 '15 08:04 fenhl