surrealdb_core/sql/
constant.rs

1use crate::err::Error;
2use crate::sql::value::Value;
3use crate::sql::Datetime;
4use chrono::TimeZone;
5use chrono::Utc;
6
7use revision::revisioned;
8use serde::{Deserialize, Serialize};
9use std::fmt;
10
11pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Constant";
12
13#[revisioned(revision = 1)]
14#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
15#[serde(rename = "$surrealdb::private::sql::Constant")]
16#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
17#[non_exhaustive]
18pub enum Constant {
19	MathE,
20	MathFrac1Pi,
21	MathFrac1Sqrt2,
22	MathFrac2Pi,
23	MathFrac2SqrtPi,
24	MathFracPi2,
25	MathFracPi3,
26	MathFracPi4,
27	MathFracPi6,
28	MathFracPi8,
29	MathInf,
30	MathLn10,
31	MathLn2,
32	MathLog102,
33	MathLog10E,
34	MathLog210,
35	MathLog2E,
36	MathNegInf,
37	MathPi,
38	MathSqrt2,
39	MathTau,
40	TimeEpoch,
41	// Add new variants here
42}
43
44/// A type of constant that may be converted to a value or JSON.
45pub(crate) enum ConstantValue {
46	Float(f64),
47	Datetime(Datetime),
48}
49
50impl Constant {
51	pub(crate) fn value(&self) -> ConstantValue {
52		use std::f64::consts as f64c;
53		match self {
54			Self::MathE => ConstantValue::Float(f64c::E),
55			Self::MathFrac1Pi => ConstantValue::Float(f64c::FRAC_1_PI),
56			Self::MathFrac1Sqrt2 => ConstantValue::Float(f64c::FRAC_1_SQRT_2),
57			Self::MathFrac2Pi => ConstantValue::Float(f64c::FRAC_2_PI),
58			Self::MathFrac2SqrtPi => ConstantValue::Float(f64c::FRAC_2_SQRT_PI),
59			Self::MathFracPi2 => ConstantValue::Float(f64c::FRAC_PI_2),
60			Self::MathFracPi3 => ConstantValue::Float(f64c::FRAC_PI_3),
61			Self::MathFracPi4 => ConstantValue::Float(f64c::FRAC_PI_4),
62			Self::MathFracPi6 => ConstantValue::Float(f64c::FRAC_PI_6),
63			Self::MathFracPi8 => ConstantValue::Float(f64c::FRAC_PI_8),
64			Self::MathInf => ConstantValue::Float(f64::INFINITY),
65			Self::MathLn10 => ConstantValue::Float(f64c::LN_10),
66			Self::MathLn2 => ConstantValue::Float(f64c::LN_2),
67			Self::MathLog102 => ConstantValue::Float(f64c::LOG10_2),
68			Self::MathLog10E => ConstantValue::Float(f64c::LOG10_E),
69			Self::MathLog210 => ConstantValue::Float(f64c::LOG2_10),
70			Self::MathLog2E => ConstantValue::Float(f64c::LOG2_E),
71			Self::MathNegInf => ConstantValue::Float(f64::NEG_INFINITY),
72			Self::MathPi => ConstantValue::Float(f64c::PI),
73			Self::MathSqrt2 => ConstantValue::Float(f64c::SQRT_2),
74			Self::MathTau => ConstantValue::Float(f64c::TAU),
75			Self::TimeEpoch => ConstantValue::Datetime(Datetime(Utc.timestamp_nanos(0))),
76		}
77	}
78	/// Process this type returning a computed simple Value
79	pub fn compute(&self) -> Result<Value, Error> {
80		Ok(match self.value() {
81			ConstantValue::Datetime(d) => d.into(),
82			ConstantValue::Float(f) => f.into(),
83		})
84	}
85}
86
87impl fmt::Display for Constant {
88	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89		f.write_str(match self {
90			Self::MathE => "math::E",
91			Self::MathFrac1Pi => "math::FRAC_1_PI",
92			Self::MathFrac1Sqrt2 => "math::FRAC_1_SQRT_2",
93			Self::MathFrac2Pi => "math::FRAC_2_PI",
94			Self::MathFrac2SqrtPi => "math::FRAC_2_SQRT_PI",
95			Self::MathFracPi2 => "math::FRAC_PI_2",
96			Self::MathFracPi3 => "math::FRAC_PI_3",
97			Self::MathFracPi4 => "math::FRAC_PI_4",
98			Self::MathFracPi6 => "math::FRAC_PI_6",
99			Self::MathFracPi8 => "math::FRAC_PI_8",
100			Self::MathInf => "math::INF",
101			Self::MathLn10 => "math::LN_10",
102			Self::MathLn2 => "math::LN_2",
103			Self::MathLog102 => "math::LOG10_2",
104			Self::MathLog10E => "math::LOG10_E",
105			Self::MathLog210 => "math::LOG2_10",
106			Self::MathLog2E => "math::LOG2_E",
107			Self::MathNegInf => "math::NEG_INF",
108			Self::MathPi => "math::PI",
109			Self::MathSqrt2 => "math::SQRT_2",
110			Self::MathTau => "math::TAU",
111			Self::TimeEpoch => "time::EPOCH",
112		})
113	}
114}