@@ -194,43 +194,36 @@ impl FunctionDispatcher {
194194 break ;
195195 }
196196
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] ) ?;
210198 }
211199
212200 // if we have remaining args, they must match one of the remaining_arg_types
213201 if let Some ( remaining_arg_types) = metadata. remaining_arg_accepted_types {
214202 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) ?;
228204 }
229205 }
230206
231207 function. invoke ( args, context)
232208 }
233209
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+
234227 #[ must_use]
235228 pub fn list ( & self ) -> Vec < FunctionDefinition > {
236229 self . functions . iter ( ) . map ( |( name, function) | {
@@ -241,6 +234,8 @@ impl FunctionDispatcher {
241234 description : metadata. description ,
242235 min_args : metadata. min_args ,
243236 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 ( ) ,
244239 return_types : metadata. return_types ,
245240 }
246241 } ) . collect ( )
@@ -263,6 +258,10 @@ pub struct FunctionDefinition {
263258 pub min_args : usize ,
264259 #[ serde( rename = "maxArgs" ) ]
265260 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 > > ,
266265 #[ serde( rename = "returnTypes" ) ]
267266 pub return_types : Vec < FunctionArgKind > ,
268267}
0 commit comments