Skip to main content

decode

Function decode 

Source
pub fn decode(block: &[u8]) -> Result<Value, CborError>
Expand description

Decodes one block into the data model.

The block is one value and nothing after it, which is what a content-addressed block is: bytes that hash to a name. Trailing bytes are an error rather than an ignored tail, because they change the hash while leaving the value alone.

use didbot_data::{dag_cbor, Value};

let value = Value::from_json(&serde_json::json!({"a": 1})).unwrap();
let block = dag_cbor::encode(&value);
assert_eq!(dag_cbor::decode(&block).unwrap(), value);

// 5 written in two bytes rather than one is not a block.
assert!(dag_cbor::decode(&[0x18, 0x05]).is_err());