diff options
Diffstat (limited to 'lib/warden-core/src/configuration')
-rw-r--r-- | lib/warden-core/src/configuration/conv.rs | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/lib/warden-core/src/configuration/conv.rs b/lib/warden-core/src/configuration/conv.rs index 02f0d27..fe4acf3 100644 --- a/lib/warden-core/src/configuration/conv.rs +++ b/lib/warden-core/src/configuration/conv.rs @@ -1,4 +1,4 @@ -use crate::google::protobuf::{ListValue, NullValue, Struct, Value, value}; +use crate::{configuration::typology::Operator, google::protobuf::{value, ListValue, NullValue, Struct, Value}}; #[derive(Debug)] /// Generic JSON value @@ -107,3 +107,50 @@ impl serde::Serialize for GenericParameter { json.serialize(serializer) } } + +pub(crate) mod operator_serde { + use serde::{self, Deserialize, Deserializer, Serializer}; + use super::Operator; + + pub fn serialize<S>(operator: &i32, s: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let operator = Operator::try_from(*operator); + if let Ok(d) = operator { + return s.serialize_str(d.as_str_name()); + } + s.serialize_none() + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result<i32, D::Error> + where + D: Deserializer<'de>, + { + let s: Option<String> = Option::deserialize(deserializer)?; + + if let Some(s) = s { + let op = Operator::from_str_name(&s) + .ok_or_else(|| serde::de::Error::custom("unsupported"))? + as i32; + return Ok(op); + } + + Err(serde::de::Error::custom("deserialise error for operator")) + } +} + +impl From<Operator> for String { + fn from(value: Operator) -> Self { + value.as_str_name().to_owned() + } +} + +impl TryFrom<String> for Operator { + type Error = String; + + fn try_from(value: String) -> Result<Self, Self::Error> { + let value = value.to_uppercase(); + Operator::from_str_name(&value).ok_or_else(|| format!("unsupported operator: {}", value)) + } +} |