pub fn heterogeneous_map<K, S>(
d: &mut Decoder<'_>,
state: S,
decode_key: impl Fn(&mut Decoder<'_>) -> Result<K, Error>,
decode_value: impl FnMut(&mut Decoder<'_>, &mut S, K) -> Result<(), Error>,
) -> Result<S, Error>Expand description
Decode any heterogeneous CBOR map, irrespective of whether they’re indefinite or definite.
A good choice for S is generally to pick a tuple of Option for each field item
that needs decoding. For example:
let (address, value, datum, script) = decode_map(
d,
(None, None, MemoizedDatum::None, None),
|d| d.u8(),
|d, state, field| {
match field {
0 => state.0 = Some(decode_address(d.bytes()?),
1 => state.1 = Some(d.decode()?),
2 => state.2 = decode_datum()?,
3 => state.3 = decode_reference_script()?,
_ => return unexpected_field::<Output, _>(field),
}
Ok(())
},
)?;