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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
mod case;
mod de;
mod ser;
use serde::{
de::{DeserializeSeed, Deserializer},
ser::{Serialize, Serializer},
};
use crate::{DynamicMessage, MessageDescriptor};
#[derive(Debug, Clone)]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
pub struct SerializeOptions {
stringify_64_bit_integers: bool,
use_enum_numbers: bool,
use_proto_field_name: bool,
skip_default_fields: bool,
}
#[derive(Debug, Clone)]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
pub struct DeserializeOptions {
deny_unknown_fields: bool,
}
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl Serialize for DynamicMessage {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.serialize_with_options(serializer, &Default::default())
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> DeserializeSeed<'de> for MessageDescriptor {
type Value = DynamicMessage;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
DynamicMessage::deserialize(self, deserializer)
}
}
impl DynamicMessage {
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
pub fn serialize_with_options<S>(
&self,
serializer: S,
options: &SerializeOptions,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
ser::serialize_message(self, serializer, options)
}
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
pub fn deserialize<'de, D>(desc: MessageDescriptor, deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Self::deserialize_with_options(desc, deserializer, &Default::default())
}
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
pub fn deserialize_with_options<'de, D>(
desc: MessageDescriptor,
deserializer: D,
options: &DeserializeOptions,
) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
de::deserialize_message(&desc, deserializer, options)
}
}
impl DeserializeOptions {
pub const fn new() -> Self {
DeserializeOptions {
deny_unknown_fields: true,
}
}
pub const fn deny_unknown_fields(mut self, yes: bool) -> Self {
self.deny_unknown_fields = yes;
self
}
}
impl Default for DeserializeOptions {
fn default() -> Self {
Self::new()
}
}
impl SerializeOptions {
pub const fn new() -> Self {
SerializeOptions {
stringify_64_bit_integers: true,
use_enum_numbers: false,
use_proto_field_name: false,
skip_default_fields: true,
}
}
pub const fn stringify_64_bit_integers(mut self, yes: bool) -> Self {
self.stringify_64_bit_integers = yes;
self
}
pub const fn use_enum_numbers(mut self, yes: bool) -> Self {
self.use_enum_numbers = yes;
self
}
pub const fn use_proto_field_name(mut self, yes: bool) -> Self {
self.use_proto_field_name = yes;
self
}
pub const fn skip_default_fields(mut self, yes: bool) -> Self {
self.skip_default_fields = yes;
self
}
}
impl Default for SerializeOptions {
fn default() -> Self {
Self::new()
}
}
const MAX_DURATION_SECONDS: u64 = 315_576_000_000;
const MAX_DURATION_NANOS: u32 = 999_999_999;
const MIN_TIMESTAMP_SECONDS: i64 = -62135596800;
const MAX_TIMESTAMP_SECONDS: i64 = 253402300799;
fn is_well_known_type(full_name: &str) -> bool {
matches!(
full_name,
"google.protobuf.Any"
| "google.protobuf.Timestamp"
| "google.protobuf.Duration"
| "google.protobuf.Struct"
| "google.protobuf.FloatValue"
| "google.protobuf.DoubleValue"
| "google.protobuf.Int32Value"
| "google.protobuf.Int64Value"
| "google.protobuf.UInt32Value"
| "google.protobuf.UInt64Value"
| "google.protobuf.BoolValue"
| "google.protobuf.StringValue"
| "google.protobuf.BytesValue"
| "google.protobuf.FieldMask"
| "google.protobuf.ListValue"
| "google.protobuf.Value"
| "google.protobuf.Empty"
)
}