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