3个版本

0.1.2 2021年7月14日
0.1.1 2021年6月25日
0.1.0 2021年6月23日

编码 中排名 2102

Download history 25/week @ 2024-03-12 22/week @ 2024-03-19 25/week @ 2024-03-26 60/week @ 2024-04-02 20/week @ 2024-04-09 21/week @ 2024-04-16 28/week @ 2024-04-23 14/week @ 2024-04-30 20/week @ 2024-05-07 23/week @ 2024-05-14 20/week @ 2024-05-21 30/week @ 2024-05-28 23/week @ 2024-06-04 23/week @ 2024-06-11 20/week @ 2024-06-18 17/week @ 2024-06-25

每月下载量 86
holium 中使用

Apache-2.0

87KB
2K SLoC

CBOR解析库

crates.io crates.io docs.rs License Maintenance

此包实现了来自RFC 8949的 Concise Binary Object Representation (CBOR)。

使用方法

fn main() {
    // Build a CBOR object with various different types included. Note that this
    // object is not built in canonical order.
    let manual_object = Value::Map(vec![
        (
            Value::Unsigned(1),
            Value::Array(vec![Value::Unsigned(2), Value::Unsigned(3)]),
        ),
        (
            Value::TextString("tstr".to_owned()),
            Value::ByteString(vec![1, 2, 3]),
        ),
        (Value::Negative(-2), Value::Simple(SimpleValue::NullValue)),
        (Value::Unsigned(3), Value::Simple(SimpleValue::TrueValue)),
    ]);

    // Build the same object using the crate's convenience macros.
    let macro_object = cbor_map! {
        1 => cbor_array![2, 3],
        "tstr" => cbor_bytes!(vec![1, 2, 3]),
        -2 => cbor_null!(),
        3 => cbor_true!(),
    };

    assert_eq!(manual_object, macro_object);
    println!("Object {:?}", manual_object);

    // Serialize to bytes.
    let mut manual_data = vec![];
    sk_cbor::writer::write(manual_object, &mut manual_data);
    let hex_manual_data = hexify(&manual_data);

    // Serialized version is in canonical order.
    println!("Serializes to {}", hex_manual_data);
    assert_eq!(
        hex_manual_data,
        concat!(
            "a4",         // 4-map
            "01",         // int(1) =>
            "820203",     // 2-array [2, 3],
            "03",         // int(3) =>
            "f5",         // true,
            "21",         // nint(-2) =>
            "f6",         // null,
            "6474737472", // 4-tstr "tstr" =>
            "43010203"    // 3-bstr
        )
    );

    // Convert back to an object.  This is different than the original object,
    // because the map is now in canonical order.
    let recovered_object = sk_cbor::reader::read(&manual_data).unwrap();
    println!("Deserializes to {:?}", recovered_object);
}

无运行时依赖