@@ -194,43 +194,36 @@ impl FunctionDispatcher {
194
194
break ;
195
195
}
196
196
197
- if value. is_array ( ) && !metadata. accepted_arg_ordered_types [ index] . contains ( & FunctionArgKind :: Array ) {
198
- return Err ( DscError :: Parser ( t ! ( "functions.noArrayArgs" , name = name, accepted_args_string = metadata. accepted_arg_ordered_types[ index] . iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
199
- } else if value. is_boolean ( ) && !metadata. accepted_arg_ordered_types [ index] . contains ( & FunctionArgKind :: Boolean ) {
200
- return Err ( DscError :: Parser ( t ! ( "functions.noBooleanArgs" , name = name, accepted_args_string = metadata. accepted_arg_ordered_types[ index] . iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
201
- } else if value. is_null ( ) && !metadata. accepted_arg_ordered_types [ index] . contains ( & FunctionArgKind :: Null ) {
202
- return Err ( DscError :: Parser ( t ! ( "functions.noNullArgs" , name = name, accepted_args_string = metadata. accepted_arg_ordered_types[ index] . iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
203
- } else if value. is_number ( ) && !metadata. accepted_arg_ordered_types [ index] . contains ( & FunctionArgKind :: Number ) {
204
- return Err ( DscError :: Parser ( t ! ( "functions.noNumberArgs" , name = name, accepted_args_string = metadata. accepted_arg_ordered_types[ index] . iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
205
- } else if value. is_object ( ) && !metadata. accepted_arg_ordered_types [ index] . contains ( & FunctionArgKind :: Object ) {
206
- return Err ( DscError :: Parser ( t ! ( "functions.noObjectArgs" , name = name, accepted_args_string = metadata. accepted_arg_ordered_types[ index] . iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
207
- } else if value. is_string ( ) && !metadata. accepted_arg_ordered_types [ index] . contains ( & FunctionArgKind :: String ) {
208
- return Err ( DscError :: Parser ( t ! ( "functions.noStringArgs" , name = name, accepted_args_string = metadata. accepted_arg_ordered_types[ index] . iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
209
- }
197
+ Self :: check_arg_against_expected_types ( value, & metadata. accepted_arg_ordered_types [ index] ) ?;
210
198
}
211
199
212
200
// if we have remaining args, they must match one of the remaining_arg_types
213
201
if let Some ( remaining_arg_types) = metadata. remaining_arg_accepted_types {
214
202
for value in args. iter ( ) . skip ( metadata. accepted_arg_ordered_types . len ( ) ) {
215
- if value. is_array ( ) && !remaining_arg_types. contains ( & FunctionArgKind :: Array ) {
216
- return Err ( DscError :: Parser ( t ! ( "functions.noArrayArgs" , name = name, accepted_args_string = remaining_arg_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
217
- } else if value. is_boolean ( ) && !remaining_arg_types. contains ( & FunctionArgKind :: Boolean ) {
218
- return Err ( DscError :: Parser ( t ! ( "functions.noBooleanArgs" , name = name, accepted_args_string = remaining_arg_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
219
- } else if value. is_null ( ) && !remaining_arg_types. contains ( & FunctionArgKind :: Null ) {
220
- return Err ( DscError :: Parser ( t ! ( "functions.noNullArgs" , name = name, accepted_args_string = remaining_arg_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
221
- } else if value. is_number ( ) && !remaining_arg_types. contains ( & FunctionArgKind :: Number ) {
222
- return Err ( DscError :: Parser ( t ! ( "functions.noNumberArgs" , name = name, accepted_args_string = remaining_arg_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
223
- } else if value. is_object ( ) && !remaining_arg_types. contains ( & FunctionArgKind :: Object ) {
224
- return Err ( DscError :: Parser ( t ! ( "functions.noObjectArgs" , name = name, accepted_args_string = remaining_arg_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
225
- } else if value. is_string ( ) && !remaining_arg_types. contains ( & FunctionArgKind :: String ) {
226
- return Err ( DscError :: Parser ( t ! ( "functions.noStringArgs" , name = name, accepted_args_string = remaining_arg_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
227
- }
203
+ Self :: check_arg_against_expected_types ( value, & remaining_arg_types) ?;
228
204
}
229
205
}
230
206
231
207
function. invoke ( args, context)
232
208
}
233
209
210
+ fn check_arg_against_expected_types ( arg : & Value , expected_types : & [ FunctionArgKind ] ) -> Result < ( ) , DscError > {
211
+ if arg. is_array ( ) && !expected_types. contains ( & FunctionArgKind :: Array ) {
212
+ return Err ( DscError :: Parser ( t ! ( "functions.noArrayArgs" , accepted_args_string = expected_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
213
+ } else if arg. is_boolean ( ) && !expected_types. contains ( & FunctionArgKind :: Boolean ) {
214
+ return Err ( DscError :: Parser ( t ! ( "functions.noBooleanArgs" , accepted_args_string = expected_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
215
+ } else if arg. is_null ( ) && !expected_types. contains ( & FunctionArgKind :: Null ) {
216
+ return Err ( DscError :: Parser ( t ! ( "functions.noNullArgs" , accepted_args_string = expected_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
217
+ } else if arg. is_number ( ) && !expected_types. contains ( & FunctionArgKind :: Number ) {
218
+ return Err ( DscError :: Parser ( t ! ( "functions.noNumberArgs" , accepted_args_string = expected_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
219
+ } else if arg. is_object ( ) && !expected_types. contains ( & FunctionArgKind :: Object ) {
220
+ return Err ( DscError :: Parser ( t ! ( "functions.noObjectArgs" , accepted_args_string = expected_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
221
+ } else if arg. is_string ( ) && !expected_types. contains ( & FunctionArgKind :: String ) {
222
+ return Err ( DscError :: Parser ( t ! ( "functions.noStringArgs" , accepted_args_string = expected_types. iter( ) . map( std:: string:: ToString :: to_string) . collect:: <Vec <_>>( ) . join( ", " ) ) . to_string ( ) ) ) ;
223
+ }
224
+ Ok ( ( ) )
225
+ }
226
+
234
227
#[ must_use]
235
228
pub fn list ( & self ) -> Vec < FunctionDefinition > {
236
229
self . functions . iter ( ) . map ( |( name, function) | {
@@ -241,6 +234,8 @@ impl FunctionDispatcher {
241
234
description : metadata. description ,
242
235
min_args : metadata. min_args ,
243
236
max_args : metadata. max_args ,
237
+ accepted_arg_ordered_types : metadata. accepted_arg_ordered_types . clone ( ) ,
238
+ remaining_arg_accepted_types : metadata. remaining_arg_accepted_types . clone ( ) ,
244
239
return_types : metadata. return_types ,
245
240
}
246
241
} ) . collect ( )
@@ -263,6 +258,10 @@ pub struct FunctionDefinition {
263
258
pub min_args : usize ,
264
259
#[ serde( rename = "maxArgs" ) ]
265
260
pub max_args : usize ,
261
+ #[ serde( rename = "acceptedArgOrderedTypes" ) ]
262
+ pub accepted_arg_ordered_types : Vec < Vec < FunctionArgKind > > ,
263
+ #[ serde( rename = "remainingArgAcceptedTypes" ) ]
264
+ pub remaining_arg_accepted_types : Option < Vec < FunctionArgKind > > ,
266
265
#[ serde( rename = "returnTypes" ) ]
267
266
pub return_types : Vec < FunctionArgKind > ,
268
267
}
0 commit comments