aboutsummaryrefslogtreecommitdiff
path: root/rprt-engine/src/evaluate.rs
blob: 4f567fbb4c7c19cd35c560ea2ac3ce96e8030a5d (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use crate::{
    expression::{Composite, HookKind},
    selection::{Selection, SelectionError, VectoriseError},
    selection_functions::{evaluate_selection_function, SFError},
    state::{EditorState, GroupedChangeError, StateChange, StateResult},
    text::Text,
};

use thiserror::Error;

#[derive(Error, Debug)]
pub enum EvaluationError {
    #[error("{0}")]
    SelectionFunctionError(VectoriseError<SFError>),
    #[error("{0}")]
    SelectionUnionError(SelectionError),
    #[error("Error in processing group {0}")]
    GroupError(GroupedChangeError),
    #[error("Invalid right-only application")]
    IROApplication,
    #[error("Not yet implemented {0}")]
    UnimplementedError(&'static str),
    #[error("Hook used in niladic context")]
    NiladicRightHook,
    #[error("Text argument provided to a selection function")]
    TextArgument,
}

// Helper functions

// Absolutely crazy that in 2026 Rust doesn't have a built-in Either type OR
// (even better) a way to do anonymous sum types! Of course it has anonymous
// product types, but big Cartesian doesn't want you to have +

#[derive(Clone)]
enum Either<L, R> {
    L(L),
    R(R),
}

fn ensure_selection(
    left: Option<Either<Selection, Text>>,
) -> Result<Option<Selection>, EvaluationError> {
    match left {
        Some(Either::L(left)) => Ok(Some(left)),
        None => Ok(None),
        _ => Err(EvaluationError::TextArgument),
    }
}

fn commit_if_needed(
    es: &mut EditorState,
    changes: Vec<StateChange>,
    commit: bool,
) -> Vec<StateChange> {
    if commit {
        es.commit_changes(changes);
        Vec::new()
    } else {
        changes
    }
}

fn _eval(
    es: &mut EditorState,
    comp: Composite,
    left: Option<Either<Selection, Text>>,
    right: Option<Selection>,
    commit: bool,
) -> Result<StateResult, EvaluationError> {
    if left.is_none() && right.is_some() {
        return Err(EvaluationError::IROApplication);
    };

    match comp {
        Composite::SelectionFunction {
            func,
            search_mod,
            result_transform,
        } => {
            let left: Option<Selection> = ensure_selection(left)?;
            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, f, g } => match kind {
            HookKind::Before => {
                let (f_result, mut changes) = _eval(es, *f, left.clone(), None, commit)?;
                changes = commit_if_needed(es, changes, commit);
                let right = if right.is_some() {
                    Ok(right)
                } else {
                    ensure_selection(left)
                }?;
                let (sel, g_changes) = _eval(es, *g, Either::L(f_result).into(), right, commit)?;
                let more_changes = commit_if_needed(es, g_changes, commit);
                changes.extend(more_changes);
                Ok((sel, changes))
            }
            HookKind::After => {
                if left.is_none() {
                    return Err(EvaluationError::NiladicRightHook);
                }
                let combi_left = match right {
                    Some(right) => Some(Either::L(right)),
                    None => left.clone(),
                };
                let (g_result, mut changes) = _eval(es, *g, combi_left, None, commit)?;
                changes = commit_if_needed(es, changes, commit);
                let (sel, f_changes) = _eval(es, *f, left.clone(), Some(g_result), commit)?;
                let more_changes = commit_if_needed(es, f_changes, commit);
                changes.extend(more_changes);
                Ok((sel, changes))
            }
        },
        Composite::Train2 { f, g } => {
            let (left, mut f_changes) = _eval(es, *f, left, right, commit)?;
            f_changes = commit_if_needed(es, f_changes, commit);
            let (sel, g_changes) = _eval(es, *g, Some(Either::L(left)), None, commit)?;
            let more_changes = commit_if_needed(es, g_changes, commit);
            f_changes.extend(more_changes);
            Ok((sel, f_changes))
        }
        Composite::Train3 { f, g, h } => {
            let (f_left, mut f_changes) = _eval(es, *f, left.clone(), right.clone(), commit)?;
            f_changes = commit_if_needed(es, f_changes, commit);
            let (g_right, h_changes) = _eval(es, *h, left, right, commit)?;
            let h_changes = commit_if_needed(es, h_changes, commit);
            let (sel, g_changes) = _eval(es, *g, Some(Either::L(f_left)), Some(g_right), commit)?;
            let g_changes = commit_if_needed(es, g_changes, commit);
            f_changes.extend(h_changes);
            f_changes.extend(g_changes);
            Ok((sel, f_changes))
        }
        Composite::Group { operations } => {
            let results: Vec<(Selection, Vec<StateChange>)> = operations
                .into_iter()
                .map(|op| _eval(es, op, left.clone(), right.clone(), commit))
                .collect::<Result<_, _>>()?;

            let (selections, arms) = results.into_iter().unzip();

            let states = if commit {
                es.commit_changes_grouped(arms).map(|_| Vec::new())
            } else {
                es.validate_grouped_changes(&arms)
                    .map(|_| arms.into_iter().flatten().collect())
            }
            .map_err(EvaluationError::GroupError)?;

            let sel = Selection::union(selections).map_err(EvaluationError::SelectionUnionError)?;
            Ok((sel, states))
        }
    }
}

pub fn evaluate(es: &mut EditorState, comp: Composite) -> Result<Selection, EvaluationError> {
    let (sel, changes) = _eval(es, comp, None, None, true)?;
    es.commit_changes(changes);
    Ok(sel)
}