Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBOR API changes #639

Merged
merged 5 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 9 additions & 24 deletions libraries/cbor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,26 @@ This crate implements Concise Binary Object Representation (CBOR) from [RFC

```rust
fn main() {
// Build a CBOR object with various different types included. Note that this
// Build a CBOR object with the crate's convenience macros. 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! {
let map_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);
println!("Object {:?}", map_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);
let mut map_data = vec![];
sk_cbor::writer::write(map_object, &mut map_data).unwrap();
let hex_map_data = hex::encode(&map_data);

// Serialized version is in canonical order.
println!("Serializes to {}", hex_manual_data);
println!("Serializes to {}", hex_map_data);
assert_eq!(
hex_manual_data,
hex_map_data,
concat!(
"a4", // 4-map
"01", // int(1) =>
Expand All @@ -63,7 +48,7 @@ fn main() {

// 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();
let recovered_object = sk_cbor::reader::read(&map_data).unwrap();
println!("Deserializes to {:?}", recovered_object);
}
```
17 changes: 7 additions & 10 deletions libraries/cbor/examples/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

extern crate alloc;

use sk_cbor::values::{SimpleValue, Value};
use sk_cbor::values::Value;
use sk_cbor::{cbor_array, cbor_bytes, cbor_map, cbor_null, cbor_true};

fn hexify(data: &[u8]) -> String {
Expand All @@ -32,17 +32,14 @@ fn hexify(data: &[u8]) -> String {
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![
let manual_object = Value::map(vec![
(
Value::Unsigned(1),
Value::Array(vec![Value::Unsigned(2), Value::Unsigned(3)]),
Value::from(1),
Value::array(vec![Value::from(2), Value::from(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)),
(Value::from("tstr".to_owned()), Value::from(vec![1, 2, 3])),
(Value::from(-2), Value::null_value()),
(Value::from(3), Value::bool_value(true)),
]);

// Build the same object using the crate's convenience macros.
Expand Down
2 changes: 1 addition & 1 deletion libraries/cbor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ pub mod values;
pub mod writer;

pub use self::reader::read;
pub use self::values::{SimpleValue, Value};
pub use self::values::Value;
pub use self::writer::write;
Loading
Loading