ferroui_base/rendering/composition/expressions/expression_evaluation_context.rs
1//! The context an expression is evaluated in and the interfaces of the
2//! objects it reads from.
3//!
4//! These traits are the seam between the expression engine and the server
5//! side composition objects: a server object implements [`IExpressionObject`],
6//! a property set snapshot implements [`IExpressionParameterCollection`].
7
8use super::expression_tracked_values::ExpressionObjectKey;
9use super::expression_variant::ExpressionVariant;
10
11/// The values and objects an expression is evaluated against.
12#[derive(Clone, Copy, Default)]
13pub struct ExpressionEvaluationContext<'a> {
14 pub starting_value: ExpressionVariant,
15 pub current_value: ExpressionVariant,
16 pub final_value: ExpressionVariant,
17 /// The object `this.Target` refers to.
18 pub target: Option<&'a dyn IExpressionObject>,
19 /// The parameters of the animation.
20 pub parameters: Option<&'a dyn IExpressionParameterCollection>,
21 /// The functions callable from the expression.
22 pub foreign_function_interface: Option<&'a dyn IExpressionForeignFunctionInterface>,
23}
24
25/// An object whose properties can be read by an expression.
26pub trait IExpressionObject {
27 /// Gets the value of the property with the given name; `Invalid` when
28 /// there is no such property.
29 fn get_property(&self, name: &str) -> ExpressionVariant;
30
31 /// The identity of the object, used to track the objects an expression
32 /// depends on (see `ExpressionTrackedObjects`). The default is the
33 /// address of the object, which is its reference identity for as long as
34 /// the object is alive and not moved; an implementation that has a stable
35 /// identifier should return that instead.
36 fn key(&self) -> ExpressionObjectKey {
37 ExpressionObjectKey(self as *const Self as *const () as usize)
38 }
39}
40
41/// The named parameters of an expression animation.
42pub trait IExpressionParameterCollection {
43 /// Gets the value of a parameter; `Invalid` when there is no such parameter.
44 fn get_parameter(&self, name: &str) -> ExpressionVariant;
45
46 /// Gets the object a parameter refers to, if the parameter is an object.
47 fn get_object_parameter(&self, name: &str) -> Option<&dyn IExpressionObject>;
48}
49
50/// The functions callable from an expression.
51pub trait IExpressionForeignFunctionInterface {
52 /// Calls the function with the given name and arguments. Returns `None`
53 /// when there is no function with that name accepting the arguments.
54 fn call(&self, name: &str, arguments: &[ExpressionVariant]) -> Option<ExpressionVariant>;
55}