aboutsummaryrefslogtreecommitdiff
path: root/rprt-engine/src/selection_functions/types.rs
blob: 4cd76d3971cda7c626cc6c0e125fc49951a5893f (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use crate::{
    buffer::{Buffer, BufferID},
    expression::BuiltinSelectionFn,
    selection::{Interval, Selection, Vectorisable},
    state::EditorState,
};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum SelectionFunctionError {
    #[error("Could not find buffer {0}")]
    BufferNotFound(BufferID),
    #[error("Function {0} has no dyadic form")]
    NoDyadicForm(&'static str),
    #[error("Function {0} has no niladic form")]
    NoNiladicForm(&'static str),
    #[error("Selection functions take their primary argument on the left")]
    NoLeftArgument,
    #[error("{0} is not yet implemented")]
    NotImplemented(BuiltinSelectionFn),
}

/// A selection function parameterised by `P` (the type of its parameter,
/// e.g. `usize` for character offsets).
///
/// Each concrete function is a zero-sized struct implementing this trait;
/// the parameter value is passed at call time. The arity of the call
/// (niladic/monadic/dyadic) is routed by [`SelectionFunction::apply`].
pub trait SelectionFunction<P>: Vectorisable<P, SelectionFunctionError> {
    /// Name of the function, used in error messages.
    const NAME: &'static str;

    /// The monadic form applied to a rank-0 (scalar) element.
    fn monadic_rank0(
        &self,
        param: &P,
        es: &EditorState,
        buffer_id: BufferID,
        pos: usize,
    ) -> Result<Selection, SelectionFunctionError>;

    /// The monadic form applied to a rank-1 (interval) element.
    fn monadic_rank1(
        &self,
        param: &P,
        es: &EditorState,
        buffer_id: BufferID,
        interval: &Interval,
    ) -> Result<Selection, SelectionFunctionError>;

    /// The niladic form.
    fn niladic(&self, _param: &P, _es: &EditorState) -> Result<Selection, SelectionFunctionError> {
        Err(SelectionFunctionError::NoNiladicForm(Self::NAME))
    }

    /// The monadic form: vectorise over `left`.
    fn monadic(
        &self,
        param: &P,
        es: &EditorState,
        left: &Selection,
    ) -> Result<Selection, SelectionFunctionError> {
        self.vectorise(param, es, left)
    }

    /// The dyadic form.
    fn dyadic(
        &self,
        _param: &P,
        _es: &EditorState,
        _left: &Selection,
        _right: &Selection,
    ) -> Result<Selection, SelectionFunctionError> {
        Err(SelectionFunctionError::NoDyadicForm(Self::NAME))
    }

    /// Route to the arity form matching the selections supplied.
    fn apply(
        &self,
        param: &P,
        es: &EditorState,
        left: Option<&Selection>,
        right: Option<&Selection>,
    ) -> Result<Selection, SelectionFunctionError> {
        match (left, right) {
            (None, None) => self.niladic(param, es),
            (Some(left), None) => self.monadic(param, es, left),
            (Some(left), Some(right)) => self.dyadic(param, es, left, right),
            (None, Some(_)) => Err(SelectionFunctionError::NoLeftArgument),
        }
    }
}

/// Every selection function can vectorise a selection by applying its
/// monadic rank-0/rank-1 forms to each element.
impl<P, F: SelectionFunction<P> + ?Sized> Vectorisable<P, SelectionFunctionError> for F {
    fn rank0(
        &self,
        param: &P,
        es: &EditorState,
        buffer_id: BufferID,
        pos: usize,
    ) -> Result<Selection, SelectionFunctionError> {
        self.monadic_rank0(param, es, buffer_id, pos)
    }

    fn rank1(
        &self,
        param: &P,
        es: &EditorState,
        buffer_id: BufferID,
        interval: &Interval,
    ) -> Result<Selection, SelectionFunctionError> {
        self.monadic_rank1(param, es, buffer_id, interval)
    }
}

/// A selection function with no parameter.
///
/// Blanket-implements [`SelectionFunction`] with `P = ()`, so
/// unparameterised functions are callable through the same interface.
pub trait UnparameterisedSelectionFunction {
    /// Name of the function, used in error messages.
    const NAME: &'static str;

    /// The monadic form applied to a rank-0 (scalar) element.
    fn monadic_rank0(
        &self,
        es: &EditorState,
        buffer_id: BufferID,
        pos: usize,
    ) -> Result<Selection, SelectionFunctionError>;

    /// The monadic form applied to a rank-1 (interval) element.
    fn monadic_rank1(
        &self,
        es: &EditorState,
        buffer_id: BufferID,
        interval: &Interval,
    ) -> Result<Selection, SelectionFunctionError>;

    /// The niladic form.
    fn niladic(&self, _es: &EditorState) -> Result<Selection, SelectionFunctionError> {
        Err(SelectionFunctionError::NoNiladicForm(Self::NAME))
    }

    /// The dyadic form.
    fn dyadic(
        &self,
        _es: &EditorState,
        _left: &Selection,
        _right: &Selection,
    ) -> Result<Selection, SelectionFunctionError> {
        Err(SelectionFunctionError::NoDyadicForm(Self::NAME))
    }
}

impl<F: UnparameterisedSelectionFunction + ?Sized> SelectionFunction<()> for F {
    const NAME: &'static str = <F as UnparameterisedSelectionFunction>::NAME;

    fn monadic_rank0(
        &self,
        _param: &(),
        es: &EditorState,
        buffer_id: BufferID,
        pos: usize,
    ) -> Result<Selection, SelectionFunctionError> {
        UnparameterisedSelectionFunction::monadic_rank0(self, es, buffer_id, pos)
    }

    fn monadic_rank1(
        &self,
        _param: &(),
        es: &EditorState,
        buffer_id: BufferID,
        interval: &Interval,
    ) -> Result<Selection, SelectionFunctionError> {
        UnparameterisedSelectionFunction::monadic_rank1(self, es, buffer_id, interval)
    }

    fn niladic(&self, _param: &(), es: &EditorState) -> Result<Selection, SelectionFunctionError> {
        UnparameterisedSelectionFunction::niladic(self, es)
    }

    fn dyadic(
        &self,
        _param: &(),
        es: &EditorState,
        left: &Selection,
        right: &Selection,
    ) -> Result<Selection, SelectionFunctionError> {
        UnparameterisedSelectionFunction::dyadic(self, es, left, right)
    }
}

pub fn get_buffer(
    es: &EditorState,
    buffer_id: BufferID,
) -> Result<&Buffer, SelectionFunctionError> {
    if let Some(buf) = es.get_buffer(buffer_id) {
        Ok(buf)
    } else {
        Err(SelectionFunctionError::BufferNotFound(es.current_buffer_id))
    }
}