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
use {
super::ValueCore,
crate::{Convert, ConvertFrom, Result, Value, ValueError},
std::ops::Neg,
};
macro_rules! generic {
($name: ident, $generic_name: ident) => {
pub fn $generic_name(self) -> Result<Self> {
if !i64::convert_from(self.clone()).is_err() {
self.$name::<i64>()
} else if !f64::convert_from(self.clone()).is_err() {
self.$name::<f64>()
} else {
Err(ValueError::OnlySupportsNumeric(self, stringify!($name)).into())
}
}
};
}
#[allow(clippy::should_implement_trait)]
impl Value {
pub fn unary_plus<Core>(self) -> Result<Self>
where
Core: ValueCore + Clone,
{
let core = Core::convert_from(self)?;
let result = core;
Ok(result.into())
}
pub fn unary_minus<Core>(self) -> Result<Self>
where
Core: ValueCore + Neg<Output = Core>,
{
let core = Core::convert_from(self)?;
let result = -core;
Ok(result.into())
}
generic!(unary_plus, generic_unary_plus);
generic!(unary_minus, generic_unary_minus);
pub fn not(self) -> Result<Self> {
let boolean: bool = self.convert()?;
let result = !boolean;
Ok(result.into())
}
pub fn is_null(self) -> Result<Self> {
Ok(Value::Bool(matches!(self, Value::Null)))
}
}