rmp-serde-1.1.1/.cargo_vcs_info.json0000644000000001470000000000100127340ustar { "git": { "sha1": "941441f484c325464c9746fdca9d11c1ca6a2d78" }, "path_in_vcs": "rmp-serde" }rmp-serde-1.1.1/CHANGELOG.md000064400000000000000000000153771046102023000133500ustar 00000000000000# Change Log All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased][unreleased] ### Added: - Generic `decode::from_read_ref` function that allows to deserialize a borrowed byte-array into the specified type. - Add `Ext` trait for `Serializer` that allows to wrap a serializer with another one, that overrides exactly one serialization policy. For example using `with_struct_map` method it is possible to serialize structs as a MessagePack map with field names, overriding default serialization policy, which emits structs as a tuple. - Add `UnderlyingWrite` trait for `Serializer` and its wrappers to be able to obtain the underlying writer. - Add missing `Debug` implementations. - More `decode::Error` conversions. - Support for serializing and deserializing 128-bit values in serde. - Support for serializing sequences and maps with unknown length, that enables the use of `#[serde(flatten)]` attribute (#196). - Depth limit is now enforced for `Deserializer`. ### Changed: - (Breaking) Serialize newtype structs by serializing its inner type without wrapping into a tuple. - (Breaking) Enums are now encoded as a map `{tag: data}` rather than as a list `[tag, data]`. (#149) - Function `encode::to_vec_named` now accepts unsized values. - Renamed `decode::Read` trait to `decode::ReadSlice` to avoid clashing with `std::io::Read` and to specify more precisely what it does. - Support reading encoded integers as floats when safe (#204) ### Removed: - Type parameter `VariantWriter` is no longer a type member of `Serializer`. Instead a `Serializer` can be wrapped by another serializer using `with_struct_map`, `with_struct_tuple` etc. methods. ### Fixed: - Fix error decoding `Some(enum)` (#185) - Fix error decoding unit structs which were encoded as `[]` (#181) - Fix `Display` implementations for errors not including all relevant information (#199) - Fix deserialization of nested `Option`s (#245) ## 0.13.7 - 2017-09-13 ### Changed: - `Raw` and `RawRef` are now serializable. - Allow to construct `Raw` and `RawRef` from string or from a byte array. ## 0.13.6 - 2017-08-04 ### Added: - Serialize struct as a map (#140). ## 0.13.5 - 2017-07-21 ### Changed - Switch to using `char::encode_utf8`. In Rust 1.15, the function `char::encode_utf8` was stabilized. Assuming that `rmp` follows the `serde` standard of supporting the last 3 stable releases, this function is now safe to use. I believe this removes the last allocation required on the serialization path. ## 0.13.4 - 2017-07-11 ### Fixed - Fixed build on nightly rustc (#135). ## 0.13.3 - 2017-05-27 ### Fixed - Fixed build on nightly rustc (#131). ## 0.13.2 - 2017-04-30 ### Changed - Fixed `rmps::decode::from_read` signature by marking that it can only deserialize into `DeserializeOwned`. The previous signature let try to deserialize, for example `&str` and other borrow types and it failed at runtime instead of catching it at compile time. ## 0.13.1 - 2017-04-25 ### Added - Add helper `RawRef` struct that allows to deserialize borrowed strings even if they contain invalid UTF-8. This can be when deserializing frames from older MessagePack spec. ## 0.13.0 - 2017-04-24 ### Added - Zero-copy deserialization from `&[u8]`. ### Changed - Adapt with serde 1.0. ## 0.12.4 - 2017-03-26 ### Fixed - Fix compilation on rustc 1.13. ## 0.12.3 - 2017-03-26 ### Added - Add helper `Raw` struct that allows to deserialize strings even if they contain invalid UTF-8. This can be when deserializing frames from older MessagePack spec. - Serializer can now return back its underlying writer by reference, mutable reference and by value. ## 0.12.2 - 2017-02-17 ### Added - Added `write`, `to_vec` and `from_read` functions to reduce boilerplate for serializing and deserializing custom types that implement `Serialize` or `Deserialize`. ## 0.12.1 - 2017-02-11 ### Added - Allow `Deserializer` to return number of bytes read in case of using Cursor as an underlying reader. ## 0.12.0 - 2017-02-08 ### Changed - Adapt with serde 0.9. ## 0.11.0 - 2017-01-05 ### Changed - Adapt with RMP core 0.8. - The `Serializer` now encodes integers using the most effective representation. - The `Deserializer` now properly decodes integer values that fit in the expected type. - Default stack protector depth is now 1024 instead of 1000. - Internal buffer in the `Deserializer` now have some capacity preallocated. ## 0.10.0 - 2016-10-06 ### Changed - Update serde dependency to 0.8. ## 0.9.6 - 2016-08-05 ### Fixed - Switch unit structs to using the same serialization mechanism as other structs (#76). ## 0.9.5 - 2016-07-28 ### Added - Added a wrapper over `rmp::Value` to be able to serialize it. ## 0.9.4 - 2016-07-11 ### Fixed - Reading binary should no longer trigger unexpected EOF error on valid read. ## 0.9.3 - 2016-07-11 ### Changed - Reuse deserializer buffer on every read for string and binary deserialization without unnecessary intermediate buffer creation. This change increases the string and binary deserialization performance (many thanks to Fedor Gogolev ). ## 0.9.2 - 2016-07-03 ### Added - Implement `size_hint()` function for `SeqVisitor` and `MapVisitor`, so it can be possible to preallocate things, increasing the performance greatly. ## 0.9.1 - 2016-06-24 ### Fixed - Serializer should no longer panic with unimplemented error on struct variant serialization ([#64]). ## 0.9.0 - 2016-03-28 ### Changed - Adapt code to be compilable with Serde v0.7. ## 0.8.2 - 2015-11-10 ### Changed - Fixed stack overflow when unpacking recursive data structures. ## 0.8.1 - 2015-10-03 ### Changed - Upper limit for serde version. ### Fixed - Use the most effective int encoding Even if the value is explicitly marked as i64 it must be encoded using the most effective bytes representation despite of signed it or unsigned. ## 0.8.0 - 2015-09-11 ### Changed - Serializer can now be extended with custom struct encoding policy. - Improved error types and its messages for serialization part. - New error type introduced - UnknownLength. Returned on attempt to serialize struct, map or serquence with unknown length (Serde allows this). - The new type is returned if necessary. ### Fixed - Deserializer now properly works with enums. - Options with default values (that can be initialized using unit marker) deserialization. This fix also forbids the following Option deserialization cases: - Option<()>. - Option>. It's impossible to properly deserialize the listed cases without explicit option marker in protocol. - Serializer now properly serializes unit structs. Previously it was serialized as a unit (nil), now there is just an empty array ([]). [#64]: (https://github.com/3Hren/msgpack-rust/pull/64) [#76]: (https://github.com/3Hren/msgpack-rust/pull/76) rmp-serde-1.1.1/Cargo.toml0000644000000022710000000000100107320ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "rmp-serde" version = "1.1.1" authors = ["Evgeny Safronov "] description = "Serde bindings for RMP" documentation = "https://docs.rs/rmp-serde" readme = "README.md" keywords = [ "msgpack", "MessagePack", "serde", "serialization", ] categories = ["encoding"] license = "MIT" repository = "https://github.com/3Hren/msgpack-rust" [package.metadata.release] tag-prefix = "{{crate_name}}/" [dependencies.byteorder] version = "1.4.3" [dependencies.rmp] version = "0.8.11" [dependencies.serde] version = "1.0.136" [dev-dependencies.serde_bytes] version = "0.11.5" [dev-dependencies.serde_derive] version = "1.0.136" [badges.maintenance] status = "passively-maintained" rmp-serde-1.1.1/Cargo.toml.orig000064400000000000000000000012701046102023000144110ustar 00000000000000[package] name = "rmp-serde" version = "1.1.1" authors = ["Evgeny Safronov "] license = "MIT" description = "Serde bindings for RMP" repository = "https://github.com/3Hren/msgpack-rust" documentation = "https://docs.rs/rmp-serde" readme = "README.md" keywords = ["msgpack", "MessagePack", "serde", "serialization"] categories = ["encoding"] edition = "2018" [package.metadata.release] tag-prefix = "{{crate_name}}/" [dependencies] byteorder = "1.4.3" serde = "1.0.136" rmp = { version = "0.8.11", path = "../rmp" } [dev-dependencies] rmpv = { path = "../rmpv" } serde_bytes = "0.11.5" serde_derive = "1.0.136" [badges] maintenance = { status = "passively-maintained" } rmp-serde-1.1.1/LICENSE000064400000000000000000000020601046102023000125250ustar 00000000000000MIT License Copyright (c) 2017 Evgeny Safronov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rmp-serde-1.1.1/README.md000064400000000000000000000047131046102023000130060ustar 00000000000000# RMP - Rust MessagePack RMP is a pure Rust [MessagePack](http://msgpack.org) implementation. [![Build Status](https://travis-ci.org/3Hren/msgpack-rust.svg?branch=master)](https://travis-ci.org/3Hren/msgpack-rust) [![Coverage Status][coveralls-img]][coveralls-url] This repository consists of three separate crates: the RMP core and two implementations to ease serializing and deserializing Rust structs. crates.rs | API Documentation | -------------------------------------------|---------------------------------| [![rmp][crates-rmp-img]][crates-rmp-url] | [RMP][rmp-docs-url] | [![rmps][crates-rmps-img]][crates-rmps-url] | [RMP Serde][rmps-docs-url] | [![rmpv][crates-rmpv-img]][crates-rmpv-url] | [RMP Value][rmpv-docs-url] | ## Features - **Convenient API** RMP is designed to be lightweight and straightforward. There are low-level API, which gives you full control on data encoding/decoding process and makes no heap allocations. On the other hand there are high-level API, which provides you convenient interface using Rust standard library and compiler reflection, allowing to encode/decode structures using `derive` attribute. - **Zero-copy value decoding** RMP allows to decode bytes from a buffer in a zero-copy manner easily and blazingly fast, while Rust static checks guarantees that the data will be valid as long as the buffer lives. - **Clear error handling** RMP's error system guarantees that you never receive an error enum with unreachable variant. - **Robust and tested** This project is developed using TDD and CI, so any found bugs will be fixed without breaking existing functionality. ## Requirements - Rust 1.53.0 or later [rustc-serialize]: https://github.com/rust-lang-nursery/rustc-serialize [serde]: https://github.com/serde-rs/serde [coveralls-img]: https://coveralls.io/repos/3Hren/msgpack-rust/badge.svg?branch=master&service=github [coveralls-url]: https://coveralls.io/github/3Hren/msgpack-rust?branch=master [rmp-docs-url]: https://docs.rs/rmp [rmps-docs-url]: https://docs.rs/rmp-serde [rmpv-docs-url]: https://docs.rs/rmpv [crates-rmp-img]: https://img.shields.io/crates/v/rmp.svg [crates-rmp-url]: https://lib.rs/crates/rmp [crates-rmps-img]: https://img.shields.io/crates/v/rmp-serde.svg [crates-rmps-url]: https://lib.rs/crates/rmp-serde [crates-rmpv-img]: https://img.shields.io/crates/v/rmpv.svg [crates-rmpv-url]: https://lib.rs/crates/rmpv rmp-serde-1.1.1/benches/buf.rs000064400000000000000000000014141046102023000142530ustar 00000000000000#![feature(test)] extern crate test; use serde::{Deserialize, Serialize}; use test::Bencher; #[bench] fn bench_strings_1000(bencher: &mut Bencher) { bench_strings(bencher, 1000) } #[bench] fn bench_strings_5000(bencher: &mut Bencher) { bench_strings(bencher, 5000) } #[bench] fn bench_strings_10000(bencher: &mut Bencher) { bench_strings(bencher, 10000) } fn bench_strings(bencher: &mut Bencher, size: usize) { let vec: Vec = ::std::iter::repeat("abcdefghijklmnopqrstuvwxyz".into()) .take(size) .collect(); let mut buf = Vec::new(); vec.serialize(&mut rmp_serde::Serializer::new(&mut buf)).unwrap(); bencher.iter(|| { >::deserialize(&mut rmp_serde::Deserializer::new(&buf[..])).unwrap(); }) } rmp-serde-1.1.1/src/config.rs000064400000000000000000000224031046102023000141250ustar 00000000000000//! Change MessagePack behavior with configuration wrappers. use rmp::encode; use serde::{Serialize, Serializer}; use crate::encode::{Error, UnderlyingWrite}; /// Represents configuration that dicatates what the serializer does. /// /// Implemented as an empty trait depending on a hidden trait in order to allow changing the /// methods of this trait without breaking backwards compatibility. pub trait SerializerConfig: sealed::SerializerConfig {} impl SerializerConfig for T {} mod sealed { use serde::{Serialize, Serializer}; use crate::encode::{Error, UnderlyingWrite}; /// This is the inner trait - the real SerializerConfig. /// /// This hack disallows external implementations and usage of SerializerConfig and thus /// allows us to change SerializerConfig methods freely without breaking backwards compatibility. pub trait SerializerConfig: Copy { fn write_struct_len(ser: &mut S, len: usize) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer; fn write_struct_field(ser: &mut S, key: &'static str, value: &T) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, T: ?Sized + Serialize; /// Encodes an enum variant ident (id or name) according to underlying writer. /// /// Used in `Serializer::serialize_*_variant` methods. fn write_variant_ident( ser: &mut S, variant_index: u32, variant: &'static str, ) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer; /// Determines the value of `Serializer::is_human_readable` and /// `Deserializer::is_human_readable`. fn is_human_readable() -> bool; } } /// The default serializer/deserializer configuration. /// /// This configuration: /// - Writes structs as a tuple, without field names /// - Writes enum variants as integers /// - Writes and reads types as binary, not human-readable // /// This is the most compact representation. #[derive(Copy, Clone, Debug)] pub struct DefaultConfig; impl sealed::SerializerConfig for DefaultConfig { fn write_struct_len(ser: &mut S, len: usize) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, { encode::write_array_len(ser.get_mut(), len as u32)?; Ok(()) } #[inline] fn write_struct_field(ser: &mut S, _key: &'static str, value: &T) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, T: ?Sized + Serialize, { value.serialize(ser) } #[inline] fn write_variant_ident( ser: &mut S, _variant_index: u32, variant: &'static str, ) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, { ser.serialize_str(variant) } #[inline(always)] fn is_human_readable() -> bool { false } } /// Config wrapper, that overrides struct serialization by packing as a map with field names. /// /// MessagePack specification does not tell how to serialize structs. This trait allows you to /// extend serialization to match your app's requirements. /// /// Default `Serializer` implementation writes structs as a tuple, i.e. only its length is encoded, /// because it is the most compact representation. #[derive(Copy, Clone, Debug)] pub struct StructMapConfig(C); impl StructMapConfig { /// Creates a `StructMapConfig` inheriting unchanged configuration options from the given configuration. #[inline] pub fn new(inner: C) -> Self { StructMapConfig(inner) } } impl sealed::SerializerConfig for StructMapConfig where C: sealed::SerializerConfig, { fn write_struct_len(ser: &mut S, len: usize) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, { encode::write_map_len(ser.get_mut(), len as u32)?; Ok(()) } fn write_struct_field(ser: &mut S, key: &'static str, value: &T) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, T: ?Sized + Serialize, { encode::write_str(ser.get_mut(), key)?; value.serialize(ser) } #[inline] fn write_variant_ident( ser: &mut S, variant_index: u32, variant: &'static str, ) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, { C::write_variant_ident(ser, variant_index, variant) } #[inline(always)] fn is_human_readable() -> bool { C::is_human_readable() } } /// Config wrapper that overrides struct serlization by packing as a tuple without field /// names. #[derive(Copy, Clone, Debug)] pub struct StructTupleConfig(C); impl StructTupleConfig { /// Creates a `StructTupleConfig` inheriting unchanged configuration options from the given configuration. #[inline] pub fn new(inner: C) -> Self { StructTupleConfig(inner) } } impl sealed::SerializerConfig for StructTupleConfig where C: sealed::SerializerConfig, { fn write_struct_len(ser: &mut S, len: usize) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, { encode::write_array_len(ser.get_mut(), len as u32)?; Ok(()) } #[inline] fn write_struct_field(ser: &mut S, _key: &'static str, value: &T) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, T: ?Sized + Serialize, { value.serialize(ser) } #[inline] fn write_variant_ident( ser: &mut S, variant_index: u32, variant: &'static str, ) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, { C::write_variant_ident(ser, variant_index, variant) } #[inline(always)] fn is_human_readable() -> bool { C::is_human_readable() } } /// Config wrapper that overrides `Serializer::is_human_readable` and /// `Deserializer::is_human_readable` to return `true`. #[derive(Copy, Clone, Debug)] pub struct HumanReadableConfig(C); impl HumanReadableConfig { /// Creates a `HumanReadableConfig` inheriting unchanged configuration options from the given configuration. #[inline] pub fn new(inner: C) -> Self { Self(inner) } } impl sealed::SerializerConfig for HumanReadableConfig where C: sealed::SerializerConfig, { #[inline] fn write_struct_len(ser: &mut S, len: usize) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, { C::write_struct_len(ser, len) } #[inline] fn write_struct_field(ser: &mut S, key: &'static str, value: &T) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, T: ?Sized + Serialize, { C::write_struct_field(ser, key, value) } #[inline] fn write_variant_ident( ser: &mut S, variant_index: u32, variant: &'static str, ) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, { C::write_variant_ident(ser, variant_index, variant) } #[inline(always)] fn is_human_readable() -> bool { true } } /// Config wrapper that overrides `Serializer::is_human_readable` and /// `Deserializer::is_human_readable` to return `false`. #[derive(Copy, Clone, Debug)] pub struct BinaryConfig(C); impl BinaryConfig { /// Creates a `BinaryConfig` inheriting unchanged configuration options from the given configuration. #[inline(always)] pub fn new(inner: C) -> Self { Self(inner) } } impl sealed::SerializerConfig for BinaryConfig where C: sealed::SerializerConfig, { #[inline] fn write_struct_len(ser: &mut S, len: usize) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, { C::write_struct_len(ser, len) } #[inline] fn write_struct_field(ser: &mut S, key: &'static str, value: &T) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, T: ?Sized + Serialize, { C::write_struct_field(ser, key, value) } #[inline] fn write_variant_ident( ser: &mut S, variant_index: u32, variant: &'static str, ) -> Result<(), Error> where S: UnderlyingWrite, for<'a> &'a mut S: Serializer, { C::write_variant_ident(ser, variant_index, variant) } #[inline(always)] fn is_human_readable() -> bool { false } } rmp-serde-1.1.1/src/decode.rs000064400000000000000000001023131046102023000141020ustar 00000000000000//! Generic MessagePack deserialization. use std::convert::TryInto; use std::error; use std::fmt::{self, Display, Formatter}; use std::io::{self, Cursor, ErrorKind, Read}; use std::num::TryFromIntError; use std::str::{self, Utf8Error}; use byteorder::{self, ReadBytesExt}; use serde; use serde::de::{self, Deserialize, DeserializeOwned, DeserializeSeed, Unexpected, Visitor}; use rmp; use rmp::decode::{self, RmpRead, DecodeStringError, MarkerReadError, NumValueReadError, ValueReadError}; use rmp::Marker; use crate::config::{BinaryConfig, DefaultConfig, HumanReadableConfig, SerializerConfig}; use crate::MSGPACK_EXT_STRUCT_NAME; /// Enum representing errors that can occur while decoding MessagePack data. #[derive(Debug)] pub enum Error { /// The enclosed I/O error occurred while trying to read a MessagePack /// marker. InvalidMarkerRead(io::Error), /// The enclosed I/O error occurred while trying to read the encoded /// MessagePack data. InvalidDataRead(io::Error), /// A mismatch occurred between the decoded and expected value types. TypeMismatch(Marker), /// A numeric cast failed due to an out-of-range error. OutOfRange, /// A decoded array did not have the enclosed expected length. LengthMismatch(u32), /// An otherwise uncategorized error occurred. See the enclosed `String` for /// details. Uncategorized(String), /// A general error occurred while deserializing the expected type. See the /// enclosed `String` for details. Syntax(String), /// An encoded string could not be parsed as UTF-8. Utf8Error(Utf8Error), /// The depth limit was exceeded. DepthLimitExceeded, } macro_rules! depth_count( ( $counter:expr, $expr:expr ) => { { $counter -= 1; if $counter == 0 { return Err(Error::DepthLimitExceeded) } let res = $expr; $counter += 1; res } } ); impl error::Error for Error { #[cold] fn source(&self) -> Option<&(dyn error::Error + 'static)> { match *self { Error::TypeMismatch(..) => None, Error::InvalidMarkerRead(ref err) => Some(err), Error::InvalidDataRead(ref err) => Some(err), Error::LengthMismatch(..) => None, Error::OutOfRange => None, Error::Uncategorized(..) => None, Error::Syntax(..) => None, Error::Utf8Error(ref err) => Some(err), Error::DepthLimitExceeded => None, } } } impl de::Error for Error { #[cold] fn custom(msg: T) -> Self { Error::Syntax(msg.to_string()) } } impl Display for Error { #[cold] fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> { match *self { Error::InvalidMarkerRead(ref err) => write!(fmt, "IO error while reading marker: {}", err), Error::InvalidDataRead(ref err) => write!(fmt, "IO error while reading data: {}", err), Error::TypeMismatch(ref actual_marker) => { write!(fmt, "wrong msgpack marker {:?}", actual_marker) } Error::OutOfRange => fmt.write_str("numeric cast found out of range"), Error::LengthMismatch(expected_length) => write!( fmt, "array had incorrect length, expected {}", expected_length ), Error::Uncategorized(ref msg) => write!(fmt, "uncategorized error: {}", msg), Error::Syntax(ref msg) => fmt.write_str(msg), Error::Utf8Error(ref err) => write!(fmt, "string found to be invalid utf8: {}", err), Error::DepthLimitExceeded => fmt.write_str("depth limit exceeded"), } } } impl From for Error { #[cold] fn from(err: MarkerReadError) -> Error { match err { MarkerReadError(err) => Error::InvalidMarkerRead(err), } } } impl From for Error { #[cold] fn from(err: Utf8Error) -> Error { Error::Utf8Error(err) } } impl From for Error { #[cold] fn from(err: ValueReadError) -> Error { match err { ValueReadError::TypeMismatch(marker) => Error::TypeMismatch(marker), ValueReadError::InvalidMarkerRead(err) => Error::InvalidMarkerRead(err), ValueReadError::InvalidDataRead(err) => Error::InvalidDataRead(err), } } } impl From for Error { #[cold] fn from(err: NumValueReadError) -> Error { match err { NumValueReadError::TypeMismatch(marker) => Error::TypeMismatch(marker), NumValueReadError::InvalidMarkerRead(err) => Error::InvalidMarkerRead(err), NumValueReadError::InvalidDataRead(err) => Error::InvalidDataRead(err), NumValueReadError::OutOfRange => Error::OutOfRange, } } } impl<'a> From> for Error { #[cold] fn from(err: DecodeStringError<'_>) -> Error { match err { DecodeStringError::InvalidMarkerRead(err) => Error::InvalidMarkerRead(err), DecodeStringError::InvalidDataRead(err) => Error::InvalidDataRead(err), DecodeStringError::TypeMismatch(marker) => Error::TypeMismatch(marker), DecodeStringError::BufferSizeTooSmall(..) => Error::Uncategorized("BufferSizeTooSmall".to_string()), DecodeStringError::InvalidUtf8(..) => Error::Uncategorized("InvalidUtf8".to_string()), } } } impl From for Error { #[cold] fn from(_: TryFromIntError) -> Self { Error::OutOfRange } } /// A Deserializer that reads bytes from a buffer. /// /// # Note /// /// All instances of `ErrorKind::Interrupted` are handled by this function and the underlying /// operation is retried. #[derive(Debug)] pub struct Deserializer { rd: R, config: C, marker: Option, depth: usize, } impl Deserializer { #[inline] fn take_or_read_marker(&mut self) -> Result { self.marker .take() .map_or_else(|| rmp::decode::read_marker(&mut self.rd), Ok) } #[inline] fn peek_or_read_marker(&mut self) -> Result { if let Some(m) = self.marker { Ok(m) } else { let m = rmp::decode::read_marker(&mut self.rd)?; Ok(self.marker.insert(m).to_owned()) } } } impl Deserializer, DefaultConfig> { /// Constructs a new `Deserializer` by consuming the given reader. #[inline] pub fn new(rd: R) -> Self { Self { rd: ReadReader::new(rd), config: DefaultConfig, // Cached marker in case of deserializing optional values. marker: None, depth: 1024, } } } impl Deserializer, C> { /// Gets a reference to the underlying reader in this decoder. #[inline(always)] pub fn get_ref(&self) -> &R { &self.rd.rd } /// Gets a mutable reference to the underlying reader in this decoder. #[inline(always)] pub fn get_mut(&mut self) -> &mut R { &mut self.rd.rd } /// Consumes this deserializer returning the underlying reader. #[inline] pub fn into_inner(self) -> R { self.rd.rd } } impl Deserializer { /// Consumes this deserializer and returns a new one, which will deserialize types with /// human-readable representations (`Deserializer::is_human_readable` will return `true`). /// /// This is primarily useful if you need to interoperate with serializations produced by older /// versions of `rmp-serde`. #[inline] pub fn with_human_readable(self) -> Deserializer> { let Deserializer { rd, config, marker, depth } = self; Deserializer { rd, config: HumanReadableConfig::new(config), marker, depth, } } /// Consumes this deserializer and returns a new one, which will deserialize types with /// binary representations (`Deserializer::is_human_readable` will return `false`). /// /// This is the default MessagePack deserialization mechanism, consuming the most compact /// representation. #[inline] pub fn with_binary(self) -> Deserializer> { let Deserializer { rd, config, marker, depth } = self; Deserializer { rd, config: BinaryConfig::new(config), marker, depth, } } } impl> Deserializer>> { /// Returns the current position of this deserializer, i.e. how many bytes were read. #[inline(always)] pub fn position(&self) -> u64 { self.rd.rd.position() } } impl<'de, R> Deserializer> where R: AsRef<[u8]> + ?Sized, { /// Constructs a new `Deserializer` from the given byte slice. #[inline(always)] pub fn from_read_ref(rd: &'de R) -> Self { Deserializer { rd: ReadRefReader::new(rd), config: DefaultConfig, marker: None, depth: 1024, } } /// Gets a reference to the underlying reader in this decoder. #[inline(always)] pub fn get_ref(&self) -> &R { self.rd.whole_slice } } impl<'de, R: ReadSlice<'de>, C: SerializerConfig> Deserializer { /// Changes the maximum nesting depth that is allowed #[inline(always)] pub fn set_max_depth(&mut self, depth: usize) { self.depth = depth; } fn read_str_data(&mut self, len: u32, visitor: V) -> Result where V: Visitor<'de> { match read_bin_data(&mut self.rd, len as u32)? { Reference::Borrowed(buf) => { match str::from_utf8(buf) { Ok(s) => visitor.visit_borrowed_str(s), Err(err) => { // Allow to unpack invalid UTF-8 bytes into a byte array. match visitor.visit_borrowed_bytes::(buf) { Ok(buf) => Ok(buf), Err(..) => Err(Error::Utf8Error(err)), } } } } Reference::Copied(buf) => { match str::from_utf8(buf) { Ok(s) => visitor.visit_str(s), Err(err) => { // Allow to unpack invalid UTF-8 bytes into a byte array. match visitor.visit_bytes::(buf) { Ok(buf) => Ok(buf), Err(..) => Err(Error::Utf8Error(err)), } } } } } } fn read_128(&mut self) -> Result<[u8; 16], Error> { let marker = self.take_or_read_marker()?; if marker != Marker::Bin8 { return Err(Error::TypeMismatch(marker)); } let len = read_u8(&mut self.rd)?; if len != 16 { return Err(Error::LengthMismatch(16)); } let buf = match read_bin_data(&mut self.rd, len as u32)? { Reference::Borrowed(buf) => buf, Reference::Copied(buf) => buf, }; Ok(buf.try_into().unwrap()) } } fn read_bin_data<'a, 'de, R: ReadSlice<'de>>(rd: &'a mut R, len: u32) -> Result, Error> { rd.read_slice(len as usize).map_err(Error::InvalidDataRead) } fn read_u8(rd: &mut R) -> Result { byteorder::ReadBytesExt::read_u8(rd).map_err(Error::InvalidDataRead) } fn read_u16(rd: &mut R) -> Result { rd.read_u16::() .map_err(Error::InvalidDataRead) } fn read_u32(rd: &mut R) -> Result { rd.read_u32::() .map_err(Error::InvalidDataRead) } fn ext_len(rd: &mut R, marker: Marker) -> Result { Ok(match marker { Marker::FixExt1 => 1, Marker::FixExt2 => 2, Marker::FixExt4 => 4, Marker::FixExt8 => 8, Marker::FixExt16 => 16, Marker::Ext8 => read_u8(rd)? as u32, Marker::Ext16 => read_u16(rd)? as u32, Marker::Ext32 => read_u32(rd)? as u32, _ => return Err(Error::TypeMismatch(marker)), }) } #[derive(Debug)] enum ExtDeserializerState { New, ReadTag, ReadBinary, } #[derive(Debug)] struct ExtDeserializer<'a, R, C> { rd: &'a mut R, _config: C, len: u32, state: ExtDeserializerState, } impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> ExtDeserializer<'a, R, C> { fn new(d: &'a mut Deserializer, len: u32) -> Self { ExtDeserializer { rd: &mut d.rd, _config: d.config, len, state: ExtDeserializerState::New, } } } impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::Deserializer<'de> for ExtDeserializer<'a, R, C> { type Error = Error; #[inline(always)] fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de> { visitor.visit_seq(self) } forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option seq bytes byte_buf map unit_struct newtype_struct struct identifier tuple enum ignored_any tuple_struct } } impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::SeqAccess<'de> for ExtDeserializer<'a, R, C> { type Error = Error; #[inline] fn next_element_seed(&mut self, seed: T) -> Result, Error> where T: DeserializeSeed<'de>, { match self.state { ExtDeserializerState::New | ExtDeserializerState::ReadTag => Ok(Some(seed.deserialize(self)?)), ExtDeserializerState::ReadBinary => Ok(None) } } } /// Deserializer for Ext SeqAccess impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::Deserializer<'de> for &mut ExtDeserializer<'a, R, C> { type Error = Error; fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de> { match self.state { ExtDeserializerState::New => { let tag = self.rd.read_data_i8()?; self.state = ExtDeserializerState::ReadTag; visitor.visit_i8(tag) } ExtDeserializerState::ReadTag => { let data = self.rd.read_slice(self.len as usize).map_err(Error::InvalidDataRead)?; self.state = ExtDeserializerState::ReadBinary; match data { Reference::Borrowed(bytes) => visitor.visit_borrowed_bytes(bytes), Reference::Copied(bytes) => visitor.visit_bytes(bytes), } } ExtDeserializerState::ReadBinary => unreachable!(), } } forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option seq bytes byte_buf map unit_struct newtype_struct tuple_struct struct identifier tuple enum ignored_any } } impl<'de, 'a, R: ReadSlice<'de>, C: SerializerConfig> serde::Deserializer<'de> for &'a mut Deserializer { type Error = Error; #[inline(always)] fn is_human_readable(&self) -> bool { C::is_human_readable() } #[inline(never)] fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de> { let marker = self.take_or_read_marker()?; match marker { Marker::Null => visitor.visit_unit(), Marker::True | Marker::False => visitor.visit_bool(marker == Marker::True), Marker::FixPos(val) => visitor.visit_u8(val), Marker::FixNeg(val) => visitor.visit_i8(val), Marker::U8 => visitor.visit_u8(self.rd.read_data_u8()?), Marker::U16 => visitor.visit_u16(self.rd.read_data_u16()?), Marker::U32 => visitor.visit_u32(self.rd.read_data_u32()?), Marker::U64 => visitor.visit_u64(self.rd.read_data_u64()?), Marker::I8 => visitor.visit_i8(self.rd.read_data_i8()?), Marker::I16 => visitor.visit_i16(self.rd.read_data_i16()?), Marker::I32 => visitor.visit_i32(self.rd.read_data_i32()?), Marker::I64 => visitor.visit_i64(self.rd.read_data_i64()?), Marker::F32 => visitor.visit_f32(self.rd.read_data_f32()?), Marker::F64 => visitor.visit_f64(self.rd.read_data_f64()?), Marker::FixStr(_) | Marker::Str8 | Marker::Str16 | Marker::Str32 => { let len = match marker { Marker::FixStr(len) => Ok(len.into()), Marker::Str8 => read_u8(&mut self.rd).map(u32::from), Marker::Str16 => read_u16(&mut self.rd).map(u32::from), Marker::Str32 => read_u32(&mut self.rd).map(u32::from), _ => unreachable!() }?; self.read_str_data(len, visitor) } Marker::FixArray(_) | Marker::Array16 | Marker::Array32 => { let len = match marker { Marker::FixArray(len) => len.into(), Marker::Array16 => read_u16(&mut self.rd)?.into(), Marker::Array32 => read_u32(&mut self.rd)?, _ => unreachable!(), }; depth_count!(self.depth, { let mut seq = SeqAccess::new(self, len); let res = visitor.visit_seq(&mut seq)?; match seq.left { 0 => Ok(res), excess => Err(Error::LengthMismatch(len - excess)), } }) } Marker::FixMap(_) | Marker::Map16 | Marker::Map32 => { let len = match marker { Marker::FixMap(len) => len.into(), Marker::Map16 => read_u16(&mut self.rd)?.into(), Marker::Map32 => read_u32(&mut self.rd)?, _ => unreachable!() }; depth_count!(self.depth, { let mut seq = MapAccess::new(self, len); let res = visitor.visit_map(&mut seq)?; match seq.left { 0 => Ok(res), excess => Err(Error::LengthMismatch(len - excess)), } }) } Marker::Bin8 | Marker::Bin16 | Marker::Bin32 => { let len = match marker { Marker::Bin8 => read_u8(&mut self.rd).map(u32::from), Marker::Bin16 => read_u16(&mut self.rd).map(u32::from), Marker::Bin32 => read_u32(&mut self.rd).map(u32::from), _ => unreachable!() }?; match read_bin_data(&mut self.rd, len)? { Reference::Borrowed(buf) => visitor.visit_borrowed_bytes(buf), Reference::Copied(buf) => visitor.visit_bytes(buf), } } Marker::FixExt1 | Marker::FixExt2 | Marker::FixExt4 | Marker::FixExt8 | Marker::FixExt16 | Marker::Ext8 | Marker::Ext16 | Marker::Ext32 => { let len = ext_len(&mut self.rd, marker)?; depth_count!(self.depth, visitor.visit_newtype_struct(ExtDeserializer::new(self, len))) } Marker::Reserved => Err(Error::TypeMismatch(Marker::Reserved)), } } fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de> { // # Important // // If a nested Option `o ∈ { Option>, Option>>, ..., Option...> }` // is visited for the first time, the marker (read from the underlying Reader) will determine // `o`'s innermost type `t`. // For subsequent visits of `o` the marker will not be re-read again but kept until type `t` // is visited. // // # Note // // Round trips of Options where `Option = None` such as `Some(None)` will fail because // they are just seriialized as `nil`. The serialization format has probably to be changed // to solve this. But as serde_json behaves the same, I think it's not worth doing this. let marker = self.take_or_read_marker()?; if marker == Marker::Null { visitor.visit_none() } else { // Keep the marker until `o`'s innermost type `t` is visited. self.marker = Some(marker); visitor.visit_some(self) } } fn deserialize_enum(self, _name: &str, _variants: &[&str], visitor: V) -> Result where V: Visitor<'de> { let marker = self.peek_or_read_marker()?; match rmp::decode::marker_to_len(&mut self.rd, marker) { Ok(len) => match len { // Enums are either encoded as maps with a single K/V pair // where the K = the variant & V = associated data // or as just the variant 1 => { self.marker = None; visitor.visit_enum(VariantAccess::new(self)) } n => Err(Error::LengthMismatch(n as u32)), }, // TODO: Check this is a string Err(_) => visitor.visit_enum(UnitVariantAccess::new(self)), } } fn deserialize_newtype_struct(self, name: &'static str, visitor: V) -> Result where V: Visitor<'de> { if name == MSGPACK_EXT_STRUCT_NAME { let marker = self.take_or_read_marker()?; let len = ext_len(&mut self.rd, marker)?; let ext_de = ExtDeserializer::new(self, len); return visitor.visit_newtype_struct(ext_de); } visitor.visit_newtype_struct(self) } fn deserialize_unit_struct(self, _name: &'static str, visitor: V) -> Result where V: Visitor<'de> { // We need to special case this so that [] is treated as a unit struct when asked for, // but as a sequence otherwise. This is because we serialize unit structs as [] rather // than as 'nil'. match self.take_or_read_marker()? { Marker::Null | Marker::FixArray(0) => visitor.visit_unit(), marker => { self.marker = Some(marker); self.deserialize_any(visitor) } } } #[inline] fn deserialize_i128(self, visitor: V) -> Result where V: Visitor<'de>, { let buf = self.read_128()?; visitor.visit_i128(i128::from_be_bytes(buf)) } #[inline] fn deserialize_u128(self, visitor: V) -> Result where V: Visitor<'de>, { let buf = self.read_128()?; visitor.visit_u128(u128::from_be_bytes(buf)) } forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string bytes byte_buf unit seq map struct identifier tuple tuple_struct ignored_any } } struct SeqAccess<'a, R, C> { de: &'a mut Deserializer, left: u32, } impl<'a, R: 'a, C> SeqAccess<'a, R, C> { #[inline] fn new(de: &'a mut Deserializer, len: u32) -> Self { SeqAccess { de, left: len, } } } impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::SeqAccess<'de> for SeqAccess<'a, R, C> { type Error = Error; #[inline] fn next_element_seed(&mut self, seed: T) -> Result, Self::Error> where T: DeserializeSeed<'de> { if self.left > 0 { self.left -= 1; Ok(Some(seed.deserialize(&mut *self.de)?)) } else { Ok(None) } } #[inline(always)] fn size_hint(&self) -> Option { self.left.try_into().ok() } } struct MapAccess<'a, R, C> { de: &'a mut Deserializer, left: u32, } impl<'a, R: 'a, C> MapAccess<'a, R, C> { fn new(de: &'a mut Deserializer, len: u32) -> Self { MapAccess { de, left: len, } } } impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::MapAccess<'de> for MapAccess<'a, R, C> { type Error = Error; #[inline] fn next_key_seed(&mut self, seed: K) -> Result, Self::Error> where K: DeserializeSeed<'de> { if self.left > 0 { self.left -= 1; seed.deserialize(&mut *self.de).map(Some) } else { Ok(None) } } #[inline] fn next_value_seed(&mut self, seed: V) -> Result where V: DeserializeSeed<'de> { seed.deserialize(&mut *self.de) } #[inline(always)] fn size_hint(&self) -> Option { self.left.try_into().ok() } } struct UnitVariantAccess<'a, R: 'a, C> { de: &'a mut Deserializer, } impl<'a, R: 'a, C> UnitVariantAccess<'a, R, C> { pub fn new(de: &'a mut Deserializer) -> Self { UnitVariantAccess { de } } } impl<'de, 'a, R: ReadSlice<'de>, C: SerializerConfig> de::EnumAccess<'de> for UnitVariantAccess<'a, R, C> { type Error = Error; type Variant = Self; #[inline] fn variant_seed(self, seed: V) -> Result<(V::Value, Self), Error> where V: de::DeserializeSeed<'de>, { let variant = seed.deserialize(&mut *self.de)?; Ok((variant, self)) } } impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::VariantAccess<'de> for UnitVariantAccess<'a, R, C> { type Error = Error; fn unit_variant(self) -> Result<(), Error> { Ok(()) } fn newtype_variant_seed(self, _seed: T) -> Result where T: de::DeserializeSeed<'de>, { Err(de::Error::invalid_type( Unexpected::UnitVariant, &"newtype variant", )) } fn tuple_variant(self, _len: usize, _visitor: V) -> Result where V: de::Visitor<'de>, { Err(de::Error::invalid_type( Unexpected::UnitVariant, &"tuple variant", )) } fn struct_variant( self, _fields: &'static [&'static str], _visitor: V, ) -> Result where V: de::Visitor<'de>, { Err(de::Error::invalid_type( Unexpected::UnitVariant, &"struct variant", )) } } struct VariantAccess<'a, R, C> { de: &'a mut Deserializer, } impl<'a, R: 'a, C> VariantAccess<'a, R, C> { pub fn new(de: &'a mut Deserializer) -> Self { VariantAccess { de } } } impl<'de, 'a, R: ReadSlice<'de>, C: SerializerConfig> de::EnumAccess<'de> for VariantAccess<'a, R, C> { type Error = Error; type Variant = Self; #[inline] fn variant_seed(self, seed: V) -> Result<(V::Value, Self), Error> where V: de::DeserializeSeed<'de>, { Ok((seed.deserialize(&mut *self.de)?, self)) } } impl<'de, 'a, R: ReadSlice<'de>, C: SerializerConfig> de::VariantAccess<'de> for VariantAccess<'a, R, C> { type Error = Error; #[inline] fn unit_variant(self) -> Result<(), Error> { decode::read_nil(&mut self.de.rd)?; Ok(()) } #[inline] fn newtype_variant_seed(self, seed: T) -> Result where T: DeserializeSeed<'de> { seed.deserialize(self.de) } #[inline] fn tuple_variant(self, len: usize, visitor: V) -> Result where V: Visitor<'de> { de::Deserializer::deserialize_tuple(self.de, len, visitor) } #[inline] fn struct_variant(self, fields: &'static [&'static str], visitor: V) -> Result where V: Visitor<'de> { de::Deserializer::deserialize_tuple(self.de, fields.len(), visitor) } } /// Unification of both borrowed and non-borrowed reference types. #[derive(Clone, Copy, Debug, PartialEq)] pub enum Reference<'b, 'c, T: ?Sized + 'static> { /// The reference is pointed at data that was borrowed. Borrowed(&'b T), /// The reference is pointed at data that was copied. Copied(&'c T), } /// Extends the `Read` trait by allowing to read slices directly by borrowing bytes. /// /// Used to allow zero-copy reading. pub trait ReadSlice<'de>: Read { /// Reads the exact number of bytes from the underlying byte-array. fn read_slice<'a>(&'a mut self, len: usize) -> Result, io::Error>; } /// Owned reader wrapper. #[derive(Debug)] pub struct ReadReader { rd: R, buf: Vec, } impl ReadReader { #[inline] fn new(rd: R) -> Self { ReadReader { rd, buf: Vec::with_capacity(128), } } } impl<'de, R: Read> ReadSlice<'de> for ReadReader { #[inline] fn read_slice<'a>(&'a mut self, len: usize) -> Result, io::Error> { self.buf.clear(); let read = self.rd.by_ref().take(len as u64).read_to_end(&mut self.buf)?; if read != len { return Err(io::ErrorKind::UnexpectedEof.into()); } Ok(Reference::Copied(&self.buf[..])) } } impl Read for ReadReader { #[inline] fn read(&mut self, buf: &mut [u8]) -> io::Result { self.rd.read(buf) } #[inline] fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { self.rd.read_exact(buf) } } /// Borrowed reader wrapper. #[derive(Debug)] pub struct ReadRefReader<'a, R: ?Sized> { whole_slice: &'a R, buf: &'a [u8], } impl<'a, T> ReadRefReader<'a, T> { /// Returns the part that hasn't been consumed yet pub fn remaining_slice(&self) -> &'a [u8] { self.buf } } impl<'a, T: AsRef<[u8]> + ?Sized> ReadRefReader<'a, T> { #[inline] fn new(rd: &'a T) -> Self { Self { whole_slice: rd, buf: rd.as_ref(), } } } impl<'a, T: AsRef<[u8]> + ?Sized> Read for ReadRefReader<'a, T> { #[inline] fn read(&mut self, buf: &mut [u8]) -> Result { self.buf.read(buf) } #[inline] fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), io::Error> { self.buf.read_exact(buf) } } impl<'de, T: AsRef<[u8]> + ?Sized> ReadSlice<'de> for ReadRefReader<'de, T> { #[inline] fn read_slice<'a>(&'a mut self, len: usize) -> Result, io::Error> { if len > self.buf.len() { return Err(ErrorKind::UnexpectedEof.into()); } let (a, b) = self.buf.split_at(len); self.buf = b; Ok(Reference::Borrowed(a)) } } #[test] fn test_as_ref_reader() { let buf = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let mut rd = ReadRefReader::new(&buf); assert_eq!(rd.read_slice(1).unwrap(), Reference::Borrowed(&[0][..])); assert_eq!(rd.read_slice(6).unwrap(), Reference::Borrowed(&[1, 2, 3, 4, 5, 6][..])); assert!(rd.read_slice(5).is_err()); assert_eq!(rd.read_slice(4).unwrap(), Reference::Borrowed(&[7, 8, 9, 10][..])); } /// Deserialize an instance of type `T` from an I/O stream of MessagePack. /// /// # Errors /// /// This conversion can fail if the structure of the Value does not match the structure expected /// by `T`. It can also fail if the structure is correct but `T`'s implementation of `Deserialize` /// decides that something is wrong with the data, for example required struct fields are missing. #[inline] pub fn from_read(rd: R) -> Result where R: Read, T: DeserializeOwned { Deserialize::deserialize(&mut Deserializer::new(rd)) } /// Deserialize a temporary scope-bound instance of type `T` from a slice, with zero-copy if possible. /// /// Deserialization will be performed in zero-copy manner whenever it is possible, borrowing the /// data from the slice itself. For example, strings and byte-arrays won't copied. /// /// # Errors /// /// This conversion can fail if the structure of the Value does not match the structure expected /// by `T`. It can also fail if the structure is correct but `T`'s implementation of `Deserialize` /// decides that something is wrong with the data, for example required struct fields are missing. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate serde_derive; /// /// // Encoded `["Bobby", 8]`. /// let buf = [0x92, 0xa5, 0x42, 0x6f, 0x62, 0x62, 0x79, 0x8]; /// /// #[derive(Debug, Deserialize, PartialEq)] /// struct Dog<'a> { /// name: &'a str, /// age: u8, /// } /// /// assert_eq!(Dog { name: "Bobby", age: 8 }, rmp_serde::from_slice(&buf).unwrap()); /// ``` #[inline(always)] #[allow(deprecated)] pub fn from_slice<'a, T>(input: &'a [u8]) -> Result where T: Deserialize<'a> { from_read_ref(input) } #[inline] #[doc(hidden)] #[deprecated(note = "use from_slice")] pub fn from_read_ref<'a, R, T>(rd: &'a R) -> Result where R: AsRef<[u8]> + ?Sized, T: Deserialize<'a>, { let mut de = Deserializer::from_read_ref(rd); Deserialize::deserialize(&mut de) } rmp-serde-1.1.1/src/encode.rs000064400000000000000000001061151046102023000141200ustar 00000000000000//! Serialize a Rust data structure into MessagePack data. use std::error; use std::fmt::{self, Display}; use std::io::Write; use serde; use serde::ser::{ SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple, SerializeTupleStruct, SerializeTupleVariant, }; use serde::Serialize; use rmp::encode::ValueWriteError; use rmp::{encode, Marker}; use crate::config::{ BinaryConfig, DefaultConfig, HumanReadableConfig, SerializerConfig, StructMapConfig, StructTupleConfig }; use crate::MSGPACK_EXT_STRUCT_NAME; /// This type represents all possible errors that can occur when serializing or /// deserializing MessagePack data. #[derive(Debug)] pub enum Error { /// Failed to write a MessagePack value. InvalidValueWrite(ValueWriteError), //TODO: This can be removed at some point /// Failed to serialize struct, sequence or map, because its length is unknown. UnknownLength, /// Invalid Data model, i.e. Serialize trait is not implmented correctly InvalidDataModel(&'static str), /// Depth limit exceeded DepthLimitExceeded, /// Catchall for syntax error messages. Syntax(String), } impl error::Error for Error { #[cold] fn source(&self) -> Option<&(dyn error::Error + 'static)> { match *self { Error::InvalidValueWrite(ref err) => Some(err), Error::UnknownLength => None, Error::InvalidDataModel(_) => None, Error::DepthLimitExceeded => None, Error::Syntax(..) => None, } } } impl Display for Error { #[cold] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match *self { Error::InvalidValueWrite(ref err) => write!(f, "invalid value write: {}", err), Error::UnknownLength => { f.write_str("attempt to serialize struct, sequence or map with unknown length") } Error::InvalidDataModel(r) => write!(f, "serialize data model is invalid: {}", r), Error::DepthLimitExceeded => f.write_str("depth limit exceeded"), Error::Syntax(ref msg) => f.write_str(msg), } } } impl From for Error { #[cold] fn from(err: ValueWriteError) -> Error { Error::InvalidValueWrite(err) } } impl serde::ser::Error for Error { /// Raised when there is general error when deserializing a type. #[cold] fn custom(msg: T) -> Error { Error::Syntax(msg.to_string()) } } /// Obtain the underlying writer. pub trait UnderlyingWrite { /// Underlying writer type. type Write: Write; /// Gets a reference to the underlying writer. fn get_ref(&self) -> &Self::Write; /// Gets a mutable reference to the underlying writer. /// /// It is inadvisable to directly write to the underlying writer. fn get_mut(&mut self) -> &mut Self::Write; /// Unwraps this `Serializer`, returning the underlying writer. fn into_inner(self) -> Self::Write; } /// Represents MessagePack serialization implementation. /// /// # Note /// /// MessagePack has no specification about how to encode enum types. Thus we are free to do /// whatever we want, so the given choice may be not ideal for you. /// /// An enum value is represented as a single-entry map whose key is the variant /// id and whose value is a sequence containing all associated data. If the enum /// does not have associated data, the sequence is empty. /// /// All instances of `ErrorKind::Interrupted` are handled by this function and the underlying /// operation is retried. // TODO: Docs. Examples. #[derive(Debug)] pub struct Serializer { wr: W, config: C, depth: usize, } impl Serializer { /// Gets a reference to the underlying writer. #[inline(always)] pub fn get_ref(&self) -> &W { &self.wr } /// Gets a mutable reference to the underlying writer. /// /// It is inadvisable to directly write to the underlying writer. #[inline(always)] pub fn get_mut(&mut self) -> &mut W { &mut self.wr } /// Unwraps this `Serializer`, returning the underlying writer. #[inline(always)] pub fn into_inner(self) -> W { self.wr } /// Changes the maximum nesting depth that is allowed. /// /// Currently unused. #[doc(hidden)] #[inline] pub fn unstable_set_max_depth(&mut self, depth: usize) { self.depth = depth; } } impl Serializer { /// Constructs a new `MessagePack` serializer whose output will be written to the writer /// specified. /// /// # Note /// /// This is the default constructor, which returns a serializer that will serialize structs /// and enums using the most compact representation. #[inline] pub fn new(wr: W) -> Self { Serializer { wr, depth: 1024, config: DefaultConfig, } } } impl<'a, W: Write + 'a, C> Serializer { #[inline] fn compound(&'a mut self) -> Result, Error> { let c = Compound { se: self }; Ok(c) } } impl<'a, W: Write + 'a, C: SerializerConfig> Serializer { #[inline] fn maybe_unknown_len_compound(&'a mut self, len: Option, f: F) -> Result, Error> where F: Fn(&mut W, u32) -> Result { Ok(MaybeUnknownLengthCompound { compound: match len { Some(len) => { f(&mut self.wr, len as u32)?; None } None => Some(UnknownLengthCompound::from(&*self)), }, se: self, }) } } impl Serializer { /// Consumes this serializer returning the new one, which will serialize structs as a map. /// /// This is used, when the default struct serialization as a tuple does not fit your /// requirements. #[inline] pub fn with_struct_map(self) -> Serializer> { let Serializer { wr, depth, config } = self; Serializer { wr, depth, config: StructMapConfig::new(config), } } /// Consumes this serializer returning the new one, which will serialize structs as a tuple /// without field names. /// /// This is the default MessagePack serialization mechanism, emitting the most compact /// representation. #[inline] pub fn with_struct_tuple(self) -> Serializer> { let Serializer { wr, depth, config } = self; Serializer { wr, depth, config: StructTupleConfig::new(config), } } /// Consumes this serializer returning the new one, which will serialize some types in /// human-readable representations (`Serializer::is_human_readable` will return `true`). Note /// that the overall representation is still binary, but some types such as IP addresses will /// be saved as human-readable strings. /// /// This is primarily useful if you need to interoperate with serializations produced by older /// versions of `rmp-serde`. #[inline] pub fn with_human_readable(self) -> Serializer> { let Serializer { wr, depth, config } = self; Serializer { wr, depth, config: HumanReadableConfig::new(config), } } /// Consumes this serializer returning the new one, which will serialize types as binary /// (`Serializer::is_human_readable` will return `false`). /// /// This is the default MessagePack serialization mechanism, emitting the most compact /// representation. #[inline] pub fn with_binary(self) -> Serializer> { let Serializer { wr, depth, config } = self; Serializer { wr, depth, config: BinaryConfig::new(config), } } } impl UnderlyingWrite for Serializer { type Write = W; #[inline(always)] fn get_ref(&self) -> &Self::Write { &self.wr } #[inline(always)] fn get_mut(&mut self) -> &mut Self::Write { &mut self.wr } #[inline(always)] fn into_inner(self) -> Self::Write { self.wr } } /// Part of serde serialization API. #[derive(Debug)] pub struct Compound<'a, W: 'a, C: 'a> { se: &'a mut Serializer, } #[derive(Debug)] #[allow(missing_docs)] pub struct ExtFieldSerializer<'a, W> { wr: &'a mut W, tag: Option, finish: bool, } /// Represents MessagePack serialization implementation for Ext. #[derive(Debug)] pub struct ExtSerializer<'a, W> { fields_se: ExtFieldSerializer<'a, W>, tuple_received: bool, } impl<'a, W: Write + 'a, C: SerializerConfig> SerializeSeq for Compound<'a, W, C> { type Ok = (); type Error = Error; #[inline] fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> { value.serialize(&mut *self.se) } #[inline(always)] fn end(self) -> Result { Ok(()) } } impl<'a, W: Write + 'a, C: SerializerConfig> SerializeTuple for Compound<'a, W, C> { type Ok = (); type Error = Error; #[inline] fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> { value.serialize(&mut *self.se) } #[inline(always)] fn end(self) -> Result { Ok(()) } } impl<'a, W: Write + 'a, C: SerializerConfig> SerializeTupleStruct for Compound<'a, W, C> { type Ok = (); type Error = Error; #[inline] fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> { value.serialize(&mut *self.se) } #[inline(always)] fn end(self) -> Result { Ok(()) } } impl<'a, W: Write + 'a, C: SerializerConfig> SerializeStruct for Compound<'a, W, C> { type Ok = (); type Error = Error; #[inline] fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> { C::write_struct_field(&mut *self.se, key, value) } #[inline(always)] fn end(self) -> Result { Ok(()) } } impl<'a, W: Write + 'a, C: SerializerConfig> SerializeTupleVariant for Compound<'a, W, C> { type Ok = (); type Error = Error; #[inline] fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> { value.serialize(&mut *self.se) } #[inline(always)] fn end(self) -> Result { Ok(()) } } impl<'a, W: Write + 'a, C: SerializerConfig> SerializeStructVariant for Compound<'a, W, C> { type Ok = (); type Error = Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> { C::write_struct_field(&mut *self.se, key, value) } #[inline(always)] fn end(self) -> Result { Ok(()) } } /// Contains a `Serializer` for sequences and maps whose length is not yet known /// and a counter for the number of elements that are encoded by the `Serializer`. #[derive(Debug)] struct UnknownLengthCompound { se: Serializer, C>, elem_count: u32, } impl From<&Serializer> for UnknownLengthCompound { fn from(se: &Serializer) -> Self { Self { se: Serializer { wr: Vec::with_capacity(128), config: se.config, depth: se.depth }, elem_count: 0 } } } /// Contains a `Serializer` for encoding elements of sequences and maps. /// /// # Note /// /// If , for example, a field inside a struct is tagged with `#serde(flatten)` the total number of /// fields of this struct will be unknown to serde because flattened fields may have name clashes /// and then will be overwritten. So, serde wants to serialize the struct as a map with an unknown /// length. /// /// For the described case a `UnknownLengthCompound` is used to encode the elements. On `end()` /// the counted length and the encoded elements will be written to the `Serializer`. A caveat is, /// that structs that contain flattened fields arem always written as a map, even when compact /// representaion is desired. /// /// Otherwise, if the length is known, the elements will be encoded directly by the `Serializer`. #[derive(Debug)] pub struct MaybeUnknownLengthCompound<'a, W: 'a, C: 'a> { se: &'a mut Serializer, compound: Option>, } impl<'a, W: Write + 'a, C: SerializerConfig> SerializeSeq for MaybeUnknownLengthCompound<'a, W, C> { type Ok = (); type Error = Error; fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> { match self.compound.as_mut() { None => value.serialize(&mut *self.se), Some(buf) => { value.serialize(&mut buf.se)?; buf.elem_count += 1; Ok(()) } } } fn end(self) -> Result { if let Some(compound) = self.compound { encode::write_array_len(&mut self.se.wr, compound.elem_count)?; self.se.wr.write_all(&compound.se.into_inner()) .map_err(ValueWriteError::InvalidDataWrite)?; } Ok(()) } } impl<'a, W: Write + 'a, C: SerializerConfig> SerializeMap for MaybeUnknownLengthCompound<'a, W, C> { type Ok = (); type Error = Error; fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> { ::serialize_element(self, key) } fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> { ::serialize_element(self, value) } fn end(self) -> Result { if let Some(compound) = self.compound { encode::write_map_len(&mut self.se.wr, compound.elem_count / 2)?; self.se.wr.write_all(&compound.se.into_inner()) .map_err(ValueWriteError::InvalidDataWrite)?; } Ok(()) } } impl<'a, W, C> serde::Serializer for &'a mut Serializer where W: Write, C: SerializerConfig, { type Ok = (); type Error = Error; type SerializeSeq = MaybeUnknownLengthCompound<'a, W, C>; type SerializeTuple = Compound<'a, W, C>; type SerializeTupleStruct = Compound<'a, W, C>; type SerializeTupleVariant = Compound<'a, W, C>; type SerializeMap = MaybeUnknownLengthCompound<'a, W, C>; type SerializeStruct = Compound<'a, W, C>; type SerializeStructVariant = Compound<'a, W, C>; fn is_human_readable(&self) -> bool { C::is_human_readable() } fn serialize_bool(self, v: bool) -> Result { encode::write_bool(&mut self.wr, v) .map_err(|err| Error::InvalidValueWrite(ValueWriteError::InvalidMarkerWrite(err))) } fn serialize_i8(self, v: i8) -> Result { self.serialize_i64(v as i64) } fn serialize_i16(self, v: i16) -> Result { self.serialize_i64(v as i64) } fn serialize_i32(self, v: i32) -> Result { self.serialize_i64(v as i64) } fn serialize_i64(self, v: i64) -> Result { encode::write_sint(&mut self.wr, v)?; Ok(()) } fn serialize_i128(self, v: i128) -> Result { self.serialize_bytes(&v.to_be_bytes()) } fn serialize_u8(self, v: u8) -> Result { self.serialize_u64(v as u64) } fn serialize_u16(self, v: u16) -> Result { self.serialize_u64(v as u64) } fn serialize_u32(self, v: u32) -> Result { self.serialize_u64(v as u64) } fn serialize_u64(self, v: u64) -> Result { encode::write_uint(&mut self.wr, v)?; Ok(()) } fn serialize_u128(self, v: u128) -> Result { self.serialize_bytes(&v.to_be_bytes()) } fn serialize_f32(self, v: f32) -> Result { encode::write_f32(&mut self.wr, v)?; Ok(()) } fn serialize_f64(self, v: f64) -> Result { encode::write_f64(&mut self.wr, v)?; Ok(()) } fn serialize_char(self, v: char) -> Result { // A char encoded as UTF-8 takes 4 bytes at most. let mut buf = [0; 4]; self.serialize_str(v.encode_utf8(&mut buf)) } fn serialize_str(self, v: &str) -> Result { encode::write_str(&mut self.wr, v)?; Ok(()) } fn serialize_bytes(self, value: &[u8]) -> Result { encode::write_bin_len(&mut self.wr, value.len() as u32)?; self.wr .write_all(value) .map_err(|err| Error::InvalidValueWrite(ValueWriteError::InvalidDataWrite(err))) } fn serialize_none(self) -> Result<(), Self::Error> { self.serialize_unit() } fn serialize_some(self, v: &T) -> Result<(), Self::Error> { v.serialize(self) } fn serialize_unit(self) -> Result { encode::write_nil(&mut self.wr) .map_err(|err| Error::InvalidValueWrite(ValueWriteError::InvalidMarkerWrite(err))) } fn serialize_unit_struct(self, _name: &'static str) -> Result { encode::write_array_len(&mut self.wr, 0)?; Ok(()) } fn serialize_unit_variant(self, _name: &str, idx: u32, variant: &'static str) -> Result { C::write_variant_ident(self, idx, variant) } fn serialize_newtype_struct(self, name: &'static str, value: &T) -> Result<(), Self::Error> { if name == MSGPACK_EXT_STRUCT_NAME { let mut ext_se = ExtSerializer::new(self); value.serialize(&mut ext_se)?; return ext_se.end(); } // Encode as if it's inner type. value.serialize(self) } fn serialize_newtype_variant(self, _name: &'static str, idx: u32, variant: &'static str, value: &T) -> Result { // encode as a map from variant idx to its attributed data, like: {idx => value} encode::write_map_len(&mut self.wr, 1)?; C::write_variant_ident(self, idx, variant)?; value.serialize(self) } fn serialize_seq(self, len: Option) -> Result { self.maybe_unknown_len_compound(len, |wr, len| encode::write_array_len(wr, len)) } //TODO: normal compund fn serialize_tuple(self, len: usize) -> Result { encode::write_array_len(&mut self.wr, len as u32)?; self.compound() } fn serialize_tuple_struct(self, _name: &'static str, len: usize) -> Result { encode::write_array_len(&mut self.wr, len as u32)?; self.compound() } fn serialize_tuple_variant(self, _name: &'static str, idx: u32, variant: &'static str, len: usize) -> Result { // encode as a map from variant idx to a sequence of its attributed data, like: {idx => [v1,...,vN]} encode::write_map_len(&mut self.wr, 1)?; C::write_variant_ident(self, idx, variant)?; self.serialize_tuple(len) } fn serialize_map(self, len: Option) -> Result { self.maybe_unknown_len_compound(len, |wr, len| encode::write_map_len(wr, len)) } fn serialize_struct(self, _name: &'static str, len: usize) -> Result { C::write_struct_len(self, len)?; self.compound() } fn serialize_struct_variant(self, name: &'static str, id: u32, variant: &'static str, len: usize) -> Result { // encode as a map from variant idx to a sequence of its attributed data, like: {idx => [v1,...,vN]} encode::write_map_len(&mut self.wr, 1)?; C::write_variant_ident(self, id, variant)?; self.serialize_struct(name, len) } } impl<'a, W: Write + 'a> serde::Serializer for &mut ExtFieldSerializer<'a, W> { type Ok = (); type Error = Error; type SerializeSeq = serde::ser::Impossible<(), Error>; type SerializeTuple = serde::ser::Impossible<(), Error>; type SerializeTupleStruct = serde::ser::Impossible<(), Error>; type SerializeTupleVariant = serde::ser::Impossible<(), Error>; type SerializeMap = serde::ser::Impossible<(), Error>; type SerializeStruct = serde::ser::Impossible<(), Error>; type SerializeStructVariant = serde::ser::Impossible<(), Error>; #[inline] fn serialize_i8(self, value: i8) -> Result { if self.tag.is_none() { self.tag.replace(value); Ok(()) } else { Err(Error::InvalidDataModel("expected i8 and bytes, unexpected second i8")) } } #[inline] fn serialize_bytes(self, val: &[u8]) -> Result { if let Some(tag) = self.tag.take() { encode::write_ext_meta(self.wr, val.len() as u32, tag)?; self.wr .write_all(val) .map_err(|err| Error::InvalidValueWrite(ValueWriteError::InvalidDataWrite(err)))?; self.finish = true; Ok(()) } else { Err(Error::InvalidDataModel("expected i8 and bytes, received bytes first")) } } #[inline] fn serialize_bool(self, _val: bool) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, bool unexpected")) } #[inline] fn serialize_i16(self, _val: i16) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, i16 unexpected")) } #[inline] fn serialize_i32(self, _val: i32) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, i32 unexpected")) } #[inline] fn serialize_i64(self, _val: i64) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, i64 unexpected")) } #[inline] fn serialize_u8(self, _val: u8) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, u8 unexpected")) } #[inline] fn serialize_u16(self, _val: u16) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, u16 unexpected")) } #[inline] fn serialize_u32(self, _val: u32) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, u32 unexpected")) } #[inline] fn serialize_u64(self, _val: u64) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, u64 unexpected")) } #[inline] fn serialize_f32(self, _val: f32) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, f32 unexpected")) } #[inline] fn serialize_f64(self, _val: f64) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, f64 unexpected")) } #[inline] fn serialize_char(self, _val: char) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, char unexpected")) } #[inline] fn serialize_str(self, _val: &str) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, str unexpected")) } #[inline] fn serialize_unit(self) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, unit unexpected")) } #[inline] fn serialize_unit_struct(self, _name: &'static str) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, unit struct unexpected")) } #[inline] fn serialize_unit_variant(self, _name: &'static str, _idx: u32, _variant: &'static str) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, unit variant unexpected")) } #[inline] fn serialize_newtype_struct(self, _name: &'static str, _value: &T) -> Result where T: Serialize { Err(Error::InvalidDataModel("expected i8 and bytes, newtype struct unexpected")) } fn serialize_newtype_variant(self, _name: &'static str, _idx: u32, _variant: &'static str, _value: &T) -> Result where T: Serialize { Err(Error::InvalidDataModel("expected i8 and bytes, newtype variant unexpected")) } #[inline] fn serialize_none(self) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, none unexpected")) } #[inline] fn serialize_some(self, _value: &T) -> Result where T: Serialize { Err(Error::InvalidDataModel("expected i8 and bytes, some unexpected")) } fn serialize_seq(self, _len: Option) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, seq unexpected")) } fn serialize_tuple(self, _len: usize) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, tuple unexpected")) } fn serialize_tuple_struct(self, _name: &'static str, _len: usize) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, tuple struct unexpected")) } fn serialize_tuple_variant(self, _name: &'static str, _idx: u32, _variant: &'static str, _len: usize) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, tuple variant unexpected")) } fn serialize_map(self, _len: Option) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, map unexpected")) } fn serialize_struct(self, _name: &'static str, _len: usize) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, struct unexpected")) } fn serialize_struct_variant(self, _name: &'static str, _idx: u32, _variant: &'static str, _len: usize) -> Result { Err(Error::InvalidDataModel("expected i8 and bytes, struct variant unexpected")) } } impl<'a, W: Write + 'a> serde::ser::Serializer for &mut ExtSerializer<'a, W> { type Ok = (); type Error = Error; type SerializeSeq = serde::ser::Impossible<(), Error>; type SerializeTuple = Self; type SerializeTupleStruct = serde::ser::Impossible<(), Error>; type SerializeTupleVariant = serde::ser::Impossible<(), Error>; type SerializeMap = serde::ser::Impossible<(), Error>; type SerializeStruct = serde::ser::Impossible<(), Error>; type SerializeStructVariant = serde::ser::Impossible<(), Error>; #[cold] fn serialize_bytes(self, _val: &[u8]) -> Result { Err(Error::InvalidDataModel("expected tuple, received bytes")) } #[cold] fn serialize_bool(self, _val: bool) -> Result { Err(Error::InvalidDataModel("expected tuple, received bool")) } #[cold] fn serialize_i8(self, _value: i8) -> Result { Err(Error::InvalidDataModel("expected tuple, received i8")) } #[cold] fn serialize_i16(self, _val: i16) -> Result { Err(Error::InvalidDataModel("expected tuple, received i16")) } #[cold] fn serialize_i32(self, _val: i32) -> Result { Err(Error::InvalidDataModel("expected tuple, received i32")) } #[cold] fn serialize_i64(self, _val: i64) -> Result { Err(Error::InvalidDataModel("expected tuple, received i64")) } #[cold] fn serialize_u8(self, _val: u8) -> Result { Err(Error::InvalidDataModel("expected tuple, received u8")) } #[cold] fn serialize_u16(self, _val: u16) -> Result { Err(Error::InvalidDataModel("expected tuple, received u16")) } #[cold] fn serialize_u32(self, _val: u32) -> Result { Err(Error::InvalidDataModel("expected tuple, received u32")) } #[cold] fn serialize_u64(self, _val: u64) -> Result { Err(Error::InvalidDataModel("expected tuple, received u64")) } #[cold] fn serialize_f32(self, _val: f32) -> Result { Err(Error::InvalidDataModel("expected tuple, received f32")) } #[cold] fn serialize_f64(self, _val: f64) -> Result { Err(Error::InvalidDataModel("expected tuple, received f64")) } #[cold] fn serialize_char(self, _val: char) -> Result { Err(Error::InvalidDataModel("expected tuple, received char")) } #[cold] fn serialize_str(self, _val: &str) -> Result { Err(Error::InvalidDataModel("expected tuple, received str")) } #[cold] fn serialize_unit(self) -> Result { Err(Error::InvalidDataModel("expected tuple, received unit")) } #[cold] fn serialize_unit_struct(self, _name: &'static str) -> Result { Err(Error::InvalidDataModel("expected tuple, received unit_struct")) } #[cold] fn serialize_unit_variant(self, _name: &'static str, _idx: u32, _variant: &'static str) -> Result { Err(Error::InvalidDataModel("expected tuple, received unit_variant")) } #[cold] fn serialize_newtype_struct(self, _name: &'static str, _value: &T) -> Result where T: Serialize { Err(Error::InvalidDataModel("expected tuple, received newtype_struct")) } #[cold] fn serialize_newtype_variant(self, _name: &'static str, _idx: u32, _variant: &'static str, _value: &T) -> Result where T: Serialize { Err(Error::InvalidDataModel("expected tuple, received newtype_variant")) } #[cold] fn serialize_none(self) -> Result { Err(Error::InvalidDataModel("expected tuple, received none")) } #[cold] fn serialize_some(self, _value: &T) -> Result where T: Serialize { Err(Error::InvalidDataModel("expected tuple, received some")) } #[cold] fn serialize_seq(self, _len: Option) -> Result { Err(Error::InvalidDataModel("expected tuple, received seq")) } fn serialize_tuple(self, _len: usize) -> Result { // FIXME check len self.tuple_received = true; Ok(self) } #[cold] fn serialize_tuple_struct(self, _name: &'static str, _len: usize) -> Result { Err(Error::InvalidDataModel("expected tuple, received tuple_struct")) } #[cold] fn serialize_tuple_variant(self, _name: &'static str, _idx: u32, _variant: &'static str, _len: usize) -> Result { Err(Error::InvalidDataModel("expected tuple, received tuple_variant")) } #[cold] fn serialize_map(self, _len: Option) -> Result { Err(Error::InvalidDataModel("expected tuple, received map")) } #[cold] fn serialize_struct(self, _name: &'static str, _len: usize) -> Result { Err(Error::InvalidDataModel("expected tuple, received struct")) } #[cold] fn serialize_struct_variant(self, _name: &'static str, _idx: u32, _variant: &'static str, _len: usize) -> Result { Err(Error::InvalidDataModel("expected tuple, received struct_variant")) } } impl<'a, W: Write + 'a> SerializeTuple for &mut ExtSerializer<'a, W> { type Ok = (); type Error = Error; #[inline] fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> { value.serialize(&mut self.fields_se) } #[inline(always)] fn end(self) -> Result { Ok(()) } } impl<'a, W: Write + 'a> ExtSerializer<'a, W> { #[inline] fn new(ser: &'a mut Serializer) -> Self { Self { fields_se: ExtFieldSerializer::new(ser), tuple_received: false, } } #[inline] fn end(self) -> Result<(), Error> { if !self.tuple_received { Err(Error::InvalidDataModel("expected tuple, received nothing")) } else { self.fields_se.end() } } } impl<'a, W: Write + 'a> ExtFieldSerializer<'a, W> { #[inline] fn new(ser: &'a mut Serializer) -> Self { Self { wr: UnderlyingWrite::get_mut(ser), tag: None, finish: false, } } #[inline] fn end(self) -> Result<(), Error> { if self.finish { Ok(()) } else { Err(Error::InvalidDataModel("expected i8 and bytes")) } } } /// Serialize the given data structure as MessagePack into the I/O stream. /// This function uses compact representation - structures as arrays /// /// Serialization can fail if `T`'s implementation of `Serialize` decides to fail. #[inline] pub fn write(wr: &mut W, val: &T) -> Result<(), Error> where W: Write + ?Sized, T: Serialize + ?Sized { val.serialize(&mut Serializer::new(wr)) } /// Serialize the given data structure as MessagePack into the I/O stream. /// This function serializes structures as maps /// /// Serialization can fail if `T`'s implementation of `Serialize` decides to fail. pub fn write_named(wr: &mut W, val: &T) -> Result<(), Error> where W: Write + ?Sized, T: Serialize + ?Sized { let mut se = Serializer::new(wr).with_struct_map(); val.serialize(&mut se) } /// Serialize the given data structure as a MessagePack byte vector. /// This method uses compact representation, structs are serialized as arrays /// /// Serialization can fail if `T`'s implementation of `Serialize` decides to fail. #[inline] pub fn to_vec(val: &T) -> Result, Error> where T: Serialize + ?Sized { let mut wr = Vec::with_capacity(128); write(&mut wr, val)?; Ok(wr) } /// Serializes data structure into byte vector as a map /// Resulting MessagePack message will contain field names /// /// # Errors /// /// Serialization can fail if `T`'s implementation of `Serialize` decides to fail. #[inline] pub fn to_vec_named(val: &T) -> Result, Error> where T: Serialize + ?Sized { let mut wr = Vec::with_capacity(128); write_named(&mut wr, val)?; Ok(wr) } rmp-serde-1.1.1/src/lib.rs000064400000000000000000000226751046102023000134410ustar 00000000000000//! This crate connects Rust MessagePack library with [`serde`][serde] providing an ability to //! easily serialize and deserialize both Rust built-in types, the standard library and custom data //! structures. //! //! ## Motivating example //! //! ``` //! let buf = rmp_serde::to_vec(&(42, "the Answer")).unwrap(); //! //! assert_eq!( //! vec![0x92, 0x2a, 0xaa, 0x74, 0x68, 0x65, 0x20, 0x41, 0x6e, 0x73, 0x77, 0x65, 0x72], //! buf //! ); //! //! assert_eq!((42, "the Answer"), rmp_serde::from_read_ref(&buf).unwrap()); //! ``` //! //! # Type-based Serialization and Deserialization //! //! Serde provides a mechanism for low boilerplate serialization & deserialization of values to and //! from MessagePack via the serialization API. //! //! To be able to serialize a piece of data, it must implement the `serde::Serialize` trait. To be //! able to deserialize a piece of data, it must implement the `serde::Deserialize` trait. Serde //! provides an annotation to automatically generate the code for these //! traits: `#[derive(Serialize, Deserialize)]`. //! //! # Examples //! //! ``` //! extern crate serde; //! #[macro_use] //! extern crate serde_derive; //! extern crate rmp_serde as rmps; //! //! use std::collections::HashMap; //! use serde::{Deserialize, Serialize}; //! use rmps::{Deserializer, Serializer}; //! //! #[derive(Debug, PartialEq, Deserialize, Serialize)] //! struct Human { //! age: u32, //! name: String, //! } //! //! fn main() { //! let mut buf = Vec::new(); //! let val = Human { //! age: 42, //! name: "John".into(), //! }; //! //! val.serialize(&mut Serializer::new(&mut buf)).unwrap(); //! } //! ``` //! //! [serde]: https://serde.rs/ #![forbid(unsafe_code)] #![warn(missing_debug_implementations, missing_docs)] #[macro_use] extern crate serde; use std::fmt::{self, Display, Formatter}; use std::str::{self, Utf8Error}; use serde::de; use serde::{Deserialize, Serialize}; pub use crate::decode::{from_read, Deserializer}; #[allow(deprecated)] pub use crate::decode::from_read_ref; pub use crate::encode::{to_vec, to_vec_named, Serializer}; pub use crate::decode::from_slice; pub mod config; pub mod decode; pub mod encode; /// Name of Serde newtype struct to Represent Msgpack's Ext /// Msgpack Ext: Ext(tag, binary) /// Serde data model: _ExtStruct((tag, binary)) /// Example Serde impl for custom type: /// /// ```ignore /// #[derive(Debug, PartialEq, Serialize, Deserialize)] /// #[serde(rename = "_ExtStruct")] /// struct ExtStruct((i8, serde_bytes::ByteBuf)); /// /// test_round(ExtStruct((2, serde_bytes::ByteBuf::from(vec![5]))), /// Value::Ext(2, vec![5])); /// ``` pub const MSGPACK_EXT_STRUCT_NAME: &str = "_ExtStruct"; /// Helper that allows both to encode and decode strings no matter whether they contain valid or /// invalid UTF-8. /// /// Regardless of validity the UTF-8 content this type will always be serialized as a string. #[derive(Clone, Debug, PartialEq)] #[doc(hidden)] pub struct Raw { s: Result, Utf8Error)>, } impl Raw { /// Constructs a new `Raw` from the UTF-8 string. #[inline] pub fn new(v: String) -> Self { Self { s: Ok(v) } } /// DO NOT USE. See #[deprecated(note = "This feature has been removed")] pub fn from_utf8(v: Vec) -> Self { match String::from_utf8(v) { Ok(v) => Raw::new(v), Err(err) => { let e = err.utf8_error(); Self { s: Err((err.into_bytes(), e)), } } } } /// Returns `true` if the raw is valid UTF-8. #[inline] pub fn is_str(&self) -> bool { self.s.is_ok() } /// Returns `true` if the raw contains invalid UTF-8 sequence. #[inline] pub fn is_err(&self) -> bool { self.s.is_err() } /// Returns the string reference if the raw is valid UTF-8, or else `None`. #[inline] pub fn as_str(&self) -> Option<&str> { match self.s { Ok(ref s) => Some(s.as_str()), Err(..) => None, } } /// Returns the underlying `Utf8Error` if the raw contains invalid UTF-8 sequence, or /// else `None`. #[inline] pub fn as_err(&self) -> Option<&Utf8Error> { match self.s { Ok(..) => None, Err((_, ref err)) => Some(err), } } /// Returns a byte slice of this raw's contents. #[inline] pub fn as_bytes(&self) -> &[u8] { match self.s { Ok(ref s) => s.as_bytes(), Err(ref err) => &err.0[..], } } /// Consumes this object, yielding the string if the raw is valid UTF-8, or else `None`. #[inline] pub fn into_str(self) -> Option { self.s.ok() } /// Converts a `Raw` into a byte vector. #[inline] pub fn into_bytes(self) -> Vec { match self.s { Ok(s) => s.into_bytes(), Err(err) => err.0, } } } impl Serialize for Raw { fn serialize(&self, se: S) -> Result where S: serde::Serializer { match self.s { Ok(ref s) => se.serialize_str(s), Err((ref b, ..)) => se.serialize_bytes(b), } } } struct RawVisitor; impl<'de> de::Visitor<'de> for RawVisitor { type Value = Raw; #[cold] fn expecting(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> { "string or bytes".fmt(fmt) } #[inline] fn visit_string(self, v: String) -> Result { Ok(Raw { s: Ok(v) }) } #[inline] fn visit_str(self, v: &str) -> Result where E: de::Error { Ok(Raw { s: Ok(v.into()) }) } #[inline] fn visit_bytes(self, v: &[u8]) -> Result where E: de::Error { let s = match str::from_utf8(v) { Ok(s) => Ok(s.into()), Err(err) => Err((v.into(), err)), }; Ok(Raw { s }) } #[inline] fn visit_byte_buf(self, v: Vec) -> Result where E: de::Error { let s = match String::from_utf8(v) { Ok(s) => Ok(s), Err(err) => { let e = err.utf8_error(); Err((err.into_bytes(), e)) } }; Ok(Raw { s }) } } impl<'de> Deserialize<'de> for Raw { #[inline] fn deserialize(de: D) -> Result where D: de::Deserializer<'de> { de.deserialize_any(RawVisitor) } } /// Helper that allows both to encode and decode strings no matter whether they contain valid or /// invalid UTF-8. /// /// Regardless of validity the UTF-8 content this type will always be serialized as a string. #[derive(Clone, Copy, Debug, PartialEq)] #[doc(hidden)] pub struct RawRef<'a> { s: Result<&'a str, (&'a [u8], Utf8Error)>, } impl<'a> RawRef<'a> { /// Constructs a new `RawRef` from the UTF-8 string. #[inline] pub fn new(v: &'a str) -> Self { Self { s: Ok(v) } } #[deprecated(note = "This feature has been removed")] pub fn from_utf8(v: &'a [u8]) -> Self { match str::from_utf8(v) { Ok(v) => RawRef::new(v), Err(err) => { Self { s: Err((v, err)) } } } } /// Returns `true` if the raw is valid UTF-8. #[inline] pub fn is_str(&self) -> bool { self.s.is_ok() } /// Returns `true` if the raw contains invalid UTF-8 sequence. #[inline] pub fn is_err(&self) -> bool { self.s.is_err() } /// Returns the string reference if the raw is valid UTF-8, or else `None`. #[inline] pub fn as_str(&self) -> Option<&str> { match self.s { Ok(s) => Some(s), Err(..) => None, } } /// Returns the underlying `Utf8Error` if the raw contains invalid UTF-8 sequence, or /// else `None`. #[inline] pub fn as_err(&self) -> Option<&Utf8Error> { match self.s { Ok(..) => None, Err((_, ref err)) => Some(err), } } /// Returns a byte slice of this raw's contents. #[inline] pub fn as_bytes(&self) -> &[u8] { match self.s { Ok(s) => s.as_bytes(), Err((bytes, _err)) => bytes, } } } impl<'a> Serialize for RawRef<'a> { fn serialize(&self, se: S) -> Result where S: serde::Serializer, { match self.s { Ok(ref s) => se.serialize_str(s), Err((ref b, ..)) => se.serialize_bytes(b), } } } struct RawRefVisitor; impl<'de> de::Visitor<'de> for RawRefVisitor { type Value = RawRef<'de>; #[cold] fn expecting(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> { "string or bytes".fmt(fmt) } #[inline] fn visit_borrowed_str(self, v: &'de str) -> Result where E: de::Error { Ok(RawRef { s: Ok(v) }) } #[inline] fn visit_borrowed_bytes(self, v: &'de [u8]) -> Result where E: de::Error { let s = match str::from_utf8(v) { Ok(s) => Ok(s), Err(err) => Err((v, err)), }; Ok(RawRef { s }) } } impl<'de> Deserialize<'de> for RawRef<'de> { #[inline] fn deserialize(de: D) -> Result where D: de::Deserializer<'de> { de.deserialize_any(RawRefVisitor) } } rmp-serde-1.1.1/tests/decode.rs000064400000000000000000000347661046102023000144750ustar 00000000000000extern crate rmp_serde as rmps; use std::fmt::{self, Formatter}; use std::io::Cursor; use serde::de; use serde::Deserialize; use crate::rmps::decode::{self, Error}; use crate::rmps::{Deserializer, Raw, RawRef}; use rmp::Marker; #[test] fn pass_nil() { let buf = [0xc0]; let mut de = Deserializer::new(&buf[..]); assert_eq!((), Deserialize::deserialize(&mut de).unwrap()); } #[test] fn fail_nil_from_reserved() { let buf = [0xc1]; let mut de = Deserializer::new(&buf[..]); let res: Result<(), Error> = Deserialize::deserialize(&mut de); match res.err() { Some(Error::TypeMismatch(Marker::Reserved)) => (), other => panic!("unexpected result: {:?}", other), } } #[test] fn pass_bool() { let buf = [0xc3, 0xc2]; let mut de = Deserializer::new(&buf[..]); assert_eq!(true, Deserialize::deserialize(&mut de).unwrap()); assert_eq!(false, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn fail_bool_from_fixint() { let buf = [0x00]; let cur = Cursor::new(&buf[..]); let mut deserializer = Deserializer::new(cur); let res: Result = Deserialize::deserialize(&mut deserializer); match res.err().unwrap() { Error::Syntax(..) => (), other => panic!("unexpected result: {:?}", other), } } #[test] fn pass_u64() { let buf = [0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(18446744073709551615u64, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_u32() { let buf = [0xce, 0xff, 0xff, 0xff, 0xff]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(4294967295u32, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn fail_u32_from_u64() { let buf = [0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let res: Result = Deserialize::deserialize(&mut de); match res.err().unwrap() { Error::Syntax(..) => (), other => panic!("unexpected result: {:?}", other), } } #[test] fn pass_u16() { let buf = [0xcd, 0xff, 0xff]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(65535u16, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_u8() { let buf = [0xcc, 0xff]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(255u8, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_u8_from_64() { let buf = [0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(42u8, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_usize() { let buf = [0xcc, 0xff]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(255usize, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_i64() { let buf = [0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(9223372036854775807i64, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_i32() { let buf = [0xd2, 0x7f, 0xff, 0xff, 0xff]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(2147483647i32, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_i16() { let buf = [0xd1, 0x7f, 0xff]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(32767i16, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_i8() { let buf = [0xd0, 0x7f]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(127i8, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_isize() { let buf = [0xd0, 0x7f]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(127isize, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_f32() { let buf = [0xca, 0x7f, 0x7f, 0xff, 0xff]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(3.4028234e38_f32, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_f64() { let buf = [0xcb, 0x40, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(42f64, Deserialize::deserialize(&mut de).unwrap()); } // spot check tests for general integers -> float conversions #[test] fn pass_i8_as_f32() { let buf = [0xd0, 0x7f]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(127f32, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_u32_as_f64() { let buf = [0xce, 0xff, 0xff, 0xff, 0xff]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!(4294967295f64, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_string() { let buf = [0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let actual: String = Deserialize::deserialize(&mut de).unwrap(); assert_eq!("le message".to_string(), actual); } #[test] fn pass_tuple() { let buf = [0x92, 0x2a, 0xce, 0x0, 0x1, 0x88, 0x94]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let actual: (u32, u32) = Deserialize::deserialize(&mut de).unwrap(); assert_eq!((42, 100500), actual); } #[ignore] #[test] fn fail_tuple_len_mismatch() { let buf = [0x92, 0x2a, 0xce, 0x0, 0x1, 0x88, 0x94]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let actual: Result<(u32,), Error> = Deserialize::deserialize(&mut de); match actual.err().unwrap() { Error::LengthMismatch(1) => (), other => panic!("unexpected result: {:?}", other), } } #[test] fn pass_option_some() { let buf = [0x1f]; let mut de = Deserializer::new(&buf[..]); let actual: Option = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Some(31), actual); } #[test] fn pass_option_none() { let buf = [0xc0]; let mut de = Deserializer::new(&buf[..]); let actual: Option = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(None, actual); } #[test] fn pass_nested_option_some() { let buf = [0x1f]; let mut de = Deserializer::new(&buf[..]); let actual: Option> = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Some(Some(31)), actual); } #[test] fn pass_nested_option_none() { let buf = [0xc0]; let mut de = Deserializer::new(&buf[..]); let actual: Option> = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(None, actual); } #[test] fn fail_option_u8_from_reserved() { let buf = [0xc1]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let actual: Result, Error> = Deserialize::deserialize(&mut de); match actual.err() { Some(Error::TypeMismatch(Marker::Reserved)) => (), other => panic!("unexpected result: {:?}", other), } } #[test] fn pass_vector() { let buf = [0x92, 0x00, 0xcc, 0x80]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let actual: Vec = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(vec![0, 128], actual); } #[test] fn pass_map() { use std::collections::HashMap; let buf = [ 0x82, // 2 (size) 0xa3, 0x69, 0x6e, 0x74, // 'int' 0xcc, 0x80, // 128 0xa3, 0x6b, 0x65, 0x79, // 'key' 0x2a, // 42 ]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let actual = Deserialize::deserialize(&mut de).unwrap(); let mut expected = HashMap::new(); expected.insert("int".to_string(), 128); expected.insert("key".to_string(), 42); assert_eq!(expected, actual); } // TODO: Merge three of them. #[test] fn pass_bin8_into_bytebuf() { use serde_bytes::ByteBuf; let buf = [0xc4, 0x02, 0xcc, 0x80]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let actual: ByteBuf = Deserialize::deserialize(&mut de).unwrap(); assert_eq!([0xcc, 0x80], actual[..]); } #[test] fn pass_bin16_into_bytebuf() { use serde_bytes::ByteBuf; let buf = [0xc5, 0x00, 0x02, 0xcc, 0x80]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let actual: ByteBuf = Deserialize::deserialize(&mut de).unwrap(); assert_eq!([0xcc, 0x80], actual[..]); } #[test] fn pass_bin32_into_bytebuf() { use serde_bytes::ByteBuf; let buf = [0xc6, 0x00, 0x00, 0x00, 0x02, 0xcc, 0x80]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let actual: ByteBuf = Deserialize::deserialize(&mut de).unwrap(); assert_eq!([0xcc, 0x80], actual[..]); } #[test] fn pass_bin8_into_bytebuf_regression_growing_buffer() { use serde_bytes::ByteBuf; // Try to deserialize large buf and a small buf let buf = [0x92, 0xc4, 0x04, 0x71, 0x75, 0x75, 0x78, 0xc4, 0x03, 0x62, 0x61, 0x72]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let (large, small): (ByteBuf, ByteBuf) = Deserialize::deserialize(&mut de).unwrap(); let (large, small): (Vec, Vec) = (large.into_vec(), small.into_vec()); assert_eq!((b"quux".to_vec(), b"bar".to_vec()), (large, small)); } #[test] fn test_deserialize_numeric() { #[derive(Debug, PartialEq)] enum FloatOrInteger { Float(f64), Integer(u64), } impl<'de> de::Deserialize<'de> for FloatOrInteger { fn deserialize(de: D) -> Result where D: de::Deserializer<'de> { struct FloatOrIntegerVisitor; impl<'de> de::Visitor<'de> for FloatOrIntegerVisitor { type Value = FloatOrInteger; fn expecting(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> { write!(fmt, "either a float or an integer") } fn visit_u64(self, value: u64) -> Result { Ok(FloatOrInteger::Integer(value)) } fn visit_f64(self, value: f64) -> Result { Ok(FloatOrInteger::Float(value)) } } de.deserialize_any(FloatOrIntegerVisitor) } } let buf = [203, 64, 36, 102, 102, 102, 102, 102, 102]; // 10.2 let mut de = Deserializer::new(&buf[..]); let x: FloatOrInteger = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(x, FloatOrInteger::Float(10.2)); let buf = [36]; // 36 let mut de = Deserializer::new(&buf[..]); let x: FloatOrInteger = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(x, FloatOrInteger::Integer(36)); } #[test] fn pass_deserializer_get_ref() { let buf = [0xc0]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!((), Deserialize::deserialize(&mut de).unwrap()); assert_eq!(1, de.get_ref().position()); } #[test] fn pass_deserializer_get_mut() { let buf = [0xc0]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!((), Deserialize::deserialize(&mut de).unwrap()); de.get_mut().set_position(0); assert_eq!((), Deserialize::deserialize(&mut de).unwrap()); } #[test] fn pass_deserializer_into_inner() { let buf = [0xc0]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); assert_eq!((), Deserialize::deserialize(&mut de).unwrap()); let cur = de.into_inner(); assert_eq!(1, cur.position()); } #[test] fn pass_deserializer_cursor_position() { let mut de = Deserializer::new(Cursor::new(vec![0xce, 0xff, 0xff, 0xff, 0xff])); assert_eq!(4294967295u32, Deserialize::deserialize(&mut de).unwrap()); assert_eq!(5, de.position()); } #[test] fn pass_from() { assert_eq!(2147483647, decode::from_read(&[0xd2, 0x7f, 0xff, 0xff, 0xff][..]).unwrap()); } #[test] fn pass_raw_valid_utf8() { let buf = vec![0xa3, 0x6b, 0x65, 0x79]; let raw: Raw = rmps::from_slice(&buf[..]).unwrap(); assert!(raw.is_str()); assert_eq!("key", raw.as_str().unwrap()); assert_eq!([0x6b, 0x65, 0x79], raw.as_bytes()); } #[test] fn pass_raw_invalid_utf8() { // >>> msgpack.dumps(msgpack.dumps([200, []])) // '\xa4\x92\xcc\xc8\x90' let buf = vec![0xa4, 0x92, 0xcc, 0xc8, 0x90]; let raw: Raw = rmps::from_slice(&buf[..]).unwrap(); assert!(raw.is_err()); assert_eq!(0, raw.as_err().unwrap().valid_up_to()); assert_eq!([0x92, 0xcc, 0xc8, 0x90], raw.as_bytes()); } #[test] fn pass_raw_ref_valid_utf8() { let buf = vec![0xa3, 0x6b, 0x65, 0x79]; let raw: RawRef<'_> = rmps::from_slice(&buf[..]).unwrap(); assert!(raw.is_str()); assert_eq!("key", raw.as_str().unwrap()); assert_eq!([0x6b, 0x65, 0x79], raw.as_bytes()); } #[test] fn pass_raw_ref_invalid_utf8() { // >>> msgpack.dumps(msgpack.dumps([200, []])) // '\xa4\x92\xcc\xc8\x90' let buf = vec![0xa4, 0x92, 0xcc, 0xc8, 0x90]; let raw: RawRef<'_> = rmps::from_slice(&buf[..]).unwrap(); assert!(raw.is_err()); assert_eq!(0, raw.as_err().unwrap().valid_up_to()); assert_eq!([0x92, 0xcc, 0xc8, 0x90], raw.as_bytes()); } #[test] fn fail_str_invalid_utf8() { let buf = vec![0xa4, 0x92, 0xcc, 0xc8, 0x90]; let err: Result = rmps::from_slice(&buf[..]); assert!(err.is_err()); match err.err().unwrap() { decode::Error::Utf8Error(err) => assert_eq!(0, err.valid_up_to()), // decode::Error::Syntax(err) => {} err => panic!("unexpected error: {:?}", err), } } #[test] fn fail_depth_limit() { #[allow(dead_code)] struct Nested { sub: Vec, } impl<'de> de::Deserialize<'de> for Nested { fn deserialize(de: D) -> Result where D: de::Deserializer<'de> { let nested = Vec::deserialize(de)?; Ok(Nested { sub: nested }) } } let mut data = Vec::new(); for _ in 0..100 { data.push(0x91u8); } let mut reader = rmp_serde::Deserializer::new(Cursor::new(data)); reader.set_max_depth(100); let res = Nested::deserialize(&mut reader); match res.err().unwrap() { decode::Error::DepthLimitExceeded => (), other => panic!("unexpected result: {:?}", other), } } rmp-serde-1.1.1/tests/decode_derive.rs000064400000000000000000000335121046102023000160170ustar 00000000000000#[macro_use] extern crate serde_derive; extern crate rmp_serde as rmps; use std::io::Cursor; use serde::Deserialize; use crate::rmps::decode::Error; use crate::rmps::Deserializer; #[test] fn pass_newtype() { let buf = [0x2a]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] struct Struct(u32); let mut de = Deserializer::new(cur); let actual: Struct = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Struct(42), actual); } #[test] fn pass_tuple_struct() { let buf = [0x92, 0x2a, 0xce, 0x0, 0x1, 0x88, 0x94]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] struct Decoded(u32, u32); let mut de = Deserializer::new(cur); let actual: Decoded = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Decoded(42, 100500), actual); } #[test] fn pass_single_field_struct() { let buf = [0x91, 0x2a]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] struct Struct { inner: u32, } let mut de = Deserializer::new(cur); let actual: Struct = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Struct { inner: 42 }, actual); } #[test] fn pass_struct() { let buf = [0x92, 0x2a, 0xce, 0x0, 0x1, 0x88, 0x94]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] struct Decoded { id: u32, value: u32, } let mut de = Deserializer::new(cur); let actual: Decoded = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Decoded { id: 42, value: 100500 }, actual); } #[test] fn pass_struct_from_map() { #[derive(Debug, PartialEq, Deserialize)] struct Struct { et: String, le: u8, shit: u8, } let buf = [ 0x83, // 3 (size) 0xa2, 0x65, 0x74, // "et" 0xa5, 0x76, 0x6f, 0x69, 0x6c, 0x61, // "voila" 0xa2, 0x6c, 0x65, // "le" 0x00, // 0 0xa4, 0x73, 0x68, 0x69, 0x74, // "shit" 0x01, // 1 ]; let cur = Cursor::new(&buf[..]); // It appears no special behavior is needed for deserializing structs encoded as maps. let mut de = Deserializer::new(cur); let actual: Struct = Deserialize::deserialize(&mut de).unwrap(); let expected = Struct { et: "voila".into(), le: 0, shit: 1 }; assert_eq!(expected, actual); } #[test] fn pass_unit_variant() { // We expect enums to be encoded as a map {variant_idx => nil} let buf = [0x81, 0x0, 0xC0, 0x81, 0x1, 0xC0]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] enum Enum { A, B, } let mut de = Deserializer::new(cur); let enum_a = Enum::deserialize(&mut de).unwrap(); let enum_b = Enum::deserialize(&mut de).unwrap(); assert_eq!(enum_a, Enum::A); assert_eq!(enum_b, Enum::B); assert_eq!(6, de.get_ref().position()); } #[test] fn pass_tuple_enum_with_arg() { // The encoded byte-array is: {1 => 42}. let buf = [0x81, 0x01, 0x2a]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] enum Enum { A, B(u32), } let mut de = Deserializer::new(cur); let actual: Enum = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Enum::B(42), actual); assert_eq!(3, de.get_ref().position()) } #[test] fn pass_tuple_enum_with_args() { // The encoded bytearray is: {1 => [42, 58]}. let buf = [0x81, 0x01, 0x92, 0x2a, 0x3a]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] enum Enum { A, B(u32, u32), } let mut de = Deserializer::new(cur); let actual: Enum = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Enum::B(42, 58), actual); assert_eq!(5, de.get_ref().position()) } #[test] fn fail_enum_map_mismatch() { let buf = [0x82, 0x0, 0x24, 0x1, 0x25]; #[derive(Debug, PartialEq, Deserialize)] enum Enum { A(i32), } let err: Result = rmps::from_slice(&buf); match err.unwrap_err() { Error::LengthMismatch(2) => (), other => panic!("unexpected result: {:?}", other), } } #[test] fn fail_enum_overflow() { // The encoded bytearray is: {1 => [42]}. let buf = [0x81, 0x01, 0x2a]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] // TODO: Rename to Enum: A, B, C, ... enum Enum { A, } let mut de = Deserializer::new(cur); let actual: Result = Deserialize::deserialize(&mut de); match actual.err().unwrap() { Error::Syntax(..) => (), other => panic!("unexpected result: {:?}", other), } } #[test] fn pass_struct_enum_with_arg() { // The encoded bytearray is: {1 => [42]}. let buf = [0x81, 0x01, 0x91, 0x2a]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] enum Enum { A, B { id: u32 }, } let mut de = Deserializer::new(cur); let actual: Enum = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Enum::B { id: 42 }, actual); assert_eq!(4, de.get_ref().position()) } #[test] fn pass_newtype_variant() { // The encoded bytearray is: {0 => 'le message'}. let buf = [0x81, 0x0, 0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] struct Newtype(String); #[derive(Debug, PartialEq, Deserialize)] enum Enum { A(Newtype), } let mut de = Deserializer::new(cur); let actual: Enum = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Enum::A(Newtype("le message".into())), actual); assert_eq!(buf.len() as u64, de.get_ref().position()) } #[cfg(disabled)] // This test doesn't actually compile anymore #[test] fn pass_enum_custom_policy() { use rmp_serde::decode::VariantVisitor; use std::io::Read; // We expect enums to be endoded as id, [...] (without wrapping tuple). let buf = [0x01, 0x90]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] enum Enum { A, B, } struct CustomDeserializer { inner: Deserializer, } impl serde::Deserializer for CustomDeserializer { type Error = Error; fn deserialize(&mut self, visitor: V) -> Result where V: serde::de::Visitor { self.inner.deserialize(visitor) } fn deserialize_enum(&mut self, _enum: &str, _variants: &'static [&'static str], mut visitor: V) -> Result where V: serde::de::EnumVisitor { visitor.visit(VariantVisitor::new(&mut self.inner)) } forward_to_deserialize! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 char str string unit seq seq_fixed_size bytes map tuple_struct unit_struct struct struct_field tuple option newtype_struct ignored_any } } let mut de = CustomDeserializer { inner: Deserializer::new(cur), }; let actual: Enum = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Enum::B, actual); assert_eq!(2, de.inner.get_ref().position()); } #[test] fn pass_struct_variant() { #[derive(Debug, PartialEq, Deserialize)] enum Custom { First { data: u32 }, Second { data: u32 }, } let out_first = vec![0x81, 0x00, 0x91, 0x2a]; let out_second = vec![0x81, 0x01, 0x91, 0x2a]; for (expected, out) in vec![(Custom::First{ data: 42 }, out_first), (Custom::Second { data: 42 }, out_second)] { let mut de = Deserializer::new(Cursor::new(&out[..])); let val: Custom = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(expected, val); } } #[test] fn pass_adjacently_tagged_enum() { // ["Foo", 123] let buf = [146, 163, 70, 111, 111, 123]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] #[serde(tag = "t", content = "c")] enum Enum { Foo(i32), Bar(u32), } let mut de = Deserializer::new(cur); let actual = Deserialize::deserialize(&mut de); assert!(actual.is_ok()); assert_eq!(Enum::Foo(123), actual.unwrap()); } #[test] #[should_panic(expected = "assertion failed")] fn fail_internally_tagged_enum_tuple() { // ["Foo", 123] let buf = [146, 163, 70, 111, 111, 123]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] #[serde(tag = "t")] enum Enum { Foo(i32), Bar(u32), } let mut de = Deserializer::new(cur); let actual: Result = Deserialize::deserialize(&mut de); assert!(actual.is_ok()) } #[test] fn pass_internally_tagged_enum_struct() { let buf = [130, 161, 116, 163, 70, 111, 111, 165, 118, 97, 108, 117, 101, 123]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] #[serde(tag = "t")] enum Enum { Foo { value: i32 }, Bar { value: u32 }, } let mut de = Deserializer::new(cur); let actual: Result = Deserialize::deserialize(&mut de); assert!(actual.is_ok()); assert_eq!(Enum::Foo { value: 123 }, actual.unwrap()) } #[test] fn pass_enum_with_one_arg() { // The encoded bytearray is: {0 => [1, 2]}. let buf = [0x81, 0x0, 0x92, 0x01, 0x02]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] enum Enum { V1(Vec), } let mut de = Deserializer::new(cur); let actual: Enum = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Enum::V1(vec![1, 2]), actual); assert_eq!(buf.len() as u64, de.get_ref().position()) } #[test] fn pass_struct_with_nested_options() { // The encoded bytearray is: [null, 13]. let buf = [0x92, 0xc0, 0x0D]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] struct Struct { f1: Option>, f2: Option>, } let mut de = Deserializer::new(cur); let actual: Struct = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(Struct { f1: None, f2: Some(Some(13)) }, actual); assert_eq!(buf.len() as u64, de.get_ref().position()); } #[test] fn pass_struct_with_flattened_map_field() { use std::collections::BTreeMap; // The encoded bytearray is: { "f1": 0, "f2": { "german": "Hallo Welt!" }, "english": "Hello World!" }. let buf = [ 0x83, 0xA2, 0x66, 0x31, 0x00, 0xA2, 0x66, 0x32, 0x81, 0xA6, 0x67, 0x65, 0x72, 0x6D, 0x61, 0x6E, 0xAB, 0x48, 0x61, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x65, 0x6C, 0x74, 0x21, 0xA7, 0x65, 0x6E, 0x67, 0x6C, 0x69, 0x73, 0x68, 0xAC, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21, ]; let cur = Cursor::new(&buf[..]); #[derive(Debug, PartialEq, Deserialize)] struct Struct { f1: u32, // not flattend! f2: BTreeMap, #[serde(flatten)] f3: BTreeMap, } let expected = Struct { f1: 0, f2: { let mut map = BTreeMap::new(); map.insert("german".to_string(), "Hallo Welt!".to_string()); map }, f3: { let mut map = BTreeMap::new(); map.insert("english".to_string(), "Hello World!".to_string()); map }, }; let mut de = Deserializer::new(cur); let actual: Struct = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(expected, actual); assert_eq!(buf.len() as u64, de.get_ref().position()); } #[test] fn pass_struct_with_flattened_struct_field() { #[derive(Debug, PartialEq, Deserialize)] struct Struct { f1: u32, // not flattend! f2: InnerStruct, #[serde(flatten)] f3: InnerStruct, } #[derive(Debug, PartialEq, Deserialize)] struct InnerStruct { f4: u32, f5: u32, } let expected = Struct { f1: 0, f2: InnerStruct { f4: 8, f5: 13 }, f3: InnerStruct { f4: 21, f5: 34 } }; // struct-as-tuple { // The encoded bytearray is: { "f1": 0, "f2": [8, 13], "f4": 21, "f5": 34 }. let buf = [ 0x84, 0xA2, 0x66, 0x31, 0x00, 0xA2, 0x66, 0x32, 0x92, 0x08, 0x0D, 0xA2, 0x66, 0x34, 0x15, 0xA2, 0x66, 0x35, 0x22, ]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let actual: Struct = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(expected, actual); assert_eq!(buf.len() as u64, de.get_ref().position()); } // struct-as-map { // The encoded bytearray is: { "f1": 0, "f2": { "f4": 8, "f5": 13 }, "f4": 21, "f5": 34 }. let buf = [ 0x84, 0xA2, 0x66, 0x31, 0x00, 0xA2, 0x66, 0x32, 0x82, 0xA2, 0x66, 0x34, 0x08, 0xA2, 0x66, 0x35, 0x0D, 0xA2, 0x66, 0x34, 0x15, 0xA2, 0x66, 0x35, 0x22, ]; let cur = Cursor::new(&buf[..]); let mut de = Deserializer::new(cur); let actual: Struct = Deserialize::deserialize(&mut de).unwrap(); assert_eq!(expected, actual); assert_eq!(buf.len() as u64, de.get_ref().position()); } } #[test] fn pass_from_slice() { let buf = [0x93, 0xa4, 0x4a, 0x6f, 0x68, 0x6e, 0xa5, 0x53, 0x6d, 0x69, 0x74, 0x68, 0x2a]; #[derive(Debug, PartialEq, Deserialize)] struct Person<'a> { name: &'a str, surname: &'a str, age: u8, } assert_eq!(Person { name: "John", surname: "Smith", age: 42 }, rmps::from_slice(&buf[..]).unwrap()); } #[test] #[allow(deprecated)] fn pass_from_ref() { let buf = [0x92, 0xa5, 0x42, 0x6f, 0x62, 0x62, 0x79, 0x8]; #[derive(Debug, Deserialize, PartialEq)] struct Dog<'a> { name: &'a str, age: u8, } assert_eq!(Dog { name: "Bobby", age: 8 }, rmps::from_read_ref(&buf).unwrap()); } rmp-serde-1.1.1/tests/encode.rs000064400000000000000000000204421046102023000144710ustar 00000000000000extern crate rmp_serde as rmps; use std::io::Cursor; use serde::Serialize; use crate::rmps::encode::{self, Error}; use crate::rmps::{Raw, RawRef, Serializer}; #[test] fn pass_null() { let mut buf = [0x00]; let val = (); val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xc0], buf); } #[test] fn fail_null() { let mut buf = []; let val = (); match val.serialize(&mut Serializer::new(&mut &mut buf[..])) { Err(Error::InvalidValueWrite(..)) => (), other => panic!("unexpected result: {:?}", other), } } #[test] fn pass_bool() { let mut buf = [0x00, 0x00]; { let mut cur = Cursor::new(&mut buf[..]); let mut encoder = Serializer::new(&mut cur); let val = true; val.serialize(&mut encoder).ok().unwrap(); let val = false; val.serialize(&mut encoder).ok().unwrap(); } assert_eq!([0xc3, 0xc2], buf); } #[test] fn pass_usize() { let mut buf = [0x00, 0x00]; let val = 255usize; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xcc, 0xff], buf); } #[test] fn pass_u8() { let mut buf = [0x00, 0x00]; let val = 255u8; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xcc, 0xff], buf); } #[test] fn pass_u16() { let mut buf = [0x00, 0x00, 0x00]; let val = 65535u16; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xcd, 0xff, 0xff], buf); } #[test] fn pass_u32() { let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00]; let val = 4294967295u32; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xce, 0xff, 0xff, 0xff, 0xff], buf); } #[test] fn pass_u64() { let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; let val = 18446744073709551615u64; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], buf); } #[test] fn pass_isize() { let mut buf = [0x00, 0x00]; let val = -128isize; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xd0, 0x80], buf); } #[test] fn pass_i8() { let mut buf = [0x00, 0x00]; let val = -128i8; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xd0, 0x80], buf); } #[test] fn pass_i16() { let mut buf = [0x00, 0x00, 0x00]; let val = -32768i16; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xd1, 0x80, 0x00], buf); } #[test] fn pass_i32() { let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00]; let val = -2147483648i32; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xd2, 0x80, 0x00, 0x00, 0x00], buf); } #[test] fn pass_i64() { let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; let val = -9223372036854775808i64; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); } #[test] fn pass_i64_most_effective() { let mut buf = [0x00, 0x00]; // This value can be represented using 2 bytes although it's i64. let val = 128i64; val.serialize(&mut Serializer::new(&mut &mut buf[..])).unwrap(); assert_eq!([0xcc, 0x80], buf); } #[test] fn pass_f32() { let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00]; let val = 3.4028234e38_f32; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xca, 0x7f, 0x7f, 0xff, 0xff], buf); } #[test] fn pass_f64() { let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; let val = 42f64; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xcb, 0x40, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); } #[test] fn pass_char() { let mut buf = [0x00, 0x00]; let val = '!'; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xa1, 0x21], buf); } #[test] fn pass_string() { let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; let val = "le message"; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65], buf); } #[test] fn pass_tuple() { let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; let val = (42u32, 100500u32); val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0x92, 0x2a, 0xce, 0x0, 0x1, 0x88, 0x94], buf); } #[test] fn pass_option_some() { let mut buf = [0x00]; let val = Some(100u32); val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0x64], buf); } #[test] fn pass_option_none() { let mut buf = [0x00]; let val: Option = None; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0xc0], buf); } #[test] fn pass_seq() { let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; let val = vec!["le", "shit"]; val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); assert_eq!([0x92, 0xa2, 0x6c, 0x65, 0xa4, 0x73, 0x68, 0x69, 0x74], buf); } #[test] fn pass_map() { use std::collections::BTreeMap; let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; let mut val = BTreeMap::new(); val.insert(0u8, "le"); val.insert(1u8, "shit"); val.serialize(&mut Serializer::new(&mut &mut buf[..])).ok().unwrap(); let out = [ 0x82, // 2 (size) 0x00, // 0 0xa2, 0x6c, 0x65, // "le" 0x01, // 1 0xa4, 0x73, 0x68, 0x69, 0x74, // "shit" ]; assert_eq!(out, buf); } #[test] fn pass_empty_map() { use std::collections::BTreeMap; let mut buf = vec![]; let val: BTreeMap = BTreeMap::new(); val.serialize(&mut Serializer::new(&mut buf)).ok().unwrap(); let out = vec![ 0x80, // (size: 0) ]; assert_eq!(out, buf); } #[test] fn pass_encoding_struct_into_vec() { let val = (42u8, "the Answer"); let mut buf: Vec = Vec::new(); val.serialize(&mut Serializer::new(&mut buf)).unwrap(); assert_eq!(vec![0x92, 0x2a, 0xaa, 0x74, 0x68, 0x65, 0x20, 0x41, 0x6e, 0x73, 0x77, 0x65, 0x72], buf); } #[test] fn pass_bin() { use serde_bytes::Bytes; let mut buf = Vec::new(); let val = Bytes::new(&[0xcc, 0x80]); val.serialize(&mut Serializer::new(&mut buf)).ok().unwrap(); assert_eq!(vec![0xc4, 0x02, 0xcc, 0x80], buf); } #[test] fn pass_to_vec() { assert_eq!(vec![0xc0], encode::to_vec(&()).unwrap()); assert_eq!(vec![0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65], encode::to_vec("le message").unwrap()); } #[test] fn get_mut() { let mut se = Serializer::new(Vec::new()); true.serialize(&mut se).unwrap(); assert_eq!(&vec![0xc3], se.get_ref()); se.get_mut().push(42); assert_eq!(vec![0xc3, 42], se.into_inner()); } #[test] fn pass_raw_valid_utf8() { let raw = Raw::new("key".into()); let mut buf = Vec::new(); raw.serialize(&mut Serializer::new(&mut buf)).unwrap(); assert_eq!(vec![0xa3, 0x6b, 0x65, 0x79], buf); } #[test] #[allow(deprecated)] fn pass_raw_invalid_utf8() { // >>> msgpack.dumps(msgpack.dumps([200, []])) // '\xa4\x92\xcc\xc8\x90' let raw = Raw::from_utf8(vec![0x92, 0xcc, 0xc8, 0x90]); let mut buf = Vec::new(); raw.serialize(&mut Serializer::new(&mut buf)).unwrap(); assert_eq!(vec![196, 4, 146, 204, 200, 144], buf); } #[test] fn pass_raw_ref_valid_utf8() { let raw = RawRef::new("key"); let mut buf = Vec::new(); raw.serialize(&mut Serializer::new(&mut buf)).unwrap(); assert_eq!(vec![0xa3, 0x6b, 0x65, 0x79], buf); } #[test] #[allow(deprecated)] fn pass_raw_ref_invalid_utf8() { // >>> msgpack.dumps(msgpack.dumps([200, []])) // '\xa4\x92\xcc\xc8\x90' let b = &[0x92, 0xcc, 0xc8, 0x90]; let raw = RawRef::from_utf8(b); let mut buf = Vec::new(); raw.serialize(&mut Serializer::new(&mut buf)).unwrap(); assert_eq!(vec![196, 4, 146, 204, 200, 144], buf); } #[test] fn serializer_one_type_arg() { let _s: rmp_serde::Serializer<&mut dyn std::io::Write>; } rmp-serde-1.1.1/tests/encode_derive.rs000064400000000000000000000205251046102023000160310ustar 00000000000000#[macro_use] extern crate serde_derive; extern crate rmp_serde as rmps; use crate::rmps::Serializer; use serde::Serialize; #[test] fn pass_unit_struct() { #[derive(Serialize)] struct Unit; let mut buf = Vec::new(); Unit.serialize(&mut Serializer::new(&mut buf)).unwrap(); // Expect: []. assert_eq!(vec![0x90], buf); } #[test] fn pass_unit_variant() { #[derive(Serialize)] enum Enum { V1, V2, } let mut buf = Vec::new(); Enum::V1.serialize(&mut Serializer::new(&mut buf)).unwrap(); Enum::V2.serialize(&mut Serializer::new(&mut buf)).unwrap(); // Expect: "V1", "V2" assert_eq!(vec![0xa2, 0x56, 0x31, 0xa2, 0x56, 0x32], buf); } #[test] fn pass_newtype_struct() { #[derive(Serialize)] struct Struct(u64); let val = Struct(42); let mut buf = Vec::new(); val.serialize(&mut Serializer::new(&mut buf)).unwrap(); assert_eq!(vec![0x2a], buf); } #[test] fn pass_newtype_variant() { #[derive(Serialize)] enum Enum { V2(u64), } let mut buf = Vec::new(); Enum::V2(42).serialize(&mut Serializer::new(&mut buf)).unwrap(); // Expect: {"V2" => 42} assert_eq!(buf, vec![0x81, 0xa2, 0x56, 0x32, 42]); } #[test] fn pass_untagged_newtype_variant() { #[derive(Serialize)] #[serde(untagged)] enum Enum1 { A(u64), B(Enum2), } #[derive(Serialize)] enum Enum2 { C, } let buf1 = rmps::to_vec(&Enum1::A(123)).unwrap(); let buf2 = rmps::to_vec(&Enum1::B(Enum2::C)).unwrap(); assert_eq!(buf1, [123]); // Expect: "C" assert_eq!(buf2, [0xa1, 0x43]); } #[test] fn pass_tuple_struct() { #[derive(Serialize)] struct Struct(u32, u64); let val = Struct(42, 100500); let mut buf = Vec::new(); val.serialize(&mut Serializer::new(&mut buf)).unwrap(); // Expect: [42, 100500]. assert_eq!(vec![0x92, 0x2a, 0xce, 0x00, 0x01, 0x88, 0x94], buf); } #[test] fn pass_tuple_variant() { #[derive(Serialize)] enum Enum { V1, V2(u32, u64), } let mut buf = Vec::new(); Enum::V1.serialize(&mut Serializer::new(&mut buf)).unwrap(); Enum::V2(42, 100500).serialize(&mut Serializer::new(&mut buf)).unwrap(); // Expect: {0 => nil} {1 => [42, 100500]} // Expect: "V1", {"V2" => [42, 100500] } assert_eq!( vec![0xa2, 0x56, 0x31, 0x81, 0xa2, 0x56, 0x32, 0x92, 0x2a, 0xce, 0x0, 0x1, 0x88, 0x94], buf ); } #[test] fn pass_struct() { #[derive(Serialize)] struct Struct { f1: u32, f2: u32, } let val = Struct { f1: 42, f2: 100500, }; let mut buf = Vec::new(); val.serialize(&mut Serializer::new(&mut buf)).unwrap(); // Expect: [42, 100500]. assert_eq!(vec![0x92, 0x2a, 0xce, 0x0, 0x1, 0x88, 0x94], buf); } #[test] fn serialize_struct_variant() { #[derive(Serialize)] enum Enum { V1 { f1: u32 }, V2 { f1: u32 }, } let mut buf = Vec::new(); Enum::V1 { f1: 42 }.serialize(&mut Serializer::new(&mut buf)).unwrap(); Enum::V2 { f1: 43 }.serialize(&mut Serializer::new(&mut buf)).unwrap(); // Expect: { "V1" => [42] } { "V2" => [43] } assert_eq!( vec![0x81, 0xa2, 0x56, 0x31, 0x91, 0x2a, 0x81, 0xa2, 0x56, 0x32, 0x91, 0x2b], buf ); } #[test] fn serialize_struct_variant_as_map() { #[derive(Serialize)] enum Enum { V1 { f1: u32, } } let mut se = Serializer::new(Vec::new()).with_struct_map(); Enum::V1 { f1: 42 }.serialize(&mut se).unwrap(); // Expect: {"V1" => {"f1": 42}}. assert_eq!( vec![0x81, 0xa2, 0x56, 0x31, 0x81, 0xa2, 0x66, 0x31, 0x2a], se.into_inner() ); } #[test] fn serialize_struct_with_flattened_map_field() { use std::collections::BTreeMap; #[derive(Serialize)] struct Struct { f1: u32, // not flattend! f2: BTreeMap, #[serde(flatten)] f3: BTreeMap, } let mut se = Serializer::new(Vec::new()); Struct { f1: 0, f2: { let mut map = BTreeMap::new(); map.insert("german".to_string(), "Hallo Welt!".to_string()); map }, f3: { let mut map = BTreeMap::new(); map.insert("english".to_string(), "Hello World!".to_string()); map }, } .serialize(&mut se).unwrap(); // Expect: { "f1": 0, "f2": { "german": "Hallo Welt!" }, "english": "Hello World!" }. assert_eq!( vec![ 0x83, 0xA2, 0x66, 0x31, 0x00, 0xA2, 0x66, 0x32, 0x81, 0xA6, 0x67, 0x65, 0x72, 0x6D, 0x61, 0x6E, 0xAB, 0x48, 0x61, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x65, 0x6C, 0x74, 0x21, 0xA7, 0x65, 0x6E, 0x67, 0x6C, 0x69, 0x73, 0x68, 0xAC, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21, ], se.into_inner() ); } #[test] fn serialize_struct_with_flattened_struct_field() { #[derive(Serialize)] struct Struct { f1: u32, // not flattend! f2: InnerStruct, #[serde(flatten)] f3: InnerStruct, } #[derive(Serialize)] struct InnerStruct { f4: u32, f5: u32, } let strct = Struct { f1: 0, f2: InnerStruct { f4: 8, f5: 13 }, f3: InnerStruct { f4: 21, f5: 34 }, }; // struct-as-tuple { let mut se = Serializer::new(Vec::new()); strct.serialize(&mut se).unwrap(); // Expect: { "f1": 0, "f2": [8, 13], "f4": 21, "f5": 34 }. assert_eq!(vec![ 0x84, 0xA2, 0x66, 0x31, 0x00, 0xA2, 0x66, 0x32, 0x92, 0x08, 0x0D, 0xA2, 0x66, 0x34, 0x15, 0xA2, 0x66, 0x35, 0x22, ], se.into_inner()); } // struct-as-map { let mut se = Serializer::new(Vec::new()).with_struct_map(); strct.serialize(&mut se).unwrap(); // Expect: { "f1": 0, "f2": { "f4": 8, "f5": 13 }, "f4": 21, "f5": 34 }. assert_eq!( vec![ 0x84, 0xA2, 0x66, 0x31, 0x00, 0xA2, 0x66, 0x32, 0x82, 0xA2, 0x66, 0x34, 0x08, 0xA2, 0x66, 0x35, 0x0D, 0xA2, 0x66, 0x34, 0x15, 0xA2, 0x66, 0x35, 0x22, ], se.into_inner() ); } } #[test] fn pass_struct_as_map_using_ext() { #[derive(Serialize)] struct Dog<'a> { name: &'a str, age: u16, } let dog = Dog { name: "Bobby", age: 8, }; let mut se = Serializer::new(Vec::new()).with_struct_map(); dog.serialize(&mut se).unwrap(); // Expect: {"name": "Bobby", "age": 8}. assert_eq!(vec![0x82, 0xa4, 0x6e, 0x61, 0x6d, 0x65, 0xa5, 0x42, 0x6f, 0x62, 0x62, 0x79, 0xa3, 0x61, 0x67, 0x65, 0x08], se.into_inner()); } #[test] fn pass_struct_as_tuple_using_double_ext() { #[derive(Serialize)] struct Dog<'a> { name: &'a str, age: u16, } let dog = Dog { name: "Bobby", age: 8, }; let mut se = Serializer::new(Vec::new()) .with_struct_map() .with_struct_tuple(); dog.serialize(&mut se).unwrap(); assert_eq!(vec![0x92, 0xa5, 0x42, 0x6f, 0x62, 0x62, 0x79, 0x08], se.into_inner()); } #[test] fn pass_struct_as_map_using_triple_ext() { #[derive(Serialize)] struct Dog<'a> { name: &'a str, age: u16, } let dog = Dog { name: "Bobby", age: 8, }; let mut se = Serializer::new(Vec::new()) .with_struct_map() .with_struct_tuple() .with_struct_map(); dog.serialize(&mut se).unwrap(); // Expect: {"name": "Bobby", "age": 8}. assert_eq!(vec![0x82, 0xa4, 0x6e, 0x61, 0x6d, 0x65, 0xa5, 0x42, 0x6f, 0x62, 0x62, 0x79, 0xa3, 0x61, 0x67, 0x65, 0x08], se.into_inner()); } #[test] fn pass_struct_as_map_using_triple_ext_many_times() { #[derive(Serialize)] struct Dog<'a> { name: &'a str, age: u16, } let dog = Dog { name: "Bobby", age: 8, }; let mut se = Serializer::new(Vec::new()) .with_struct_map() .with_struct_tuple() .with_struct_map() .with_struct_map() .with_struct_map() .with_struct_map(); dog.serialize(&mut se).unwrap(); // Expect: {"name": "Bobby", "age": 8}. assert_eq!(vec![0x82, 0xa4, 0x6e, 0x61, 0x6d, 0x65, 0xa5, 0x42, 0x6f, 0x62, 0x62, 0x79, 0xa3, 0x61, 0x67, 0x65, 0x08], se.into_inner()); } rmp-serde-1.1.1/tests/round.rs000064400000000000000000000436271046102023000143750ustar 00000000000000#[macro_use] extern crate serde_derive; use rmp_serde as rmps; use rmps::config::{DefaultConfig, SerializerConfig}; use rmps::decode::ReadReader; use rmps::{Deserializer, Serializer}; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::io::Cursor; #[test] #[ignore] fn issue_250() { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(tag = "type", content = "payload")] pub enum Example { Unit1, Unit2, HasValue { x: u32 }, TupleWithValue(u32, u32), InnerValue(SomeInnerValue), } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct SomeInnerValue { pub a: u32, pub b: String, } let v = rmp_serde::to_vec(&Example::HasValue { x: 3 }).unwrap(); // Fails unwrap with Syntax("invalid type: sequence, // expected struct variant Example::HasValue") let ex: Example = rmp_serde::from_slice(&v).unwrap(); assert_eq!(Example::HasValue { x: 3 }, ex); let v = rmp_serde::to_vec(&Example::Unit1).unwrap(); // Fails unwrap with Syntax("invalid length 1, // expected adjacently tagged enum Example") let ex: Example = rmp_serde::from_slice(&v).unwrap(); assert_eq!(Example::Unit1, ex); } #[test] fn round_trip_option() { #[derive(Debug, PartialEq, Serialize, Deserialize)] struct Foo { v: Option>, } let expected = Foo { v: None }; let mut buf = Vec::new(); expected.serialize(&mut Serializer::new(&mut buf)).unwrap(); let mut de = Deserializer::new(Cursor::new(&buf[..])); assert_eq!(expected, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn round_trip_nested_option() { #[derive(Debug, PartialEq, Serialize, Deserialize)] struct Struct { f1: Option>, f2: Option>, } let expected = Struct { f1: Some(Some(13)), f2: None, }; let mut buf = Vec::new(); expected.serialize(&mut Serializer::new(&mut buf)).unwrap(); let mut de = Deserializer::new(Cursor::new(&buf[..])); assert_eq!(expected, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn round_trip_optional_enum() { #[derive(Serialize, Deserialize, Debug, PartialEq)] pub enum SimpleEnum { Variant, } let expected = Some(SimpleEnum::Variant); let mut buf = Vec::new(); expected.serialize(&mut Serializer::new(&mut buf)).unwrap(); let mut de = Deserializer::new(Cursor::new(&buf[..])); assert_eq!(expected, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn round_trip_cow() { #[derive(Serialize, Deserialize, Debug, PartialEq)] struct Foo<'a> { v: Cow<'a, [u8]>, } let expected = Foo { v: Cow::Borrowed(&[]), }; let mut buf = Vec::new(); expected.serialize(&mut Serializer::new(&mut buf)).unwrap(); let mut de = Deserializer::new(Cursor::new(&buf[..])); assert_eq!(expected, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn round_trip_option_cow() { use serde::Serialize; use std::borrow::Cow; use std::io::Cursor; #[derive(Serialize, Deserialize, Debug, PartialEq)] struct Foo<'a> { v: Option>, } let expected = Foo { v: None }; let mut buf = Vec::new(); expected.serialize(&mut Serializer::new(&mut buf)).unwrap(); let mut de = Deserializer::new(Cursor::new(&buf[..])); assert_eq!(expected, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn round_struct_like_enum() { use serde::Serialize; #[derive(Serialize, Deserialize, Debug, PartialEq)] enum Enum { A { data: u32 }, } let expected = Enum::A { data: 42 }; let mut buf = Vec::new(); expected.serialize(&mut Serializer::new(&mut buf)).unwrap(); let mut de = Deserializer::new(&buf[..]); assert_eq!(expected, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn round_struct_like_enum_with_struct_map() { use serde::Serialize; #[derive(Serialize, Deserialize, Debug, PartialEq)] enum Enum { A { data: u32 }, } let expected = Enum::A { data: 42 }; let mut buf = Vec::new(); expected .serialize(&mut Serializer::new(&mut buf).with_struct_map()) .unwrap(); let mut de = Deserializer::new(&buf[..]); assert_eq!(expected, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn round_struct_like_enum_with_struct_tuple() { use serde::Serialize; #[derive(Serialize, Deserialize, Debug, PartialEq)] enum Enum { A { data: u32 }, } let expected = Enum::A { data: 42 }; let mut buf = Vec::new(); expected .serialize(&mut Serializer::new(&mut buf).with_struct_tuple()) .unwrap(); let mut de = Deserializer::new(&buf[..]); assert_eq!(expected, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn round_enum_with_newtype_struct() { use serde::Serialize; #[derive(Serialize, Deserialize, Debug, PartialEq)] struct Newtype(String); #[derive(Serialize, Deserialize, Debug, PartialEq)] enum Enum { A(Newtype), } let expected = Enum::A(Newtype("le message".into())); let mut buf = Vec::new(); expected.serialize(&mut Serializer::new(&mut buf)).unwrap(); let mut de = Deserializer::new(&buf[..]); assert_eq!(expected, Deserialize::deserialize(&mut de).unwrap()); } #[test] fn round_trip_untagged_enum_with_enum_associated_data() { #[derive(Serialize, Deserialize, Debug, PartialEq)] #[serde(untagged)] enum Foo { A(Bar), } #[derive(Serialize, Deserialize, Debug, PartialEq)] enum Bar { B, C(String), D(u64, u64, u64), E { f1: String }, } let data1_1 = Foo::A(Bar::B); let bytes_1 = rmps::to_vec(&data1_1).unwrap(); let data1_2 = rmps::from_slice(&bytes_1).unwrap(); assert_eq!(data1_1, data1_2); let data2_1 = Foo::A(Bar::C("Hello".into())); let bytes_2 = rmps::to_vec(&data2_1).unwrap(); let data2_2 = rmps::from_slice(&bytes_2).unwrap(); assert_eq!(data2_1, data2_2); let data3_1 = Foo::A(Bar::D(1, 2, 3)); let bytes_3 = rmps::to_vec(&data3_1).unwrap(); let data3_2 = rmps::from_slice(&bytes_3).unwrap(); assert_eq!(data3_1, data3_2); let data4_1 = Foo::A(Bar::E { f1: "Hello".into() }); let bytes_4 = rmps::to_vec(&data4_1).unwrap(); let data4_2 = rmps::from_slice(&bytes_4).unwrap(); assert_eq!(data4_1, data4_2); } // Checks whether deserialization and serialization can both work with structs as maps #[test] fn round_struct_as_map() { use crate::rmps::decode::from_slice; use crate::rmps::to_vec_named; #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct Dog1 { name: String, age: u16, } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct Dog2 { age: u16, name: String, } let dog1 = Dog1 { name: "Frankie".into(), age: 42, }; let serialized: Vec = to_vec_named(&dog1).unwrap(); let deserialized: Dog2 = from_slice(&serialized).unwrap(); let check = Dog1 { age: deserialized.age, name: deserialized.name, }; assert_eq!(dog1, check); } #[test] fn round_struct_as_map_in_vec() { // See: issue #205 use crate::rmps::decode::from_slice; use crate::rmps::to_vec_named; #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct Dog1 { name: String, age: u16, } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct Dog2 { age: u16, name: String, } let dog1 = Dog1 { name: "Frankie".into(), age: 42, }; let data = vec![dog1]; let serialized: Vec = to_vec_named(&data).unwrap(); let deserialized: Vec = from_slice(&serialized).unwrap(); let dog2 = &deserialized[0]; assert_eq!(dog2.name, "Frankie"); assert_eq!(dog2.age, 42); } #[test] fn round_trip_unit_struct() { #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct Message1 { data: u8, } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct Message2; #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] enum Messages { Message1(Message1), Message2(Message2), } let msg2 = Messages::Message2(Message2); // struct-as-tuple { let serialized: Vec = rmps::to_vec(&msg2).unwrap(); let deserialized: Messages = rmps::from_slice(&serialized).unwrap(); assert_eq!(deserialized, msg2); } // struct-as-map { let serialized: Vec = rmps::to_vec_named(&msg2).unwrap(); let deserialized: Messages = rmps::from_slice(&serialized).unwrap(); assert_eq!(deserialized, msg2); } } #[test] #[ignore] fn round_trip_unit_struct_untagged_enum() { #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct UnitStruct; #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct MessageA { some_int: i32, unit: UnitStruct, } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] #[serde(untagged)] enum Messages { MessageA(MessageA), } let msga = Messages::MessageA(MessageA { some_int: 32, unit: UnitStruct, }); // struct-as-tuple { let serialized: Vec = rmps::to_vec(&msga).unwrap(); let deserialized: Messages = rmps::from_slice(&serialized).unwrap(); assert_eq!(deserialized, msga); } // struct-as-map { let serialized: Vec = rmps::to_vec_named(&msga).unwrap(); let deserialized: Messages = rmps::from_slice(&serialized).unwrap(); assert_eq!(deserialized, msga); } } #[test] fn round_trip_struct_with_flattened_map_field() { use std::collections::BTreeMap; #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct Struct { f1: u32, // not flattend! f2: BTreeMap, #[serde(flatten)] f3: BTreeMap, } let strct = Struct { f1: 0, f2: { let mut map = BTreeMap::new(); map.insert("german".to_string(), "Hallo Welt!".to_string()); map }, f3: { let mut map = BTreeMap::new(); map.insert("english".to_string(), "Hello World!".to_string()); map }, }; let serialized: Vec = rmps::to_vec(&strct).unwrap(); let deserialized: Struct = rmps::from_slice(&serialized).unwrap(); assert_eq!(deserialized, strct); } #[test] fn round_trip_struct_with_flattened_struct_field() { #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct Struct { f1: u32, // not flattend! f2: InnerStruct, #[serde(flatten)] f3: InnerStruct, } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct InnerStruct { f4: u32, f5: u32, } let strct = Struct { f1: 0, f2: InnerStruct { f4: 8, f5: 13 }, f3: InnerStruct { f4: 21, f5: 34 }, }; // struct-as-tuple { let serialized: Vec = rmps::to_vec(&strct).unwrap(); let deserialized: Struct = rmps::from_slice(&serialized).unwrap(); assert_eq!(deserialized, strct); } // struct-as-map { let serialized: Vec = rmps::to_vec_named(&strct).unwrap(); let deserialized: Struct = rmps::from_slice(&serialized).unwrap(); assert_eq!(deserialized, strct); } } // Checks whether deserialization and serialization can both work with enum variants as strings #[test] fn round_variant_string() { use crate::rmps::decode::from_slice; #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] enum Animal1 { Dog { breed: String }, Cat, Emu, } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] enum Animal2 { Emu, Dog { breed: String }, Cat, } // use helper macro so that we can test many combinations at once. Needs to be a macro to deal // with the serializer owning a reference to the Vec. macro_rules! do_test { ($ser:expr) => { { let animal1 = Animal1::Dog { breed: "Pitbull".to_owned() }; let expected = Animal2::Dog { breed: "Pitbull".to_owned() }; let mut buf = Vec::new(); animal1.serialize(&mut $ser(&mut buf)).unwrap(); let deserialized: Animal2 = from_slice(&buf).unwrap(); assert_eq!(deserialized, expected); } } } do_test!(Serializer::new); do_test!(|b| Serializer::new(b).with_struct_map()); do_test!(|b| Serializer::new(b).with_struct_tuple()); do_test!(|b| Serializer::new(b).with_struct_map()); do_test!(|b| Serializer::new(b).with_struct_tuple()); do_test!(|b| { Serializer::new(b) .with_struct_tuple() .with_struct_map() .with_struct_tuple() .with_struct_map() }); } use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; #[test] fn roundtrip_ipv4addr() { assert_roundtrips(Ipv4Addr::new(127, 0, 0, 1)); } #[test] fn roundtrip_ipv6addr() { assert_roundtrips(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8)); } #[test] fn roundtrip_ipaddr_ipv4addr() { assert_roundtrips(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))); } #[test] fn roundtrip_ipaddr_ipv6addr() { assert_roundtrips(IpAddr::V6(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8))); } #[test] fn roundtrip_result_ipv4addr() { let val: Result = Ok(Ipv4Addr::new(127, 0, 0, 1)); assert_roundtrips(val); } #[test] fn roundtrip_result_num() { assert_roundtrips(Ok::(42)); assert_roundtrips(Err::<(), _>(222)); } #[test] fn roundtrip_simple_enum() { #[derive(PartialEq, Debug, Serialize, Deserialize)] enum SimpleEnum { V1(u32), V2(String), } assert_roundtrips(SimpleEnum::V1(42)); assert_roundtrips(SimpleEnum::V2("hello".into())); } #[test] fn roundtrip_some() { #[derive(PartialEq, Debug, Serialize, Deserialize)] struct Wrapper(T); assert_roundtrips(Some(99)); assert_roundtrips(Wrapper(Some(99))); assert_roundtrips(Some(Wrapper(99))); assert_roundtrips(Some("hi".to_string())); } /// Some types don't fully consume their input SeqAccess, leading to incorrect /// deserializes. /// /// https://github.com/3Hren/msgpack-rust/issues/287 #[test] fn checked_seq_access_len() { #[derive(Serialize)] struct Input { a: [&'static str; 4], d: &'static str, } #[allow(dead_code)] #[derive(Deserialize, Debug)] struct Output { a: [String; 2], c: String, } let mut buffer = Vec::new(); let mut serializer = Serializer::new(&mut buffer) .with_binary() .with_struct_map(); // The bug is basically that Output will successfully deserialize from input // because the [String; 0] deserializer doesn't drain the SeqAccess, and // the two fields it leaves behind can then be deserialized into `v` let data = Input { a: ["b", "b", "c", "c"], d: "d", }; data.serialize(&mut serializer).expect("failed to serialize"); let mut deserializer = rmp_serde::Deserializer::new( Cursor::new(&buffer) ).with_binary(); Output::deserialize(&mut deserializer) .expect_err("Input round tripped into Output; this shouldn't happen"); } #[ignore] #[test] fn roundtrip_some_failures() { // FIXME assert_roundtrips(Some(None::<()>)); } #[cfg(test)] #[track_caller] fn assert_roundtrips Deserialize<'a>>(val: T) { assert_roundtrips_config(&val, "default", |s| s, |d| d); assert_roundtrips_config(&val, ".with_struct_map()", |s| s.with_struct_map(), |d| d); assert_roundtrips_config( &val, ".with_struct_map()", |s| s.with_struct_map(), |d| d, ); assert_roundtrips_config( &val, ".with_human_readable()", |s| s.with_human_readable(), |d| d.with_human_readable(), ); assert_roundtrips_config( &val, ".with_human_readable().with_struct_map()", |s| s.with_human_readable().with_struct_map(), |d| d.with_human_readable(), ); assert_roundtrips_config( &val, ".with_human_readable()", |s| s.with_human_readable(), |d| d.with_human_readable(), ); assert_roundtrips_config( &val, ".with_human_readable().with_struct_map()", |s| { s.with_human_readable() .with_struct_map() }, |d| d.with_human_readable(), ); } #[cfg(test)] #[track_caller] fn assert_roundtrips_config( val: &T, desc: &str, config_serializer: CSF, config_deserializer: CDF, ) where T: PartialEq + std::fmt::Debug + Serialize + for<'a> Deserialize<'a>, CSF: FnOnce(Serializer, DefaultConfig>) -> Serializer, SC>, SC: SerializerConfig, CDF: FnOnce( Deserializer, DefaultConfig>, ) -> Deserializer, DC>, DC: SerializerConfig, { let mut serializer = config_serializer(Serializer::new(Vec::new())); if let Err(e) = val.serialize(&mut serializer) { panic!( "Failed to serialize: {}\nConfig: {}\nValue: {:?}\n", e, desc, val ); } let serialized = serializer.into_inner(); let mut deserializer = config_deserializer(Deserializer::new(serialized.as_slice())); let val2: T = match T::deserialize(&mut deserializer) { Ok(t) => t, Err(e) => { panic!( "Does not deserialize: {}\nConfig: {}\nSerialized {:?}\nGot {:?}\n", e, desc, val, rmpv::decode::value::read_value(&mut serialized.as_slice()) .expect("rmp didn't serialize correctly at all") ); } }; assert_eq!(val, &val2, "Config: {}", desc); }