From a41dc40f72130d076cff088937c124f9aa44cfdc Mon Sep 17 00:00:00 2001 From: James Carl Date: Wed, 9 Oct 2024 17:26:24 -0400 Subject: [PATCH] Add support for optional parameters --- r2r/src/parameters.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/r2r/src/parameters.rs b/r2r/src/parameters.rs index 8c56b06c7..1497af201 100644 --- a/r2r/src/parameters.rs +++ b/r2r/src/parameters.rs @@ -63,6 +63,35 @@ try_into_template!(Vec, "integer array", ParameterValue::IntegerArray(value try_into_template!(Vec, "double array", ParameterValue::DoubleArray(value) => value); try_into_template!(Vec, "string array", ParameterValue::StringArray(value) => value); +macro_rules! try_into_option_template { + ($ty:ty, $expected_type_name:literal, $variant:pat => $result:expr) => { + impl TryInto> for ParameterValue { + type Error = WrongParameterType; + + fn try_into(self) -> std::prelude::v1::Result, Self::Error> { + match self { + $variant => Ok(Some($result)), + ParameterValue::NotSet => Ok(None), + _ => Err(WrongParameterType { + expected_type_name: $expected_type_name, + actual_type_name: self.type_name(), + }), + } + } + } + }; +} + +try_into_option_template!(bool, "boolean", ParameterValue::Bool(value) => value); +try_into_option_template!(i64, "integer", ParameterValue::Integer(value) => value); +try_into_option_template!(f64, "double", ParameterValue::Double(value) => value); +try_into_option_template!(String, "string", ParameterValue::String(value) => value); +try_into_option_template!(Vec, "boolean array", ParameterValue::BoolArray(value) => value); +try_into_option_template!(Vec, "byte array", ParameterValue::ByteArray(value) => value); +try_into_option_template!(Vec, "integer array", ParameterValue::IntegerArray(value) => value); +try_into_option_template!(Vec, "double array", ParameterValue::DoubleArray(value) => value); +try_into_option_template!(Vec, "string array", ParameterValue::StringArray(value) => value); + impl ParameterValue { pub fn type_name(&self) -> &'static str { match self {