@@ -32,7 +32,7 @@ pub struct ProtobufDeserializerConfig {
32
32
impl ProtobufDeserializerConfig {
33
33
/// Build the `ProtobufDeserializer` from this configuration.
34
34
pub fn build ( & self ) -> ProtobufDeserializer {
35
- Into :: < ProtobufDeserializer > :: into ( self )
35
+ ProtobufDeserializer :: try_from ( self ) . unwrap ( )
36
36
}
37
37
38
38
/// Return the type of event build by this deserializer.
@@ -84,17 +84,17 @@ impl ProtobufDeserializer {
84
84
Self { message_descriptor }
85
85
}
86
86
87
- pub fn get_message_descriptor (
87
+ fn get_message_descriptor (
88
88
desc_file : String ,
89
89
message_type : String ,
90
90
) -> vector_common:: Result < MessageDescriptor > {
91
91
let b = fs:: read ( desc_file. clone ( ) )
92
92
. map_err ( |e| format ! ( "Failed to open protobuf desc file '{desc_file}': {e}" ) ) ?;
93
93
let pool = DescriptorPool :: decode ( b. as_slice ( ) )
94
94
. map_err ( |e| format ! ( "Failed to parse protobuf desc file '{desc_file}': {e}" ) ) ?;
95
- Ok ( pool. get_message_by_name ( & message_type) . expect ( & format ! (
96
- "The message type '{message_type}' could not be found in '{desc_file}'"
97
- ) ) )
95
+ Ok ( pool. get_message_by_name ( & message_type) . unwrap_or_else ( || {
96
+ panic ! ( "The message type '{message_type}' could not be found in '{desc_file}'" )
97
+ } ) )
98
98
}
99
99
}
100
100
@@ -129,14 +129,14 @@ impl Deserializer for ProtobufDeserializer {
129
129
}
130
130
}
131
131
132
- impl From < & ProtobufDeserializerConfig > for ProtobufDeserializer {
133
- fn from ( config : & ProtobufDeserializerConfig ) -> Self {
132
+ impl TryFrom < & ProtobufDeserializerConfig > for ProtobufDeserializer {
133
+ type Error = vector_common:: Error ;
134
+ fn try_from ( config : & ProtobufDeserializerConfig ) -> vector_common:: Result < Self > {
134
135
let message_descriptor = ProtobufDeserializer :: get_message_descriptor (
135
136
config. desc_file . clone ( ) ,
136
137
config. message_type . clone ( ) ,
137
- )
138
- . unwrap ( ) ;
139
- Self :: new ( message_descriptor)
138
+ ) ?;
139
+ Ok ( Self :: new ( message_descriptor) )
140
140
}
141
141
}
142
142
@@ -145,29 +145,31 @@ fn to_vrl(
145
145
field_descriptor : Option < & prost_reflect:: FieldDescriptor > ,
146
146
) -> vector_common:: Result < vrl:: value:: Value > {
147
147
let vrl_value = match prost_reflect_value {
148
- prost_reflect:: Value :: Bool ( v) => vrl:: value:: Value :: from ( v . clone ( ) ) ,
149
- prost_reflect:: Value :: I32 ( v) => vrl:: value:: Value :: from ( v . clone ( ) ) ,
150
- prost_reflect:: Value :: I64 ( v) => vrl:: value:: Value :: from ( v . clone ( ) ) ,
151
- prost_reflect:: Value :: U32 ( v) => vrl:: value:: Value :: from ( v . clone ( ) ) ,
152
- prost_reflect:: Value :: U64 ( v) => vrl:: value:: Value :: from ( v . clone ( ) ) ,
148
+ prost_reflect:: Value :: Bool ( v) => vrl:: value:: Value :: from ( * v ) ,
149
+ prost_reflect:: Value :: I32 ( v) => vrl:: value:: Value :: from ( * v ) ,
150
+ prost_reflect:: Value :: I64 ( v) => vrl:: value:: Value :: from ( * v ) ,
151
+ prost_reflect:: Value :: U32 ( v) => vrl:: value:: Value :: from ( * v ) ,
152
+ prost_reflect:: Value :: U64 ( v) => vrl:: value:: Value :: from ( * v ) ,
153
153
prost_reflect:: Value :: F32 ( v) => vrl:: value:: Value :: Float (
154
- NotNan :: new ( f64:: from ( v. clone ( ) ) )
155
- . map_err ( |_e| format ! ( "Float number cannot be Nan" ) ) ?,
156
- ) ,
157
- prost_reflect:: Value :: F64 ( v) => vrl:: value:: Value :: Float (
158
- NotNan :: new ( v. clone ( ) ) . map_err ( |_e| format ! ( "F64 number cannot be Nan" ) ) ?,
154
+ NotNan :: new ( f64:: from ( * v) ) . map_err ( |_e| "Float number cannot be Nan" ) ?,
159
155
) ,
156
+ prost_reflect:: Value :: F64 ( v) => {
157
+ vrl:: value:: Value :: Float ( NotNan :: new ( * v) . map_err ( |_e| "F64 number cannot be Nan" ) ?)
158
+ }
160
159
prost_reflect:: Value :: String ( v) => vrl:: value:: Value :: from ( v. as_str ( ) ) ,
161
160
prost_reflect:: Value :: Bytes ( v) => vrl:: value:: Value :: from ( v. clone ( ) ) ,
162
161
prost_reflect:: Value :: EnumNumber ( v) => {
163
162
if let Some ( field_descriptor) = field_descriptor {
164
163
let kind = field_descriptor. kind ( ) ;
165
- let enum_desc = kind
166
- . as_enum ( )
167
- . ok_or_else ( || format ! ( "Internal error while parsing protobuf enum" ) ) ?;
164
+ let enum_desc = kind. as_enum ( ) . ok_or_else ( || {
165
+ format ! (
166
+ "Internal error while parsing protobuf enum. Field descriptor: {:?}" ,
167
+ field_descriptor
168
+ )
169
+ } ) ?;
168
170
vrl:: value:: Value :: from (
169
171
enum_desc
170
- . get_value ( v . clone ( ) )
172
+ . get_value ( * v )
171
173
. ok_or_else ( || {
172
174
format ! ( "The number {} cannot be in '{}'" , v, enum_desc. name( ) )
173
175
} ) ?
@@ -188,24 +190,30 @@ fn to_vrl(
188
190
}
189
191
prost_reflect:: Value :: List ( v) => {
190
192
let vec = v
191
- . into_iter ( )
193
+ . iter ( )
192
194
. map ( |o| to_vrl ( o, field_descriptor) )
193
195
. collect :: < Result < Vec < _ > , vector_common:: Error > > ( ) ?;
194
196
vrl:: value:: Value :: from ( vec)
195
197
}
196
198
prost_reflect:: Value :: Map ( v) => {
197
199
if let Some ( field_descriptor) = field_descriptor {
198
200
let kind = field_descriptor. kind ( ) ;
199
- let message_desc = kind
200
- . as_message ( )
201
- . ok_or_else ( || format ! ( "Internal error while parsing protobuf message" ) ) ?;
201
+ let message_desc = kind. as_message ( ) . ok_or_else ( || {
202
+ format ! (
203
+ "Internal error while parsing protobuf field descriptor: {:?}" ,
204
+ field_descriptor
205
+ )
206
+ } ) ?;
202
207
vrl:: value:: Value :: from (
203
- v. into_iter ( )
208
+ v. iter ( )
204
209
. map ( |kv| {
205
210
Ok ( (
206
211
kv. 0 . as_str ( )
207
212
. ok_or_else ( || {
208
- format ! ( "Internal error while parsing protobuf map" )
213
+ format ! (
214
+ "Internal error while parsing protobuf map. Field descriptor: {:?}" ,
215
+ field_descriptor
216
+ )
209
217
} ) ?
210
218
. to_string ( ) ,
211
219
to_vrl ( kv. 1 , Some ( & message_desc. map_entry_value_field ( ) ) ) ?,
0 commit comments