Skip to content

Commit

Permalink
Add support for optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
IamTheCarl committed Oct 9, 2024
1 parent e2b68c9 commit a41dc40
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions r2r/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ try_into_template!(Vec<i64>, "integer array", ParameterValue::IntegerArray(value
try_into_template!(Vec<f64>, "double array", ParameterValue::DoubleArray(value) => value);
try_into_template!(Vec<String>, "string array", ParameterValue::StringArray(value) => value);

macro_rules! try_into_option_template {
($ty:ty, $expected_type_name:literal, $variant:pat => $result:expr) => {
impl TryInto<Option<$ty>> for ParameterValue {
type Error = WrongParameterType;

fn try_into(self) -> std::prelude::v1::Result<Option<$ty>, 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<bool>, "boolean array", ParameterValue::BoolArray(value) => value);
try_into_option_template!(Vec<u8>, "byte array", ParameterValue::ByteArray(value) => value);
try_into_option_template!(Vec<i64>, "integer array", ParameterValue::IntegerArray(value) => value);
try_into_option_template!(Vec<f64>, "double array", ParameterValue::DoubleArray(value) => value);
try_into_option_template!(Vec<String>, "string array", ParameterValue::StringArray(value) => value);

impl ParameterValue {
pub fn type_name(&self) -> &'static str {
match self {
Expand Down

0 comments on commit a41dc40

Please sign in to comment.