surrealdb_sql/
constant.rs1use crate::ctx::Context;
2use crate::dbs::{Options, Transaction};
3use crate::doc::CursorDoc;
4use crate::err::Error;
5use crate::value::Value;
6use crate::Datetime;
7use chrono::TimeZone;
8use chrono::Utc;
9use derive::Store;
10use revision::revisioned;
11use serde::{Deserialize, Serialize};
12use std::fmt;
13
14pub(crate) const TOKEN: &str = "$surrealdb::private::crate::Constant";
15
16#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
17#[serde(rename = "$surrealdb::private::crate::Constant")]
18#[revisioned(revision = 1)]
19pub enum Constant {
20 MathE,
21 MathFrac1Pi,
22 MathFrac1Sqrt2,
23 MathFrac2Pi,
24 MathFrac2SqrtPi,
25 MathFracPi2,
26 MathFracPi3,
27 MathFracPi4,
28 MathFracPi6,
29 MathFracPi8,
30 MathInf,
31 MathLn10,
32 MathLn2,
33 MathLog102,
34 MathLog10E,
35 MathLog210,
36 MathLog2E,
37 MathPi,
38 MathSqrt2,
39 MathTau,
40 TimeEpoch,
41 }
43
44pub(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::MathPi => ConstantValue::Float(f64c::PI),
72 Self::MathSqrt2 => ConstantValue::Float(f64c::SQRT_2),
73 Self::MathTau => ConstantValue::Float(f64c::TAU),
74 Self::TimeEpoch => ConstantValue::Datetime(Datetime(Utc.timestamp_nanos(0))),
75 }
76 }
77 pub(crate) async fn compute(
79 &self,
80 _ctx: &Context<'_>,
81 _opt: &Options,
82 _txn: &Transaction,
83 _doc: Option<&CursorDoc<'_>>,
84 ) -> Result<Value, Error> {
85 Ok(match self.value() {
86 ConstantValue::Datetime(d) => d.into(),
87 ConstantValue::Float(f) => f.into(),
88 })
89 }
90}
91
92impl fmt::Display for Constant {
93 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94 f.write_str(match self {
95 Self::MathE => "math::E",
96 Self::MathFrac1Pi => "math::FRAC_1_PI",
97 Self::MathFrac1Sqrt2 => "math::FRAC_1_SQRT_2",
98 Self::MathFrac2Pi => "math::FRAC_2_PI",
99 Self::MathFrac2SqrtPi => "math::FRAC_2_SQRT_PI",
100 Self::MathFracPi2 => "math::FRAC_PI_2",
101 Self::MathFracPi3 => "math::FRAC_PI_3",
102 Self::MathFracPi4 => "math::FRAC_PI_4",
103 Self::MathFracPi6 => "math::FRAC_PI_6",
104 Self::MathFracPi8 => "math::FRAC_PI_8",
105 Self::MathInf => "math::INF",
106 Self::MathLn10 => "math::LN_10",
107 Self::MathLn2 => "math::LN_2",
108 Self::MathLog102 => "math::LOG10_2",
109 Self::MathLog10E => "math::LOG10_E",
110 Self::MathLog210 => "math::LOG2_10",
111 Self::MathLog2E => "math::LOG2_E",
112 Self::MathPi => "math::PI",
113 Self::MathSqrt2 => "math::SQRT_2",
114 Self::MathTau => "math::TAU",
115 Self::TimeEpoch => "time::EPOCH",
116 })
117 }
118}