From c18970e8d1003a7b4b3b95b8ade07226bd235f0f Mon Sep 17 00:00:00 2001 From: troido Date: Thu, 6 Feb 2020 21:00:13 +0100 Subject: refactored into multiple classes --- src/parameter.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/parameter.rs (limited to 'src/parameter.rs') diff --git a/src/parameter.rs b/src/parameter.rs new file mode 100644 index 0000000..3ae88cd --- /dev/null +++ b/src/parameter.rs @@ -0,0 +1,81 @@ + +use serde_json::Value; + +#[derive(Debug, PartialEq, Clone)] +pub enum Parameter { + String(String), + Int(i64), +// Pos(Pos), + Float(f64) +} + +impl Parameter { + + pub fn from_typed_json(typ: ParameterType, val: &Value) -> Option{ + match typ { + ParameterType::String => Some(Self::String(val.as_str()?.to_string())), + ParameterType::Int => Some(Self::Int(val.as_i64()?)), + ParameterType::Float => Some(Self::Float(val.as_f64()?)) + } + } + + pub fn paramtype(&self) -> ParameterType { + match self { + Self::String(_) => ParameterType::String, + Self::Int(_) => ParameterType::Int, + Self::Float(_) => ParameterType::Float + } + } + +// pub fn from_json(val: &Value) -> Option { +// Self::from_typed_json(ParameterType::from_str(val.get(0)?.as_str()?)?, val.get(1)?) +// } + + pub fn as_str(&self) -> Option<&str> { + if let Self::String(str) = self { + Some(str) + } else { + None + } + } + +// pub fn as_string(&self) -> Option { +// Some(self.as_str()?.to_string()) +// } + + pub fn as_i64(&self) -> Option { + if let Self::Int(num) = self { + Some(*num) + } else { + None + } + } + + pub fn as_f64(&self) -> Option { + if let Self::Float(num) = self { + Some(*num) + } else { + None + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ParameterType { + String, +// Pos, + Int, + Float +} + +impl ParameterType { + + pub fn from_str(typename: &str) -> Option{ + match typename { + "string" => Some(Self::String), + "int" => Some(Self::Int), + "float" => Some(Self::Float), + _ => None + } + } +} -- cgit