|
1 |
| -# Serde CBOR |
2 | 1 | [](https://travis-ci.org/pyfisch/cbor)
|
| 2 | + |
| 3 | +This repository manages 2 packages; ll_cbor and serde_cbor. |
| 4 | + |
| 5 | +# LL CBOR |
| 6 | +[](https://crates.io/crates/ll_cbor) |
| 7 | +[](https://docs.rs/ll_cbor) |
| 8 | + |
| 9 | +LL CBOR is a low level CBOR serialization and deserialization API that allows people to |
| 10 | +serialize and deserialize CBOR without any framework, in the Schema they want. |
| 11 | + |
| 12 | +This crate comes from the need for developers to support CBOR values that aren't natively |
| 13 | +supported by other serialization platform (like serde). An example of this is large integers |
| 14 | +and dates; Serde does not support bit integer types or special object types, but CBOR has |
| 15 | +them and an application might want to serialize or deserialize using those (or the full |
| 16 | +CBOR spectrum, or custom made tags). |
| 17 | + |
| 18 | +## Usage |
| 19 | +LL CBOR supports Rust 1.38 and up. To install it add this to your `Cargo.toml`: |
| 20 | +```toml |
| 21 | +[dependencies] |
| 22 | +ll_cbor = "0.1.0" |
| 23 | +``` |
| 24 | + |
| 25 | +Then, for serializing values: |
| 26 | + |
| 27 | +```rust |
| 28 | +use ll_cbor::serialize::values as cbor; |
| 29 | +use ll_cbor::serialize::builders; |
| 30 | + |
| 31 | +fn main() -> Result<(), std::error::Error> { |
| 32 | + // Serialize a single u64. |
| 33 | + let some_value = cbor::u64(0); |
| 34 | + |
| 35 | + // Serialize a vector of u32. |
| 36 | + let some_vector = cbor::vector(vec![cbor::u32(1), cbor::u32(2), cbor::u32(3)]); |
| 37 | + |
| 38 | + // Serialize a map of variable values. |
| 39 | + // This is a HashMap<ll_cbor::Value, ll_cbor::Value>. |
| 40 | + let hash = std::collections::HashMap::new(); |
| 41 | + hash.insert(cbor::string("hello"), cbor::i8(-100)); |
| 42 | + // It is legal in CBOR to have different type of keys in maps, but impossible to represent |
| 43 | + // natively with Serde. |
| 44 | + hash.insert(cbor::u32(1), cbor::string("World")); |
| 45 | + let some_map = cbor::dictionary(&hash); |
| 46 | + |
| 47 | + // We can also just pass in bytes and get an untrusted ll_cbor::Value from it: |
| 48 | + let value = ll_cbor::Value::from_untrusted_slice(&[1, 2, 3]); |
| 49 | + |
| 50 | + // When we don't know how many objects in advance, we can use a builder. |
| 51 | + let some_map_builder = builders::dictionary(); |
| 52 | + for i in 0..1000 { |
| 53 | + // It is also possible in CBOR to have multiple values with the same key. |
| 54 | + some_map_builder.insert(cbor::string("key"), cbor::u32(i)); |
| 55 | + } |
| 56 | + |
| 57 | + // Adding a CBOR tag to it. |
| 58 | + let some_map2 = cbor::tag(55799, some_map_builder.build()); |
| 59 | + |
| 60 | + // Getting the bytes for the second map. |
| 61 | + println("{}", hex::encode(&some_map2)); |
| 62 | + |
| 63 | + Ok(()) |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +For deserialization, there are multiple ways. The main way is to use various `try_from` |
| 68 | +to check if a byte stream is of the right type. You can also build a schema and validate |
| 69 | +the input with it. |
| 70 | + |
| 71 | +```rust |
| 72 | +use ll_cbor::deserialize::values as cbor_de; |
| 73 | +use ll_cbor::schema; |
| 74 | + |
| 75 | +fn main() -> Result<(), std::error::Error> { |
| 76 | + let bytes: Vec<u8> = vec![1, 2, 3]; |
| 77 | + |
| 78 | + // These will be of type Result<u64, ll_cbor::deserialize::Error>. |
| 79 | + let maybe_u64 = cbor_de::u64::try_from(&bytes); |
| 80 | + let maybe_string = cbor_de::string::try_from(&bytes); |
| 81 | + |
| 82 | + // A vector can contain any elements. |
| 83 | + let maybe_vec = cbor_de::vec::try_from(&bytes); |
| 84 | + |
| 85 | + // So we have to map and test all items. |
| 86 | + // TODO: correct the unwrap() calls with results. |
| 87 | + let maybe_vec_of_u32 = maybe_vec.map(|v| v.iter().map(|i| cbor_de::u32::try_from(i)).collect()); |
| 88 | + |
| 89 | + // This will create a schema for a dictionary of string -> tag + i8. |
| 90 | + let s = schema::dictionary(schema::string, schema::tag(schema::i8)); |
| 91 | + // Validate bytes match the schema. |
| 92 | + // This returns a `Result<BTreeMap<String, i8>, ll_cbor::schema::Error>`. |
| 93 | + let maybe_v = s.validate(&bytes); |
| 94 | + |
| 95 | + Ok(()) |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | + |
| 100 | +# Serde CBOR |
3 | 101 | [](https://crates.io/crates/serde_cbor)
|
4 | 102 | [](https://docs.rs/serde_cbor)
|
5 | 103 |
|
|
0 commit comments