vegafusion_runtime/expression/compiler/
config.rs

1use crate::expression::compiler::call::{default_callables, VegaFusionCallable};
2use crate::task_graph::timezone::RuntimeTzConfig;
3use num_traits::float::FloatConst;
4use std::collections::HashMap;
5use vegafusion_common::data::table::VegaFusionTable;
6use vegafusion_common::datafusion_common::ScalarValue;
7
8#[derive(Clone)]
9pub struct CompilationConfig {
10    pub signal_scope: HashMap<String, ScalarValue>,
11    pub data_scope: HashMap<String, VegaFusionTable>,
12    pub callable_scope: HashMap<String, VegaFusionCallable>,
13    pub constants: HashMap<String, ScalarValue>,
14    pub tz_config: Option<RuntimeTzConfig>,
15}
16
17impl Default for CompilationConfig {
18    fn default() -> Self {
19        Self {
20            signal_scope: Default::default(),
21            data_scope: Default::default(),
22            callable_scope: default_callables(),
23            constants: default_constants(),
24            tz_config: None,
25        }
26    }
27}
28
29/// ## Constants
30/// Constant values that can be referenced by name within expressions.
31///
32/// See: https://vega.github.io/vega/docs/expressions/#bound-variables
33fn default_constants() -> HashMap<String, ScalarValue> {
34    let mut constants = HashMap::new();
35
36    // # NaN
37    // Not a number. Same as the JavaScript literal NaN.
38    constants.insert("NaN".to_string(), ScalarValue::from(f64::NAN));
39
40    // # E
41    // The transcendental number e. Same as JavaScript’s Math.E.
42    constants.insert("E".to_string(), ScalarValue::from(f64::E()));
43
44    // # LN2
45    // The natural log of 2. Same as JavaScript’s Math.LN2.
46    constants.insert("LN2".to_string(), ScalarValue::from(f64::LN_2()));
47
48    // # LN10
49    // The natural log of 10. Same as JavaScript’s Math.LN10.
50    constants.insert("LN10".to_string(), ScalarValue::from(f64::LN_10()));
51
52    // # LOG2E
53    // The base 2 logarithm of e. Same as JavaScript’s Math.LOG2E.
54    constants.insert("LOG2E".to_string(), ScalarValue::from(f64::LOG2_E()));
55
56    // # LOG10E
57    // The base 10 logarithm e. Same as JavaScript’s Math.LOG10E.
58    constants.insert("LOG10E".to_string(), ScalarValue::from(f64::LOG10_E()));
59
60    // # MAX_VALUE
61    // The largest positive numeric value. Same as JavaScript’s Number.MAX_VALUE.
62    constants.insert("MAX_VALUE".to_string(), ScalarValue::from(f64::MAX));
63
64    // # MIN_VALUE
65    // The smallest positive numeric value. Same as JavaScript’s Number.MIN_VALUE.
66    constants.insert(
67        "MIN_VALUE".to_string(),
68        ScalarValue::from(f64::MIN_POSITIVE),
69    );
70
71    // # PI
72    // The transcendental number π. Same as JavaScript’s Math.PI.
73    constants.insert("PI".to_string(), ScalarValue::from(f64::PI()));
74
75    // # SQRT1_2
76    // The square root of 0.5. Same as JavaScript’s Math.SQRT1_2.
77    constants.insert(
78        "SQRT1_2".to_string(),
79        ScalarValue::from(f64::FRAC_1_SQRT_2()),
80    );
81
82    // # SQRT2
83    // The square root of 2. Same as JavaScript’s Math.SQRT2.
84    constants.insert("SQRT2".to_string(), ScalarValue::from(f64::SQRT_2()));
85
86    constants
87}