diff options
| author | troido <troido@protonmail.com> | 2020-02-06 21:00:13 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-06 21:00:13 +0100 |
| commit | c18970e8d1003a7b4b3b95b8ade07226bd235f0f (patch) | |
| tree | de0e1d331d0134ab8c7ecaf3569c589a0de07208 /src/parameter.rs | |
| parent | 837d5b3a2c70d240b053644ef2e5c3264d453756 (diff) | |
refactored into multiple classes
Diffstat (limited to 'src/parameter.rs')
| -rw-r--r-- | src/parameter.rs | 81 |
1 files changed, 81 insertions, 0 deletions
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<Parameter>{ + 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<Parameter> { +// 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<String> { +// Some(self.as_str()?.to_string()) +// } + + pub fn as_i64(&self) -> Option<i64> { + if let Self::Int(num) = self { + Some(*num) + } else { + None + } + } + + pub fn as_f64(&self) -> Option<f64> { + 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<Self>{ + match typename { + "string" => Some(Self::String), + "int" => Some(Self::Int), + "float" => Some(Self::Float), + _ => None + } + } +} |
