pub struct Value(/* private fields */);Expand description
The CBOR data structure.
Implementations§
Source§impl Value
impl Value
Sourcepub fn integer(int: i64) -> Value
pub fn integer(int: i64) -> Value
Create an appropriate CBOR integer value (uint/nint). For simplicity, this only takes i64. Construct directly for the last bit.
Sourcepub fn byte_string(bytes: Vec<u8>) -> Value
pub fn byte_string(bytes: Vec<u8>) -> Value
Creates a CBOR byte string value.
Sourcepub fn text_string(text: String) -> Value
pub fn text_string(text: String) -> Value
Creates a CBOR text string value.
Sourcepub fn array(a: Vec<Value>) -> Value
pub fn array(a: Vec<Value>) -> Value
Create a CBOR array value.
Examples found in repository?
examples/cbor.rs (line 38)
32fn main() {
33 // Build a CBOR object with various different types included. Note that this
34 // object is not built in canonical order.
35 let manual_object = Value::map(vec![
36 (
37 Value::from(1),
38 Value::array(vec![Value::from(2), Value::from(3)]),
39 ),
40 (Value::from("tstr".to_owned()), Value::from(vec![1, 2, 3])),
41 (Value::from(-2), Value::null_value()),
42 (Value::from(3), Value::bool_value(true)),
43 ]);
44
45 // Build the same object using the crate's convenience macros.
46 let macro_object = cbor_map! {
47 1 => cbor_array![2, 3],
48 "tstr" => cbor_bytes!(vec![1, 2, 3]),
49 -2 => cbor_null!(),
50 3 => cbor_true!(),
51 };
52
53 assert_eq!(manual_object, macro_object);
54 println!("Object {:?}", manual_object);
55
56 // Serialize to bytes.
57 let mut manual_data = vec![];
58 sk_cbor::writer::write(manual_object, &mut manual_data).unwrap();
59 let hex_manual_data = hexify(&manual_data);
60 let mut macro_data = vec![];
61 sk_cbor::writer::write(macro_object, &mut macro_data).unwrap();
62 let hex_macro_data = hexify(¯o_data);
63
64 assert_eq!(hex_manual_data, hex_macro_data);
65
66 // Serialized version is in canonical order.
67 println!("Serializes to {}", hex_manual_data);
68 assert_eq!(
69 hex_manual_data,
70 concat!(
71 "a4", // 4-map
72 "01", // int(1) =>
73 "820203", // 2-array [2, 3],
74 "03", // int(3) =>
75 "f5", // true,
76 "21", // nint(-2) =>
77 "f6", // null,
78 "6474737472", // 4-tstr "tstr" =>
79 "43010203" // 3-bstr
80 )
81 );
82
83 // Convert back to an object. This is different than the original object,
84 // because the map is now in canonical order.
85 let recovered_object = sk_cbor::reader::read(&manual_data).unwrap();
86 println!("Deserializes to {:?}", recovered_object);
87}Sourcepub fn map(m: Vec<(Value, Value)>) -> Value
pub fn map(m: Vec<(Value, Value)>) -> Value
Create a CBOR map value.
Keys do not have to be sorted.
§Panics
You may not call this function with identical keys in its argument.
Examples found in repository?
examples/cbor.rs (lines 35-43)
32fn main() {
33 // Build a CBOR object with various different types included. Note that this
34 // object is not built in canonical order.
35 let manual_object = Value::map(vec![
36 (
37 Value::from(1),
38 Value::array(vec![Value::from(2), Value::from(3)]),
39 ),
40 (Value::from("tstr".to_owned()), Value::from(vec![1, 2, 3])),
41 (Value::from(-2), Value::null_value()),
42 (Value::from(3), Value::bool_value(true)),
43 ]);
44
45 // Build the same object using the crate's convenience macros.
46 let macro_object = cbor_map! {
47 1 => cbor_array![2, 3],
48 "tstr" => cbor_bytes!(vec![1, 2, 3]),
49 -2 => cbor_null!(),
50 3 => cbor_true!(),
51 };
52
53 assert_eq!(manual_object, macro_object);
54 println!("Object {:?}", manual_object);
55
56 // Serialize to bytes.
57 let mut manual_data = vec![];
58 sk_cbor::writer::write(manual_object, &mut manual_data).unwrap();
59 let hex_manual_data = hexify(&manual_data);
60 let mut macro_data = vec![];
61 sk_cbor::writer::write(macro_object, &mut macro_data).unwrap();
62 let hex_macro_data = hexify(¯o_data);
63
64 assert_eq!(hex_manual_data, hex_macro_data);
65
66 // Serialized version is in canonical order.
67 println!("Serializes to {}", hex_manual_data);
68 assert_eq!(
69 hex_manual_data,
70 concat!(
71 "a4", // 4-map
72 "01", // int(1) =>
73 "820203", // 2-array [2, 3],
74 "03", // int(3) =>
75 "f5", // true,
76 "21", // nint(-2) =>
77 "f6", // null,
78 "6474737472", // 4-tstr "tstr" =>
79 "43010203" // 3-bstr
80 )
81 );
82
83 // Convert back to an object. This is different than the original object,
84 // because the map is now in canonical order.
85 let recovered_object = sk_cbor::reader::read(&manual_data).unwrap();
86 println!("Deserializes to {:?}", recovered_object);
87}Sourcepub fn bool_value(b: bool) -> Value
pub fn bool_value(b: bool) -> Value
Create a CBOR boolean simple value.
Examples found in repository?
examples/cbor.rs (line 42)
32fn main() {
33 // Build a CBOR object with various different types included. Note that this
34 // object is not built in canonical order.
35 let manual_object = Value::map(vec![
36 (
37 Value::from(1),
38 Value::array(vec![Value::from(2), Value::from(3)]),
39 ),
40 (Value::from("tstr".to_owned()), Value::from(vec![1, 2, 3])),
41 (Value::from(-2), Value::null_value()),
42 (Value::from(3), Value::bool_value(true)),
43 ]);
44
45 // Build the same object using the crate's convenience macros.
46 let macro_object = cbor_map! {
47 1 => cbor_array![2, 3],
48 "tstr" => cbor_bytes!(vec![1, 2, 3]),
49 -2 => cbor_null!(),
50 3 => cbor_true!(),
51 };
52
53 assert_eq!(manual_object, macro_object);
54 println!("Object {:?}", manual_object);
55
56 // Serialize to bytes.
57 let mut manual_data = vec![];
58 sk_cbor::writer::write(manual_object, &mut manual_data).unwrap();
59 let hex_manual_data = hexify(&manual_data);
60 let mut macro_data = vec![];
61 sk_cbor::writer::write(macro_object, &mut macro_data).unwrap();
62 let hex_macro_data = hexify(¯o_data);
63
64 assert_eq!(hex_manual_data, hex_macro_data);
65
66 // Serialized version is in canonical order.
67 println!("Serializes to {}", hex_manual_data);
68 assert_eq!(
69 hex_manual_data,
70 concat!(
71 "a4", // 4-map
72 "01", // int(1) =>
73 "820203", // 2-array [2, 3],
74 "03", // int(3) =>
75 "f5", // true,
76 "21", // nint(-2) =>
77 "f6", // null,
78 "6474737472", // 4-tstr "tstr" =>
79 "43010203" // 3-bstr
80 )
81 );
82
83 // Convert back to an object. This is different than the original object,
84 // because the map is now in canonical order.
85 let recovered_object = sk_cbor::reader::read(&manual_data).unwrap();
86 println!("Deserializes to {:?}", recovered_object);
87}Sourcepub fn null_value() -> Value
pub fn null_value() -> Value
Creates a null value.
Examples found in repository?
examples/cbor.rs (line 41)
32fn main() {
33 // Build a CBOR object with various different types included. Note that this
34 // object is not built in canonical order.
35 let manual_object = Value::map(vec![
36 (
37 Value::from(1),
38 Value::array(vec![Value::from(2), Value::from(3)]),
39 ),
40 (Value::from("tstr".to_owned()), Value::from(vec![1, 2, 3])),
41 (Value::from(-2), Value::null_value()),
42 (Value::from(3), Value::bool_value(true)),
43 ]);
44
45 // Build the same object using the crate's convenience macros.
46 let macro_object = cbor_map! {
47 1 => cbor_array![2, 3],
48 "tstr" => cbor_bytes!(vec![1, 2, 3]),
49 -2 => cbor_null!(),
50 3 => cbor_true!(),
51 };
52
53 assert_eq!(manual_object, macro_object);
54 println!("Object {:?}", manual_object);
55
56 // Serialize to bytes.
57 let mut manual_data = vec![];
58 sk_cbor::writer::write(manual_object, &mut manual_data).unwrap();
59 let hex_manual_data = hexify(&manual_data);
60 let mut macro_data = vec![];
61 sk_cbor::writer::write(macro_object, &mut macro_data).unwrap();
62 let hex_macro_data = hexify(¯o_data);
63
64 assert_eq!(hex_manual_data, hex_macro_data);
65
66 // Serialized version is in canonical order.
67 println!("Serializes to {}", hex_manual_data);
68 assert_eq!(
69 hex_manual_data,
70 concat!(
71 "a4", // 4-map
72 "01", // int(1) =>
73 "820203", // 2-array [2, 3],
74 "03", // int(3) =>
75 "f5", // true,
76 "21", // nint(-2) =>
77 "f6", // null,
78 "6474737472", // 4-tstr "tstr" =>
79 "43010203" // 3-bstr
80 )
81 );
82
83 // Convert back to an object. This is different than the original object,
84 // because the map is now in canonical order.
85 let recovered_object = sk_cbor::reader::read(&manual_data).unwrap();
86 println!("Deserializes to {:?}", recovered_object);
87}pub fn extract_unsigned(self) -> Option<u64>
pub fn extract_integer(self) -> Option<i64>
pub fn extract_byte_string(self) -> Option<Vec<u8>>
pub fn extract_text_string(self) -> Option<String>
pub fn extract_array(self) -> Option<Vec<Value>>
pub fn extract_map(self) -> Option<Vec<(Value, Value)>>
pub fn extract_tag(self) -> Option<(u64, Value)>
pub fn extract_bool(self) -> Option<bool>
pub fn extract_null(self) -> Option<()>
pub fn extract_undefined(self) -> Option<()>
Trait Implementations§
impl Eq for Value
Source§impl Ord for Value
impl Ord for Value
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
Source§impl PartialOrd for Value
impl PartialOrd for Value
impl StructuralPartialEq for Value
Auto Trait Implementations§
impl Freeze for Value
impl RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnsafeUnpin for Value
impl UnwindSafe for Value
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoCborValue for T
impl<T> IntoCborValue for T
Source§fn into_cbor_value(self) -> Value
fn into_cbor_value(self) -> Value
Convert
self into a CBOR Value, consuming it along the way.Source§impl<T> IntoCborValueOption for T
impl<T> IntoCborValueOption for T
Source§fn into_cbor_value_option(self) -> Option<Value>
fn into_cbor_value_option(self) -> Option<Value>
Convert
self into a CBOR Option<Value>, consuming it along the way.