MCPcopy Create free account
hub / github.com/PowerShell/DSC / invoke

Method invoke

lib/dsc-lib/src/functions/range.rs:31–65  ·  view source on GitHub ↗
(&self, args: &[Value], _context: &Context)

Source from the content-addressed store, hash-verified

29 }
30
31 fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {
32 debug!("{}", t!("functions.range.invoked"));
33
34 let start_index = args[0].as_i64()
35 .ok_or_else(|| DscError::FunctionArg("range".to_string(), t!("functions.range.startIndexNotInt").to_string()))?;
36
37 let count = args[1].as_i64()
38 .ok_or_else(|| DscError::FunctionArg("range".to_string(), t!("functions.range.countNotInt").to_string()))?;
39
40 // validation checks
41 if count < 0 {
42 return Err(DscError::FunctionArg("range".to_string(), t!("functions.range.countNegative").to_string()));
43 }
44
45 if count > 10000 {
46 return Err(DscError::FunctionArg("range".to_string(), t!("functions.range.countTooLarge").to_string()));
47 }
48
49 // should not exceed
50 if let Some(sum) = start_index.checked_add(count) {
51 if sum > 2_147_483_647 {
52 return Err(DscError::FunctionArg("range".to_string(), t!("functions.range.sumTooLarge").to_string()));
53 }
54 } else {
55 return Err(DscError::FunctionArg("range".to_string(), t!("functions.range.sumOverflow").to_string()));
56 }
57
58 let mut result = Vec::<Value>::new();
59 for i in 0..count {
60 let value = start_index + i;
61 result.push(Value::Number(value.into()));
62 }
63
64 Ok(Value::Array(result))
65 }
66}
67
68#[cfg(test)]

Callers

nothing calls this directly

Calls 3

FunctionArgEnum · 0.85
ArrayClass · 0.85
newFunction · 0.50

Tested by

no test coverage detected