@@ -124,28 +124,37 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
124
124
// in serde_json. I'm going for this implementation anyway because serde_json isn't supposed to be
125
125
// memory efficient and we're using it anyway. See https://github.com/serde-rs/json/issues/635.
126
126
let mut missing_path = None ;
127
- let temp_doc = paths. drain ( ..) . fold ( HashMap :: new ( ) , |mut acc, path : Path | {
128
- let query = compile ( path. get_path ( ) ) ;
129
- if query. is_err ( ) {
130
- return acc;
131
- }
132
- let query = query. unwrap ( ) ;
133
- let s = calc_once ( query, self . val ) ;
134
-
135
- let value = if is_legacy && !s. is_empty ( ) {
136
- Some ( Values :: Single ( s[ 0 ] ) )
137
- } else if !is_legacy {
138
- Some ( Values :: Multi ( s) )
139
- } else {
140
- None
141
- } ;
142
-
143
- if value. is_none ( ) && missing_path. is_none ( ) {
144
- missing_path = Some ( path. get_original ( ) . to_string ( ) ) ;
145
- }
146
- acc. insert ( path. get_original ( ) , value) ;
147
- acc
148
- } ) ;
127
+ let path_len = paths. len ( ) ;
128
+ let temp_doc =
129
+ paths
130
+ . drain ( ..)
131
+ . fold ( HashMap :: with_capacity ( path_len) , |mut acc, path : Path | {
132
+ let query = compile ( path. get_path ( ) ) ;
133
+
134
+ // If we can't compile the path, we can't continue
135
+ if query. is_err ( ) {
136
+ return acc;
137
+ }
138
+
139
+ let query = query. unwrap ( ) ;
140
+ let results = calc_once ( query, self . val ) ;
141
+
142
+ let value = if is_legacy {
143
+ if results. is_empty ( ) {
144
+ None
145
+ } else {
146
+ Some ( Values :: Single ( results[ 0 ] ) )
147
+ }
148
+ } else {
149
+ Some ( Values :: Multi ( results) )
150
+ } ;
151
+
152
+ if value. is_none ( ) && missing_path. is_none ( ) {
153
+ missing_path = Some ( path. get_original ( ) . to_string ( ) ) ;
154
+ }
155
+ acc. insert ( path. get_original ( ) , value) ;
156
+ acc
157
+ } ) ;
149
158
if let Some ( p) = missing_path {
150
159
return Err ( err_msg_json_path_doesnt_exist_with_param ( p. as_str ( ) ) . into ( ) ) ;
151
160
}
0 commit comments