summaryrefslogtreecommitdiff
path: root/src/parameter.rs
blob: 41137bb5f666ec1806f4b81ab32b62294b1963a7 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

use serde_json::Value;

#[derive(Debug, PartialEq, Clone)]
pub enum Parameter {
	String(String),
	Int(i64),
// 	Pos(Pos),
	Float(f64)
}

impl Parameter {
	
	#[allow(dead_code)]
	pub fn string(string: &str) -> Self {
		Self::String(string.to_string())
	}
	
	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 guess_from_json(val: &Value) -> Option<Parameter> {
		let typ = 
			if val.is_string() {
				ParameterType::String
			} else if val.is_u64() || val.is_i64() {
				ParameterType::Int
			} else if val.is_f64() {
				ParameterType::Float
			} else {
				println!("{:?}", val);
				return None
			};
		Self::from_typed_json(typ, val)
	}
	
	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())
	}
	
	#[allow(dead_code)]
	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
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use serde_json::json;
	
	macro_rules! gfj { // guess from json
		($j:expr) => {Parameter::guess_from_json(&json!($j)).unwrap()}
	}
	
	#[test]
	fn can_guess_json() {
		Parameter::guess_from_json(&json!(3)).unwrap();
	}
	
	#[test]
	fn guess_json() {
		assert_eq!(gfj!("charles"), Parameter::string("charles"));
		assert_eq!(gfj!("1"), Parameter::string("1"));
		assert_eq!(gfj!(""), Parameter::string(""));
		assert_eq!(gfj!(3), Parameter::Int(3));
		assert_eq!(gfj!(-3), Parameter::Int(-3));
		assert_eq!(gfj!(0), Parameter::Int(0));
		assert_eq!(gfj!(-0), Parameter::Int(0));
		assert_eq!(gfj!(3.5), Parameter::Float(3.5));
		assert_eq!(gfj!(3.0), Parameter::Float(3.0));
		assert_eq!(gfj!(-3.0), Parameter::Float(-3.0));
		assert_eq!(gfj!(0.0), Parameter::Float(0.0));
		assert_eq!(gfj!(-0.0), Parameter::Float(0.0));
	}
	
	#[test]
	fn guess_json_none() {
		assert!(Parameter::guess_from_json(&json!([2, 5])).is_none());
		assert!(Parameter::guess_from_json(&json!(true)).is_none());
		assert!(Parameter::guess_from_json(&json!({"hello": "world"})).is_none());
	}
}