summaryrefslogtreecommitdiff
path: root/src/parameter.rs
blob: 3ae88cde8d7f8dce3e04157a37cde9c8f2576c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
		}
	}
}