1use crate::array::Array;
2use crate::object::Object;
3
4#[derive(Clone, Debug)]
6pub enum Number {
7 Decimal(f64),
8 Integer(i128),
9}
10
11#[derive(Clone, Debug)]
13pub enum Value {
14 Array(Array),
15 Bool(bool),
16 Null,
17 Number(Number),
18 Object(Object),
19 String(String),
20}
21
22impl PartialEq for Number {
23 fn eq(&self, other: &Self) -> bool {
24 match self {
25 Number::Decimal(n) => Some(n) == other.as_decimal().as_ref(),
26 Number::Integer(n) => Some(n) == other.as_integer().as_ref(),
27 }
28 }
29}
30
31impl Number {
32 pub fn as_decimal(&self) -> Option<f64> {
33 match self {
34 Number::Decimal(v) => Some(*v),
35 Number::Integer(_) => None,
36 }
37 }
38
39 pub fn as_integer(&self) -> Option<i128> {
40 match self {
41 Number::Decimal(_) => None,
42 Number::Integer(v) => Some(*v),
43 }
44 }
45
46 pub fn is_decimal(&self) -> bool {
47 matches!(self, Number::Decimal(_))
48 }
49
50 pub fn is_integer(&self) -> bool {
51 matches!(self, Number::Integer(_))
52 }
53}
54
55impl PartialEq for Value {
56 fn eq(&self, other: &Self) -> bool {
57 match self {
58 Value::Array(v) => Some(v) == other.as_array().as_ref(),
59 Value::Bool(v) => Some(v) == other.as_bool().as_ref(),
60 Value::Null => other.is_null(),
61 Value::Number(v) => Some(v) == other.as_number().as_ref(),
62 Value::Object(v) => Some(v) == other.as_object().as_ref(),
63 Value::String(v) => Some(v) == other.as_string().as_ref(),
64 }
65 }
66}
67
68impl Value {
69 pub fn as_array(&self) -> Option<Array> {
70 match self {
71 Value::Array(a) => Some(a.clone()),
72 _ => None,
73 }
74 }
75
76 pub fn as_bool(&self) -> Option<bool> {
77 match self {
78 Value::Bool(b) => Some(*b),
79 _ => None,
80 }
81 }
82
83 pub fn as_decimal(&self) -> Option<f64> {
84 match self {
85 Value::Number(n) => Number::as_decimal(n),
86 _ => None,
87 }
88 }
89
90 pub fn as_integer(&self) -> Option<i128> {
91 match self {
92 Value::Number(n) => Number::as_integer(n),
93 _ => None,
94 }
95 }
96
97 pub fn as_number(&self) -> Option<Number> {
98 match self {
99 Value::Number(n) => Some(n.clone()),
100 _ => None,
101 }
102 }
103
104 pub fn as_object(&self) -> Option<Object> {
105 match self {
106 Value::Object(o) => Some(o.clone()),
107 _ => None,
108 }
109 }
110
111 pub fn as_string(&self) -> Option<String> {
112 match self {
113 Value::String(s) => Some(s.clone()),
114 _ => None,
115 }
116 }
117
118 pub fn is_array(&self) -> bool {
119 matches!(self, Value::Array(_))
120 }
121
122 pub fn is_bool(&self) -> bool {
123 matches!(self, Value::Bool(_))
124 }
125
126 pub fn is_decimal(&self) -> bool {
127 match self {
128 Value::Number(n) => Number::is_decimal(n),
129 _ => false,
130 }
131 }
132
133 pub fn is_integer(&self) -> bool {
134 match self {
135 Value::Number(n) => Number::is_integer(n),
136 _ => false,
137 }
138 }
139
140 pub fn is_null(&self) -> bool {
141 matches!(self, Value::Null)
142 }
143
144 pub fn is_number(&self) -> bool {
145 matches!(self, Value::Number(_))
146 }
147
148 pub fn is_object(&self) -> bool {
149 matches!(self, Value::Object(_))
150 }
151
152 pub fn is_string(&self) -> bool {
153 matches!(self, Value::String(_))
154 }
155}