Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dsc_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ base64 = "0.21"
derive_builder ="0.20"
indicatif = { version = "0.17" }
jsonschema = "0.17"
num-traits = "0.2"
regex = "1.7"
reqwest = { version = "0.11", features = ["blocking"] }
schemars = { version = "0.8.12", features = ["preserve_order"] }
Expand Down
3 changes: 3 additions & 0 deletions dsc_lib/src/dscerror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub enum DscError {
#[error("Function '{0}' error: {1}")]
Function(String, String),

#[error("Function '{0}' error: {1}")]
FunctionArg(String, String),

#[error("HTTP: {0}")]
Http(#[from] reqwest::Error),

Expand Down
83 changes: 83 additions & 0 deletions dsc_lib/src/functions/int.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::DscError;
use crate::configure::context::Context;
use crate::functions::AcceptedArgKind;
use num_traits::cast::NumCast;
use serde_json::Value;
use super::Function;

#[derive(Debug, Default)]
pub struct Int {}

impl Function for Int {
fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> {
vec![AcceptedArgKind::String, AcceptedArgKind::Number]
}

fn min_args(&self) -> usize {
1
}

fn max_args(&self) -> usize {
1
}

fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {
let arg = &args[0];
let value: i64;
if arg.is_string() {
let input = arg.as_str().ok_or(DscError::FunctionArg("int".to_string(), "invalid input string".to_string()))?;
let result = input.parse::<f64>().map_err(|_| DscError::FunctionArg("int".to_string(), "unable to parse string to int".to_string()))?;
value = NumCast::from(result).ok_or(DscError::FunctionArg("int".to_string(), "unable to cast to int".to_string()))?;
} else if arg.is_number() {
value = arg.as_i64().ok_or(DscError::FunctionArg("int".to_string(), "unable to parse number to int".to_string()))?;
} else {
return Err(DscError::FunctionArg("int".to_string(), "Invalid argument type".to_string()));
}
Ok(Value::Number(value.into()))
}
}

#[cfg(test)]
mod tests {
use crate::configure::context::Context;
use crate::parser::Statement;
use crate::DscError;

#[test]
fn string() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[int('4')]", &Context::new()).unwrap();
assert_eq!(result, 4);
}

#[test]
fn string_with_decimal() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[int('4.0')]", &Context::new()).unwrap();
assert_eq!(result, 4);
}

#[test]
fn number() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[int(123)]", &Context::new()).unwrap();
assert_eq!(result, 123);
}

#[test]
fn nested() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[int(int('-1'))]", &Context::new()).unwrap();
assert_eq!(result, -1);
}

#[test]
fn error() {
let mut parser = Statement::new().unwrap();
let err = parser.parse_and_execute("[int('foo')]", &Context::new()).unwrap_err();
assert!(matches!(err, DscError::FunctionArg(_, _)));
}
}
2 changes: 2 additions & 0 deletions dsc_lib/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod concat;
pub mod create_array;
pub mod div;
pub mod envvar;
pub mod int;
pub mod max;
pub mod min;
pub mod mod_function;
Expand Down Expand Up @@ -68,6 +69,7 @@ impl FunctionDispatcher {
functions.insert("createArray".to_string(), Box::new(create_array::CreateArray{}));
functions.insert("div".to_string(), Box::new(div::Div{}));
functions.insert("envvar".to_string(), Box::new(envvar::Envvar{}));
functions.insert("int".to_string(), Box::new(int::Int{}));
functions.insert("max".to_string(), Box::new(max::Max{}));
functions.insert("min".to_string(), Box::new(min::Min{}));
functions.insert("mod".to_string(), Box::new(mod_function::Mod{}));
Expand Down