|
| 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::{FunctionArgKind, Function, FunctionCategory, FunctionMetadata}; |
| 7 | +use rust_i18n::t; |
| 8 | +use serde_json::Value; |
| 9 | +use tracing::debug; |
| 10 | + |
| 11 | +#[derive(Debug, Default)] |
| 12 | +pub struct Skip {} |
| 13 | + |
| 14 | +impl Function for Skip { |
| 15 | + fn get_metadata(&self) -> FunctionMetadata { |
| 16 | + FunctionMetadata { |
| 17 | + name: "skip".to_string(), |
| 18 | + description: t!("functions.skip.description").to_string(), |
| 19 | + category: FunctionCategory::Array, |
| 20 | + min_args: 2, |
| 21 | + max_args: 2, |
| 22 | + accepted_arg_ordered_types: vec![ |
| 23 | + vec![FunctionArgKind::Array, FunctionArgKind::String], |
| 24 | + vec![FunctionArgKind::Number], |
| 25 | + ], |
| 26 | + remaining_arg_accepted_types: None, |
| 27 | + return_types: vec![FunctionArgKind::Array, FunctionArgKind::String], |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { |
| 32 | + debug!("{}", t!("functions.skip.invoked")); |
| 33 | + |
| 34 | + let Some(count_i64) = args[1].as_i64() else { |
| 35 | + return Err(DscError::Parser(t!("functions.skip.invalidNumberToSkip").to_string())); |
| 36 | + }; |
| 37 | + if count_i64 < 0 { |
| 38 | + return Err(DscError::Parser(t!("functions.skip.negativeNotAllowed").to_string())); |
| 39 | + } |
| 40 | + let count: usize = count_i64.try_into().unwrap_or(usize::MAX); |
| 41 | + |
| 42 | + if let Some(array) = args[0].as_array() { |
| 43 | + if count >= array.len() { return Ok(Value::Array(vec![])); } |
| 44 | + let skipped = array.iter().skip(count).cloned().collect::<Vec<Value>>(); |
| 45 | + return Ok(Value::Array(skipped)); |
| 46 | + } |
| 47 | + |
| 48 | + if let Some(s) = args[0].as_str() { |
| 49 | + |
| 50 | + let result: String = s.chars().skip(count).collect(); |
| 51 | + return Ok(Value::String(result)); |
| 52 | + } |
| 53 | + |
| 54 | + Err(DscError::Parser(t!("functions.skip.invalidOriginalValue").to_string())) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[cfg(test)] |
| 59 | +mod tests { |
| 60 | + use crate::configure::context::Context; |
| 61 | + use crate::parser::Statement; |
| 62 | + use serde_json::Value; |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn skip_array_basic() { |
| 66 | + let mut parser = Statement::new().unwrap(); |
| 67 | + let result = parser.parse_and_execute("[skip(createArray('a','b','c','d'), 2)]", &Context::new()).unwrap(); |
| 68 | + assert_eq!(result, Value::Array(vec![Value::String("c".into()), Value::String("d".into())])); |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn skip_string_basic() { |
| 73 | + let mut parser = Statement::new().unwrap(); |
| 74 | + let result = parser.parse_and_execute("[skip('hello', 2)]", &Context::new()).unwrap(); |
| 75 | + assert_eq!(result, Value::String("llo".into())); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn skip_more_than_length() { |
| 80 | + let mut parser = Statement::new().unwrap(); |
| 81 | + let result = parser.parse_and_execute("[skip(createArray('a','b'), 5)]", &Context::new()).unwrap(); |
| 82 | + assert_eq!(result, Value::Array(vec![])); |
| 83 | + } |
| 84 | +} |
0 commit comments