use core::ops::{BitAnd, BitOr, BitOrAssign};
use crate::types::{CanonicalTileId, Color, LatLng};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ExprCapabilities(u32);
impl ExprCapabilities {
pub const NONE: Self = Self(0);
pub const CAMERA: Self = Self(1 << 0);
pub const HOST: Self = Self(1 << 1);
pub const TILE: Self = Self(1 << 2);
pub const FEATURE: Self = Self(1 << 3);
pub const ALL: Self = Self(Self::CAMERA.0 | Self::HOST.0 | Self::TILE.0 | Self::FEATURE.0);
#[must_use]
pub const fn from_bits(bits: u32) -> Self {
Self(bits)
}
#[must_use]
pub const fn bits(self) -> u32 {
self.0
}
#[must_use]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
#[must_use]
pub const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
impl BitOr for ExprCapabilities {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
impl BitOrAssign for ExprCapabilities {
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
impl BitAnd for ExprCapabilities {
type Output = Self;
fn bitand(self, rhs: Self) -> Self {
Self(self.0 & rhs.0)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ExprType {
Null,
Boolean,
Number,
String,
Color,
Object,
Value,
Array { item: Box<ExprType>, len: Option<usize> },
}
impl ExprType {
#[must_use]
pub fn array(item: Self) -> Self {
Self::Array { item: Box::new(item), len: None }
}
#[must_use]
pub fn array_n(item: Self, len: usize) -> Self {
Self::Array { item: Box::new(item), len: Some(len) }
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ExprParams {
Fixed(Vec<ExprType>),
Varargs(ExprType),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ExprOverloadDesc {
pub params: ExprParams,
pub result: ExprType,
pub capabilities: ExprCapabilities,
}
impl ExprOverloadDesc {
#[must_use]
pub const fn new(params: ExprParams, result: ExprType) -> Self {
Self { params, result, capabilities: ExprCapabilities::NONE }
}
#[must_use]
pub const fn with_capabilities(mut self, capabilities: ExprCapabilities) -> Self {
self.capabilities = capabilities;
self
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ExprCamera {
pub zoom: f64,
pub bearing: f64,
pub pitch: f64,
pub roll: f64,
pub field_of_view: f64,
pub center: LatLng,
pub center_altitude: f64,
pub viewport_width: f64,
pub viewport_height: f64,
pub meters_per_pixel: f64,
pub is_globe: bool,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ExprEvalCx<'a> {
pub camera: Option<&'a ExprCamera>,
pub tile: Option<CanonicalTileId>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ExprArg<'a> {
Null,
Bool(bool),
Number(f64),
String(&'a str),
Color(Color),
ArrayJson(&'a str),
ObjectJson(&'a str),
}
#[derive(Clone, Debug, Default, PartialEq)]
pub enum ExprValue {
#[default]
Null,
Bool(bool),
Number(f64),
String(String),
Color(Color),
Json(String),
}
impl From<bool> for ExprValue {
fn from(value: bool) -> Self {
Self::Bool(value)
}
}
impl From<f64> for ExprValue {
fn from(value: f64) -> Self {
Self::Number(value)
}
}
impl From<String> for ExprValue {
fn from(value: String) -> Self {
Self::String(value)
}
}
impl From<&str> for ExprValue {
fn from(value: &str) -> Self {
Self::String(value.to_owned())
}
}
impl From<Color> for ExprValue {
fn from(value: Color) -> Self {
Self::Color(value)
}
}
pub trait ExpressionPlugin: Send + Sync + 'static {
fn name(&self) -> &str;
fn overloads(&self) -> Vec<ExprOverloadDesc>;
fn evaluate(&self, overload_index: usize, args: &[ExprArg<'_>], cx: &ExprEvalCx<'_>) -> Result<ExprValue, String>;
}