aboutsummaryrefslogtreecommitdiff
path: root/rprt-engine/src/evaluate.rs
blob: b23c1bea1c3d293ea2cdcc7d4972fd586199dd34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::{
    expression::Composite,
    selection::{Selection, VectoriseError},
    selection_functions::{evaluate_selection_function, SFError},
    state::{EditorState, StateResult},
};

use thiserror::Error;

#[derive(Error, Debug)]
pub enum EvaluationError {
    #[error("{0}")]
    SelectionFunctionError(VectoriseError<SFError>),
    #[error("Invalid right-only application")]
    IROApplication,
    #[error("Not yet implemented {0}")]
    UnimplementedError(&'static str),
}

pub fn evaluate(
    es: &EditorState,
    comp: Composite,
    left: Option<Selection>,
    right: Option<Selection>,
) -> Result<StateResult, EvaluationError> {
    if left.is_none() && right.is_some() {
        return Err(EvaluationError::IROApplication);
    };

    match comp {
        Composite::SelectionFunction {
            func,
            search_mod,
            result_transform,
        } => {
            let sel =
                evaluate_selection_function(es, func, search_mod, result_transform, left, right)
                    .map_err(EvaluationError::SelectionFunctionError)?;
            Ok((sel, Vec::new()))
        }
        Composite::TextFunction { func, swapped } => {
            Err(EvaluationError::UnimplementedError("text function"))
        }
        Composite::Hook { kind, left, right } => Err(EvaluationError::UnimplementedError("hook")),
        Composite::Train2 { f, g } => {
            let (left, mut state) = evaluate(es, *f, left, right)?;
            let (sel, more_state) = evaluate(es, *g, Some(left), None)?;
            state.extend(more_state);
            Ok((sel, state))
        }
        Composite::Train3 { f, g, h } => {
            let (f_left, mut f_state) = evaluate(es, *f, left.clone(), right.clone())?;
            let (g_right, h_state) = evaluate(es, *h, left, right)?;
            let (sel, g_state) = evaluate(es, *g, Some(f_left), Some(g_right))?;
            f_state.extend(h_state);
            f_state.extend(g_state);
            Ok((sel, f_state))
        }
        Composite::Group { operations } => {
            // TODO: does rust have some monadic failure map thing on first failure?
            let mut selections = Vec::new();
            let mut states = Vec::new();
            for f in operations {
                let (sel, state) = evaluate(es, f, left.clone(), right.clone())?;
                selections.extend(sel);
                states.extend(state);
            }
        }
    }
}