|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use crate::DscError; |
| 5 | +use crate::configure::context::Context; |
| 6 | +use crate::functions::AcceptedArgKind; |
| 7 | +use num_traits::cast::NumCast; |
| 8 | +use serde_json::Value; |
| 9 | +use super::Function; |
| 10 | + |
| 11 | +#[derive(Debug, Default)] |
| 12 | +pub struct Int {} |
| 13 | + |
| 14 | +impl Function for Int { |
| 15 | + fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { |
| 16 | + vec![AcceptedArgKind::String, AcceptedArgKind::Number] |
| 17 | + } |
| 18 | + |
| 19 | + fn min_args(&self) -> usize { |
| 20 | + 1 |
| 21 | + } |
| 22 | + |
| 23 | + fn max_args(&self) -> usize { |
| 24 | + 1 |
| 25 | + } |
| 26 | + |
| 27 | + fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { |
| 28 | + let arg = &args[0]; |
| 29 | + let value: i64; |
| 30 | + if arg.is_string() { |
| 31 | + let input = arg.as_str().ok_or(DscError::FunctionArg("int".to_string(), "invalid input string".to_string()))?; |
| 32 | + let result = input.parse::<f64>().map_err(|_| DscError::FunctionArg("int".to_string(), "unable to parse string to int".to_string()))?; |
| 33 | + value = NumCast::from(result).ok_or(DscError::FunctionArg("int".to_string(), "unable to cast to int".to_string()))?; |
| 34 | + } else if arg.is_number() { |
| 35 | + value = arg.as_i64().ok_or(DscError::FunctionArg("int".to_string(), "unable to parse number to int".to_string()))?; |
| 36 | + } else { |
| 37 | + return Err(DscError::FunctionArg("int".to_string(), "Invalid argument type".to_string())); |
| 38 | + } |
| 39 | + Ok(Value::Number(value.into())) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[cfg(test)] |
| 44 | +mod tests { |
| 45 | + use crate::configure::context::Context; |
| 46 | + use crate::parser::Statement; |
| 47 | + use crate::DscError; |
| 48 | + |
| 49 | + #[test] |
| 50 | + fn string() { |
| 51 | + let mut parser = Statement::new().unwrap(); |
| 52 | + let result = parser.parse_and_execute("[int('4')]", &Context::new()).unwrap(); |
| 53 | + assert_eq!(result, 4); |
| 54 | + } |
| 55 | + |
| 56 | + #[test] |
| 57 | + fn string_with_decimal() { |
| 58 | + let mut parser = Statement::new().unwrap(); |
| 59 | + let result = parser.parse_and_execute("[int('4.0')]", &Context::new()).unwrap(); |
| 60 | + assert_eq!(result, 4); |
| 61 | + } |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn number() { |
| 65 | + let mut parser = Statement::new().unwrap(); |
| 66 | + let result = parser.parse_and_execute("[int(123)]", &Context::new()).unwrap(); |
| 67 | + assert_eq!(result, 123); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn nested() { |
| 72 | + let mut parser = Statement::new().unwrap(); |
| 73 | + let result = parser.parse_and_execute("[int(int('-1'))]", &Context::new()).unwrap(); |
| 74 | + assert_eq!(result, -1); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn error() { |
| 79 | + let mut parser = Statement::new().unwrap(); |
| 80 | + let err = parser.parse_and_execute("[int('foo')]", &Context::new()).unwrap_err(); |
| 81 | + assert!(matches!(err, DscError::FunctionArg(_, _))); |
| 82 | + } |
| 83 | +} |
0 commit comments