From 37288ef069d82dc32a9bb118082abd36919952f2 Mon Sep 17 00:00:00 2001 From: alu Date: Thu, 5 Dec 2019 15:32:30 +0900 Subject: [PATCH] add supported parameter types - Option<&'a str> - &Option --- crates/dao/src/value.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/dao/src/value.rs b/crates/dao/src/value.rs index 83d4a4f..7a0c0c4 100644 --- a/crates/dao/src/value.rs +++ b/crates/dao/src/value.rs @@ -101,6 +101,10 @@ macro_rules! impl_to_value { impl<'a> ToValue for &'a $ty { fn to_value(&self) -> Value { (*self).into() } } + + impl<'a> ToValue for &'a Option<$ty> { + fn to_value(&self) -> Value { (*self).into() } + } }; } @@ -122,7 +126,7 @@ macro_rules! impl_from { /// For dobule borrowed types impl<'a> From<&&'a $ty> for Value { - fn from(f: &&'a $ty) -> Self { Value::$variant(f.to_owned().to_owned()) } + fn from(f: &&'a $ty) -> Self { (*f).into() } } /// for borrowed option types @@ -135,6 +139,13 @@ macro_rules! impl_from { } } + /// for dobule borrowed option types + impl<'a> From<&&'a Option<$ty>> for Value { + fn from(f: &&'a Option<$ty>) -> Self { + (*f).into() + } + } + impl_to_value!($ty); }; @@ -187,10 +198,23 @@ impl<'a> From<&&'a str> for Value { fn from(f: &&'a str) -> Value { Value::Text(f.to_string()) } } +impl<'a> From<&'a Option<&'a str>> for Value { + fn from(f: &'a Option<&'a str>) -> Value { + match f { + Some(f) => Value::Text(f.to_string()), + None => Value::Nil + } + } +} + impl ToValue for &str { fn to_value(&self) -> Value { Value::Text(self.to_string()) } } +impl ToValue for Option<&str> { + fn to_value(&self) -> Value { self.into() } +} + impl From> for Value { fn from(f: Vec) -> Value { Value::Array(Array::Text(f)) } }