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
use crate::ValueType;
use float_cmp::approx_eq;
use std::fmt;
pub struct Tape<'input>(Vec<Node<'input>>);
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Node<'input> {
String(&'input str),
Object(usize, usize),
Array(usize, usize),
Static(StaticNode),
}
#[derive(Debug, Clone, Copy)]
pub enum StaticNode {
I64(i64),
U64(u64),
F64(f64),
Bool(bool),
Null,
}
impl StaticNode {
pub fn value_type(&self) -> ValueType {
match self {
Self::Null => ValueType::Null,
Self::Bool(_) => ValueType::Bool,
Self::F64(_) => ValueType::F64,
Self::I64(_) => ValueType::I64,
Self::U64(_) => ValueType::U64,
}
}
}
#[cfg_attr(tarpaulin, skip)]
impl<'v> fmt::Display for StaticNode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Null => write!(f, "null"),
Self::Bool(b) => write!(f, "{}", b),
Self::I64(n) => write!(f, "{}", n),
Self::U64(n) => write!(f, "{}", n),
Self::F64(n) => write!(f, "{}", n),
}
}
}
#[allow(clippy::cast_sign_loss, clippy::default_trait_access)]
impl<'a> PartialEq for StaticNode {
#[inline]
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Null, Self::Null) => true,
(Self::Bool(v1), Self::Bool(v2)) => v1.eq(v2),
(Self::I64(v1), Self::I64(v2)) => v1.eq(v2),
(Self::F64(v1), Self::F64(v2)) => approx_eq!(f64, *v1, *v2),
(Self::U64(v1), Self::U64(v2)) => v1.eq(v2),
(Self::U64(v1), Self::I64(v2)) if *v2 >= 0 => (*v2 as u64).eq(v1),
(Self::I64(v1), Self::U64(v2)) if *v1 >= 0 => (*v1 as u64).eq(v2),
_ => false,
}
}
}