Skip to main content

mln_expression/
find_zoom_curve.rs

1use mln_util::logging::{Event, Log};
2
3use crate::expression::{Dependency, Expr, Node};
4use crate::functions::Role;
5use crate::interpolate::Interpolate;
6use crate::parsing_context::ParsingError;
7use crate::step::Step;
8
9/// The alternatives of `variant<const Interpolate*, const Step*, ParsingError>`.
10#[derive(Clone)]
11pub(crate) enum ZoomCurve<'a> {
12    Interpolate(&'a Interpolate),
13    Step(&'a Step),
14    Error(ParsingError),
15}
16
17impl PartialEq for ZoomCurve<'_> {
18    fn eq(&self, other: &Self) -> bool {
19        match (self, other) {
20            (ZoomCurve::Interpolate(a), ZoomCurve::Interpolate(b)) => std::ptr::eq(*a, *b),
21            (ZoomCurve::Step(a), ZoomCurve::Step(b)) => std::ptr::eq(*a, *b),
22            (ZoomCurve::Error(a), ZoomCurve::Error(b)) => a == b,
23            _ => false,
24        }
25    }
26}
27
28impl std::fmt::Debug for ZoomCurve<'_> {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        match self {
31            ZoomCurve::Interpolate(_) => f.write_str("Interpolate"),
32            ZoomCurve::Step(_) => f.write_str("Step"),
33            ZoomCurve::Error(e) => write!(f, "Error({e:?})"),
34        }
35    }
36}
37
38pub(crate) type ZoomCurveOrError<'a> = Option<ZoomCurve<'a>>;
39
40/// The zoom curve of an expression: none, an interpolate or a step.
41#[derive(Clone, Copy)]
42pub enum ZoomCurvePtr<'a> {
43    Null,
44    Interpolate(&'a Interpolate),
45    Step(&'a Step),
46}
47
48impl ZoomCurvePtr<'_> {
49    #[must_use]
50    pub fn is_null(&self) -> bool {
51        matches!(self, ZoomCurvePtr::Null)
52    }
53}
54
55fn zoom_input(input: &Expr) -> bool {
56    input.as_compound().is_some_and(|z| z.overload().role == Role::Zoom)
57}
58
59/// Find a zoom curve in the expression tree: the relevant step or
60/// interpolate item, or the error if the expression represents an error.
61pub(crate) fn find_zoom_curve(e: &Expr) -> ZoomCurveOrError<'_> {
62    let mut result: ZoomCurveOrError<'_> = None;
63
64    match e.node() {
65        Node::Let(let_) => {
66            result = find_zoom_curve(let_.result());
67        }
68        Node::Coalesce(coalesce) => {
69            for child in coalesce.args() {
70                result = find_zoom_curve(child);
71                if result.is_some() {
72                    break;
73                }
74            }
75        }
76        Node::Interpolate(curve) => {
77            if zoom_input(curve.get_input()) {
78                result = Some(ZoomCurve::Interpolate(curve));
79            }
80        }
81        Node::Step(step) if zoom_input(step.get_input()) => {
82            result = Some(ZoomCurve::Step(step));
83        }
84        _ => {}
85    }
86
87    if let Some(ZoomCurve::Error(_)) = result {
88        return result;
89    }
90
91    e.each_child(&mut |child| {
92        let child_result = find_zoom_curve(child);
93        if let Some(child_result) = child_result {
94            if let ZoomCurve::Error(_) = child_result {
95                result = Some(child_result);
96            } else if result.is_none() {
97                result = Some(ZoomCurve::Error(ParsingError {
98                    message: r#""zoom" expression may only be used as input to a top-level "step" or "interpolate" expression."#.to_owned(),
99                    key: String::new(),
100                }));
101            } else if result.as_ref() != Some(&child_result) {
102                result = Some(ZoomCurve::Error(ParsingError {
103                    message: r#"Only one zoom-based "step" or "interpolate" subexpression may be used in an expression."#.to_owned(),
104                    key: String::new(),
105                }));
106            }
107        }
108    });
109
110    result
111}
112
113/// Find a zoom curve in the expression tree: the relevant step or
114/// interpolate item, or null if the expression represents an error, which
115/// is logged.
116#[must_use]
117pub fn find_zoom_curve_checked(e: &Expr) -> ZoomCurvePtr<'_> {
118    if !e.has(Dependency::ZOOM) {
119        return ZoomCurvePtr::Null;
120    }
121    match find_zoom_curve(e) {
122        Some(ZoomCurve::Error(err)) => {
123            Log::error(Event::Style, format!("Invalid Expression: {}", err.message));
124            ZoomCurvePtr::Null
125        }
126        Some(ZoomCurve::Interpolate(i)) => ZoomCurvePtr::Interpolate(i),
127        Some(ZoomCurve::Step(s)) => ZoomCurvePtr::Step(s),
128        None => ZoomCurvePtr::Null,
129    }
130}