[−][src]Macro forest_ipld::ipld
Construct a forest_ipld::Ipld
roughly matching JSON format. This code is a modified version
of serde_json::json
macro, with extensions for being able to indicate links and bytes.
These two matterns can be matched by wrapping Cid
link in Link(..)
or the Vec<u8>
in
Bytes()
.
let value = ipld!({ "code": 200, "success": true, "link": Link("QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n".parse().unwrap()), "bytes": Bytes(vec![0x1, 0xfa, 0x8b]), "payload": { "features": [ "serde", "ipld" ] } });
Variables or expressions can be interpolated into the JSON literal. Any type
interpolated into an array element or object value must implement Serde's
Serialize
trait, while any type interpolated into a object key must
implement Into<String>
. If the Serialize
implementation of the
interpolated type decides to fail, or if the interpolated type contains a
map with non-string keys, the ipld!
macro will panic.
let code = 200; let features = vec!["serde", "ipld"]; let value = ipld!({ "code": code, "success": code == 200, "payload": { features[0]: features[1] } });
Trailing commas are allowed inside both arrays and objects.
let value = ipld!([ "notice", "the", "trailing", "comma -->", ]);