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
use crate::graph::NodeId;
#[derive(Debug, Clone)]
pub enum Val {
Node(NodeId),
Number(f64),
Bool(bool),
String(String),
}
impl Val {
pub fn to_node(&self) -> Result<NodeId, String> {
match self {
Val::Node(n) => Ok(*n),
other => Err(format!(
"Type mismatch: expected a Node, found `{:?}`",
other
)),
}
}
pub fn to_nat(&self) -> Result<usize, String> {
match self {
Val::Number(u) => Ok(*u as usize),
other => Err(format!(
"Type mismatch: expected a Number, found `{:?}`",
other
)),
}
}
pub fn to_float(&self) -> Result<f64, String> {
match self {
Val::Number(u) => Ok(*u),
other => Err(format!(
"Type mismatch: expected a Number, found `{:?}`",
other
)),
}
}
pub fn to_string(&self) -> Result<String, String> {
match self {
Val::String(s) => Ok(s.to_string()),
Val::Number(f) => Ok(format!("{}", f)),
other => Err(format!(
"Type mismatch: expected a String, found `{:?}`",
other
)),
}
}
pub fn to_bool(&self) -> Result<bool, String> {
match self {
Val::Bool(b) => Ok(*b),
other => Err(format!(
"Type mismatch: expected a Boolean, found `{:?}`",
other
)),
}
}
}