pub enum MsgPack {
Nil,
Int(i64),
Uint(u64),
Float(f64),
Boolean(bool),
String(String),
Binary(Vec<u8>),
Array(Vec<MsgPack>),
Map(Vec<MapElement>),
Extension(Extension),
}
Expand description
A piece of MessagePack-compatible data
use msgpack_simple::{MsgPack, MapElement, Extension};
let message = MsgPack::Map(vec![
MapElement {
key: MsgPack::String(String::from("hello")),
value: MsgPack::Int(42)
},
MapElement {
key: MsgPack::String(String::from("world")),
value: MsgPack::Array(vec![
MsgPack::Boolean(true),
MsgPack::Nil,
MsgPack::Binary(vec![0x42, 0xff]),
MsgPack::Extension(Extension {
type_id: 2,
value: vec![0x32, 0x4a, 0x67, 0x11]
})
])
}
]);
Variants§
Nil
Empty value
use msgpack_simple::MsgPack;
let nil = MsgPack::Nil;
assert!(nil.is_nil());
Int(i64)
Signed integer, not much magic here
use msgpack_simple::MsgPack;
let int = MsgPack::Int(42);
assert!(int.is_int());
assert!(int.is_some_int());
assert_eq!(int.clone().as_int().unwrap(), 42);
assert_eq!(int.as_some_int().unwrap(), 42);
Uint(u64)
Unsigned integer
use msgpack_simple::MsgPack;
let uint = MsgPack::Uint(42);
assert!(uint.is_uint());
assert!(uint.is_some_int());
assert_eq!(uint.clone().as_uint().unwrap(), 42);
assert_eq!(uint.as_some_int().unwrap(), 42);
Float(f64)
Floating-point number
use msgpack_simple::MsgPack;
let float = MsgPack::Float(42.0);
assert!(float.is_float());
assert_eq!(float.as_float().unwrap(), 42.0);
Boolean(bool)
Boolean (wait, really?)
use msgpack_simple::MsgPack;
let boolean = MsgPack::Boolean(true);
assert!(boolean.is_boolean());
assert_eq!(boolean.as_boolean().unwrap(), true);
String(String)
Unicode compatible string
use msgpack_simple::MsgPack;
let string = MsgPack::String(String::from("foo"));
assert!(string.is_string());
assert_eq!(string.as_string().unwrap(), "foo".to_string());
Binary(Vec<u8>)
Raw binary value
use msgpack_simple::MsgPack;
let binary = MsgPack::Binary(vec![0x42]);
assert!(binary.is_binary());
assert_eq!(binary.as_binary().unwrap(), vec![0x42]);
Array(Vec<MsgPack>)
An array of other MsgPack fields
use msgpack_simple::MsgPack;
let array = MsgPack::Array(vec![
MsgPack::Int(42)
]);
assert!(array.is_array());
assert_eq!(array.as_array().unwrap(), vec![MsgPack::Int(42)]);
Map(Vec<MapElement>)
A map with key-value pairs, both being MsgPack data fields
use msgpack_simple::{MsgPack, MapElement};
let map = MsgPack::Map(vec![
MapElement {
key: MsgPack::String("foo".to_string()),
value: MsgPack::String("bar".to_string())
}
]);
assert!(map.is_map());
assert_eq!(map.as_map().unwrap(), vec![MapElement {
key: MsgPack::String("foo".to_string()),
value: MsgPack::String("bar".to_string())
}]);
Extension(Extension)
A tuple of an extension type and a raw data value
use msgpack_simple::{MsgPack, Extension};
let extension = MsgPack::Extension(Extension {
type_id: 42,
value: vec![0x42]
});
assert!(extension.is_extension());
assert_eq!(extension.as_extension().unwrap(), Extension { type_id: 42, value: vec![0x42] });
Implementations§
Source§impl MsgPack
impl MsgPack
Sourcepub fn parse(raw: &[u8]) -> Result<MsgPack, ParseError>
pub fn parse(raw: &[u8]) -> Result<MsgPack, ParseError>
Parses binary data as MessagePack
use msgpack_simple::MsgPack;
let data = vec![0xaa, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x52, 0x75, 0x73, 0x74];
let decoded = MsgPack::parse(&data);
assert!(decoded.is_ok());
let decoded = decoded.unwrap();
assert!(decoded.is_string());
assert_eq!(decoded.as_string().unwrap(), "Hello Rust".to_string());
Sourcepub fn encode(&self) -> Vec<u8> ⓘ
pub fn encode(&self) -> Vec<u8> ⓘ
Encodes a MsgPack enum into binary format
use msgpack_simple::MsgPack;
let message = MsgPack::String("Hello Rust".to_string());
let encoded = message.encode();
let data = vec![0xaa, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x52, 0x75, 0x73, 0x74];
assert_eq!(encoded, data);
Sourcepub fn is_int(&self) -> bool
pub fn is_int(&self) -> bool
Checks if the MsgPack is an int variant
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Int(42).is_int(), true);
assert_eq!(MsgPack::Float(42.0).is_int(), false);
Sourcepub fn as_int(self) -> Result<i64, ConversionError>
pub fn as_int(self) -> Result<i64, ConversionError>
Consumes the MsgPack as int
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Int(42).as_int().unwrap(), 42);
Sourcepub fn is_uint(&self) -> bool
pub fn is_uint(&self) -> bool
Checks if the MsgPack is a uint variant
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Uint(42).is_uint(), true);
assert_eq!(MsgPack::Float(42.0).is_uint(), false);
Sourcepub fn as_uint(self) -> Result<u64, ConversionError>
pub fn as_uint(self) -> Result<u64, ConversionError>
Consumes the MsgPack as uint
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Uint(42).as_uint().unwrap(), 42);
Sourcepub fn is_some_int(&self) -> bool
pub fn is_some_int(&self) -> bool
Checks if the MsgPack is one of the integer variants
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Int(42).is_some_int(), true);
assert_eq!(MsgPack::Uint(42).is_some_int(), true);
assert_eq!(MsgPack::Float(42.0).is_some_int(), false);
Sourcepub fn as_some_int(self) -> Result<i64, ConversionError>
pub fn as_some_int(self) -> Result<i64, ConversionError>
Consumes the MsgPack as an int, even if it’s a uint
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Int(42).as_some_int().unwrap(), 42);
assert_eq!(MsgPack::Uint(42).as_some_int().unwrap(), 42);
Sourcepub fn is_float(&self) -> bool
pub fn is_float(&self) -> bool
Checks if the MsgPack is a float
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Float(42.0).is_float(), true);
assert_eq!(MsgPack::Int(42).is_float(), false);
Sourcepub fn as_float(self) -> Result<f64, ConversionError>
pub fn as_float(self) -> Result<f64, ConversionError>
Consumes the MsgPack as a float
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Float(42.0).as_float().unwrap(), 42.0);
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Checks if the MsgPack is a boolean
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Boolean(true).is_boolean(), true);
assert_eq!(MsgPack::Int(1).is_boolean(), false);
Sourcepub fn as_boolean(self) -> Result<bool, ConversionError>
pub fn as_boolean(self) -> Result<bool, ConversionError>
Consumes the MsgPack as a boolean
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Boolean(true).as_boolean().unwrap(), true);
Sourcepub fn is_nil(&self) -> bool
pub fn is_nil(&self) -> bool
Checks if the MsgPack is a nil
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Nil.is_nil(), true);
assert_eq!(MsgPack::Boolean(false).is_nil(), false);
Sourcepub fn is_string(&self) -> bool
pub fn is_string(&self) -> bool
Checks if the MsgPack is a string
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::String("foo".to_string()).is_string(), true);
assert_eq!(MsgPack::Binary(vec![0x66, 0x6f, 0x6f]).is_string(), false);
Sourcepub fn as_string(self) -> Result<String, ConversionError>
pub fn as_string(self) -> Result<String, ConversionError>
Consumes the MsgPack as a string
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::String("foo".to_string()).as_string().unwrap(), "foo".to_string());
Sourcepub fn is_binary(&self) -> bool
pub fn is_binary(&self) -> bool
Checks if the MsgPack is a binary
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Binary(vec![0x66, 0x6f, 0x6f]).is_binary(), true);
assert_eq!(MsgPack::String("foo".to_string()).is_binary(), false);
Sourcepub fn as_binary(self) -> Result<Vec<u8>, ConversionError>
pub fn as_binary(self) -> Result<Vec<u8>, ConversionError>
Consumes the MsgPack as a binary
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Binary(vec![0x66, 0x6f, 0x6f]).as_binary().unwrap(), vec![0x66, 0x6f, 0x6f]);
Sourcepub fn is_array(&self) -> bool
pub fn is_array(&self) -> bool
Checks if the MsgPack is an array
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Array(vec![]).is_array(), true);
assert_eq!(MsgPack::Map(vec![]).is_array(), false);
Sourcepub fn as_array(self) -> Result<Vec<MsgPack>, ConversionError>
pub fn as_array(self) -> Result<Vec<MsgPack>, ConversionError>
Consumes the MsgPack as an array
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Array(vec![]).as_array().unwrap(), vec![]);
Sourcepub fn is_map(&self) -> bool
pub fn is_map(&self) -> bool
Checks if the MsgPack is a map
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Map(vec![]).is_map(), true);
assert_eq!(MsgPack::Array(vec![]).is_map(), false);
Sourcepub fn as_map(self) -> Result<Vec<MapElement>, ConversionError>
pub fn as_map(self) -> Result<Vec<MapElement>, ConversionError>
Consumes the MsgPack as a map
use msgpack_simple::MsgPack;
assert_eq!(MsgPack::Map(vec![]).as_map().unwrap(), vec![]);
Sourcepub fn is_extension(&self) -> bool
pub fn is_extension(&self) -> bool
Checks if the MsgPack is an extension
use msgpack_simple::{MsgPack, Extension};
let value = Extension { type_id: 42, value: vec![0x42] };
assert_eq!(MsgPack::Extension(value).is_extension(), true);
assert_eq!(MsgPack::Binary(vec![0x42]).is_extension(), false);
Sourcepub fn as_extension(self) -> Result<Extension, ConversionError>
pub fn as_extension(self) -> Result<Extension, ConversionError>
Consumes the MsgPack as an extension
use msgpack_simple::{MsgPack, Extension};
let value = Extension { type_id: 42, value: vec![0x42] };
assert_eq!(MsgPack::Extension(value.clone()).as_extension().unwrap(), value);