1use core::ops::{BitAnd, BitOr, BitOrAssign};
9
10use crate::types::{CanonicalTileId, Color, LatLng};
11
12#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
18pub struct ExprCapabilities(u32);
19
20impl ExprCapabilities {
21 pub const NONE: Self = Self(0);
23 pub const CAMERA: Self = Self(1 << 0);
25 pub const HOST: Self = Self(1 << 1);
28 pub const TILE: Self = Self(1 << 2);
30 pub const FEATURE: Self = Self(1 << 3);
33
34 pub const ALL: Self = Self(Self::CAMERA.0 | Self::HOST.0 | Self::TILE.0 | Self::FEATURE.0);
36
37 #[must_use]
39 pub const fn from_bits(bits: u32) -> Self {
40 Self(bits)
41 }
42
43 #[must_use]
45 pub const fn bits(self) -> u32 {
46 self.0
47 }
48
49 #[must_use]
51 pub const fn is_empty(self) -> bool {
52 self.0 == 0
53 }
54
55 #[must_use]
57 pub const fn contains(self, other: Self) -> bool {
58 (self.0 & other.0) == other.0
59 }
60
61 #[must_use]
63 pub const fn union(self, other: Self) -> Self {
64 Self(self.0 | other.0)
65 }
66}
67
68impl BitOr for ExprCapabilities {
69 type Output = Self;
70
71 fn bitor(self, rhs: Self) -> Self {
72 Self(self.0 | rhs.0)
73 }
74}
75
76impl BitOrAssign for ExprCapabilities {
77 fn bitor_assign(&mut self, rhs: Self) {
78 self.0 |= rhs.0;
79 }
80}
81
82impl BitAnd for ExprCapabilities {
83 type Output = Self;
84
85 fn bitand(self, rhs: Self) -> Self {
86 Self(self.0 & rhs.0)
87 }
88}
89
90#[derive(Clone, Debug, PartialEq, Eq)]
92pub enum ExprType {
93 Null,
94 Boolean,
95 Number,
96 String,
97 Color,
98 Object,
99 Value,
100 Array { item: Box<ExprType>, len: Option<usize> },
101}
102
103impl ExprType {
104 #[must_use]
106 pub fn array(item: Self) -> Self {
107 Self::Array { item: Box::new(item), len: None }
108 }
109
110 #[must_use]
112 pub fn array_n(item: Self, len: usize) -> Self {
113 Self::Array { item: Box::new(item), len: Some(len) }
114 }
115}
116
117#[derive(Clone, Debug, PartialEq, Eq)]
119pub enum ExprParams {
120 Fixed(Vec<ExprType>),
122 Varargs(ExprType),
124}
125
126#[derive(Clone, Debug, PartialEq, Eq)]
128pub struct ExprOverloadDesc {
129 pub params: ExprParams,
130 pub result: ExprType,
131 pub capabilities: ExprCapabilities,
132}
133
134impl ExprOverloadDesc {
135 #[must_use]
137 pub const fn new(params: ExprParams, result: ExprType) -> Self {
138 Self { params, result, capabilities: ExprCapabilities::NONE }
139 }
140
141 #[must_use]
143 pub const fn with_capabilities(mut self, capabilities: ExprCapabilities) -> Self {
144 self.capabilities = capabilities;
145 self
146 }
147}
148
149#[derive(Clone, Copy, Debug, Default, PartialEq)]
151pub struct ExprCamera {
152 pub zoom: f64,
153 pub bearing: f64,
155 pub pitch: f64,
157 pub roll: f64,
159 pub field_of_view: f64,
161 pub center: LatLng,
163 pub center_altitude: f64,
165 pub viewport_width: f64,
167 pub viewport_height: f64,
169 pub meters_per_pixel: f64,
171 pub is_globe: bool,
173}
174
175#[derive(Clone, Copy, Debug, Default, PartialEq)]
177pub struct ExprEvalCx<'a> {
178 pub camera: Option<&'a ExprCamera>,
180 pub tile: Option<CanonicalTileId>,
182}
183
184#[derive(Clone, Copy, Debug, PartialEq)]
186pub enum ExprArg<'a> {
187 Null,
188 Bool(bool),
189 Number(f64),
190 String(&'a str),
191 Color(Color),
192 ArrayJson(&'a str),
194 ObjectJson(&'a str),
196}
197
198#[derive(Clone, Debug, Default, PartialEq)]
200pub enum ExprValue {
201 #[default]
202 Null,
203 Bool(bool),
204 Number(f64),
205 String(String),
206 Color(Color),
207 Json(String),
209}
210
211impl From<bool> for ExprValue {
212 fn from(value: bool) -> Self {
213 Self::Bool(value)
214 }
215}
216
217impl From<f64> for ExprValue {
218 fn from(value: f64) -> Self {
219 Self::Number(value)
220 }
221}
222
223impl From<String> for ExprValue {
224 fn from(value: String) -> Self {
225 Self::String(value)
226 }
227}
228
229impl From<&str> for ExprValue {
230 fn from(value: &str) -> Self {
231 Self::String(value.to_owned())
232 }
233}
234
235impl From<Color> for ExprValue {
236 fn from(value: Color) -> Self {
237 Self::Color(value)
238 }
239}
240
241pub trait ExpressionPlugin: Send + Sync + 'static {
247 fn name(&self) -> &str;
249
250 fn overloads(&self) -> Vec<ExprOverloadDesc>;
252
253 fn evaluate(&self, overload_index: usize, args: &[ExprArg<'_>], cx: &ExprEvalCx<'_>) -> Result<ExprValue, String>;
256}