Invoke a user-defined function by name with the provided arguments and context. # Arguments `name` - The name of the user-defined function to invoke. `args` - The arguments to pass to the user-defined function. `context` - The current execution context. # Returns `Result ` - The result of the function invocation or an error. # Errors `DscError::Parser` - If the function is not f
(name: &str, args: &[Value], context: &Context)
| 22 | /// * `DscError::Parser` - If the function is not found, parameters are invalid, or output type is incorrect. |
| 23 | /// |
| 24 | pub fn invoke_user_function(name: &str, args: &[Value], context: &Context) -> Result<Value, DscError> { |
| 25 | if let Some(function_definition) = context.user_functions.get(name) { |
| 26 | validate_parameters(name, function_definition, args)?; |
| 27 | let mut user_context = context.clone(); |
| 28 | user_context.process_mode = ProcessMode::UserFunction; |
| 29 | // can only use its own parameters and not the global ones |
| 30 | user_context.parameters.clear(); |
| 31 | // cannot call other user functions |
| 32 | user_context.user_functions.clear(); |
| 33 | for (i, arg) in args.iter().enumerate() { |
| 34 | let Some(params) = &function_definition.parameters else { |
| 35 | return Err(DscError::Parser(t!("functions.userFunction.expectedNoParameters", name = name).to_string())); |
| 36 | }; |
| 37 | user_context.parameters.insert(params[i].name.clone(), (arg.clone(), params[i].r#type.clone())); |
| 38 | } |
| 39 | let mut parser = Statement::new()?; |
| 40 | let result = parser.parse_and_execute(&function_definition.output.value, &user_context)?; |
| 41 | validate_output_type(name, function_definition, &result)?; |
| 42 | Ok(result) |
| 43 | } else { |
| 44 | Err(DscError::Parser(t!("functions.userFunction.unknownUserFunction", name = name).to_string())) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn validate_parameters(name: &str, function_definition: &UserFunctionDefinition, args: &[Value]) -> Result<(), DscError> { |
| 49 | if let Some(expected_params) = &function_definition.parameters { |
no test coverage detected