| 29 | } |
| 30 | |
| 31 | fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { |
| 32 | debug!("{}", t!("functions.contains.invoked")); |
| 33 | let mut found = false; |
| 34 | |
| 35 | let (string_to_find, number_to_find) = if let Some(string) = args[1].as_str() { |
| 36 | (Some(string.to_string()), None) |
| 37 | } else if let Some(number) = args[1].as_i64() { |
| 38 | (None, Some(number)) |
| 39 | } else { |
| 40 | return Err(DscError::Parser(t!("functions.contains.invalidItemToFind").to_string())); |
| 41 | }; |
| 42 | |
| 43 | // for array, we check if the string or number exists |
| 44 | if let Some(array) = args[0].as_array() { |
| 45 | for item in array { |
| 46 | if let Some(item_str) = item.as_str() { |
| 47 | if let Some(string) = &string_to_find && item_str == string { |
| 48 | found = true; |
| 49 | break; |
| 50 | } |
| 51 | } else if let Some(item_num) = item.as_i64() |
| 52 | && let Some(number) = number_to_find |
| 53 | && item_num == number { |
| 54 | found = true; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | return Ok(Value::Bool(found)); |
| 59 | } |
| 60 | |
| 61 | // for object, we check if the key exists |
| 62 | if let Some(object) = args[0].as_object() { |
| 63 | // see if key exists |
| 64 | for key in object.keys() { |
| 65 | if let Some(string) = &string_to_find { |
| 66 | if key == string { |
| 67 | found = true; |
| 68 | break; |
| 69 | } |
| 70 | } else if let Some(number) = number_to_find && key == &number.to_string() { |
| 71 | found = true; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | return Ok(Value::Bool(found)); |
| 76 | } |
| 77 | |
| 78 | // for string, we check if the string contains the substring or number |
| 79 | if let Some(str) = args[0].as_str() { |
| 80 | if let Some(string) = &string_to_find { |
| 81 | found = str.contains(string); |
| 82 | } else if let Some(number) = number_to_find { |
| 83 | found = str.contains(&number.to_string()); |
| 84 | } |
| 85 | return Ok(Value::Bool(found)); |
| 86 | } |
| 87 | |
| 88 | Err(DscError::Parser(t!("functions.contains.invalidArgType").to_string())) |