1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! # serde-struct-tuple
//!
//! **serde-struct-tuple** is a utility crate, built initially for [`battler-wamp`](https://crates.io/crates/battler-wamp). It provides procedural macros to automatically derive [`serde`](https://serde.rs/)'s `Serialize` and `Deserialize` traits for struct types that should be encoded as a tuple (list) of its fields.
//!
//! Struct fields can be any type that implement `serde::Serialize` and/or `serde::Deserialize`.
//!
//! The macro also has additional optional attributes for struct fields:
//!
//! * `default` - If the field is missing during deserialization, the field is initialized to its
//! default value.
//! * `skip_serializing_if` - Checks if the field should be skipped during serialization using the
//! function provided. All subsequent fields will also be skipped, regardless of their value.
//!
//! ## Example
//!
//! ```
//! use std::collections::BTreeMap;
//!
//! use serde_struct_tuple::{
//! DeserializeStructTuple,
//! SerializeStructTuple,
//! };
//!
//! fn is_true(b: &bool) -> bool {
//! *b
//! }
//!
//! #[derive(Debug, Default, PartialEq, Eq, SerializeStructTuple, DeserializeStructTuple)]
//! struct Message {
//! a: u64,
//! b: String,
//! #[serde_struct_tuple(default, skip_serializing_if = Vec::is_empty)]
//! c: Vec<u64>,
//! #[serde_struct_tuple(default, skip_serializing_if = BTreeMap::is_empty)]
//! d: BTreeMap<u8, bool>,
//! #[serde_struct_tuple(default, skip_serializing_if = is_true)]
//! e: bool,
//! }
//!
//! fn main() {
//! // Serialization.
//! assert_eq!(
//! serde_json::to_string(&Message {
//! a: 123,
//! b: "foo".to_owned(),
//! e: true,
//! ..Default::default()
//! })
//! .unwrap(),
//! r#"[123,"foo"]"#
//! );
//! assert_eq!(
//! serde_json::to_string(&Message {
//! a: 123,
//! b: "foo".to_owned(),
//! d: BTreeMap::from_iter([(1, false), (2, true)]),
//! e: true,
//! ..Default::default()
//! })
//! .unwrap(),
//! r#"[123,"foo",[],{"1":false,"2":true}]"#
//! );
//! assert_eq!(
//! serde_json::to_string(&Message {
//! a: 123,
//! b: "foo".to_owned(),
//! c: Vec::from_iter([6, 7, 8]),
//! d: BTreeMap::from_iter([(1, false), (2, true)]),
//! ..Default::default()
//! })
//! .unwrap(),
//! r#"[123,"foo",[6,7,8],{"1":false,"2":true},false]"#
//! );
//!
//! // Deserialization.
//! assert_eq!(
//! serde_json::from_str::<Message>(r#"[123, "foo"]"#).unwrap(),
//! Message {
//! a: 123,
//! b: "foo".to_owned(),
//! ..Default::default()
//! }
//! );
//! assert_eq!(
//! serde_json::from_str::<Message>(r#"[123, "foo", [99, 100], { "20": true }, true]"#)
//! .unwrap(),
//! Message {
//! a: 123,
//! b: "foo".to_owned(),
//! c: Vec::from_iter([99, 100]),
//! d: BTreeMap::from_iter([(20, true)]),
//! e: true,
//! }
//! );
//! }
//! ```
pub use ;
/// Trait for deserializing a struct from a tuple of its fields.
/// Trait for serializing a struct into a tuple of its fields.