aboutsummaryrefslogtreecommitdiff
path: root/rprt-engine/src/selection_function.rs
blob: 6f056506e1bd9e9b4994674ba66b25bb064876d2 (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
use crate::buffer::BufferID;
use crate::selection::{Interval, Selection, VectoriseError};
use crate::state::EditorState;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum SFError {}

struct SFComponents {
    niladic: Box<dyn Fn(&EditorState) -> Result<Selection, SFError>>,
    monadic_rank0: Box<dyn Fn(&EditorState, &BufferID, usize) -> Result<Selection, SFError>>,
    monadic_rank1: Box<dyn Fn(&EditorState, &BufferID, &Interval) -> Result<Selection, SFError>>,
    dyadic: Option<Box<dyn Fn(&EditorState, &Selection, &Selection) -> Result<Selection, SFError>>>,
}

pub struct SelectionFunction<E> {
    niladic: Box<dyn Fn(&EditorState) -> Result<Selection, E>>,
    monadic: Box<dyn Fn(&EditorState, &Selection) -> Result<Selection, E>>,
    dyadic: Option<Box<dyn Fn(&EditorState, &Selection, &Selection) -> Result<Selection, E>>>,
}

impl From<SFComponents> for SelectionFunction<VectoriseError<SFError>> {
    fn from(sfc: SFComponents) -> Self {
        let dyadic: Option<Box<dyn Fn(&EditorState, &Selection, &Selection) -> _>> =
            if let Some(func) = sfc.dyadic {
                Some(Box::new(
                    move |editor_state: &EditorState, left: &Selection, right: &Selection| {
                        (func)(editor_state, left, right).map_err(VectoriseError::ProcessingError)
                    },
                ))
            } else {
                None
            };

        Self {
            niladic: Box::new(move |editor_state: &EditorState| {
                (sfc.niladic)(editor_state).map_err(VectoriseError::ProcessingError)
            }),
            monadic: Box::new(Selection::vectorise(sfc.monadic_rank0, sfc.monadic_rank1)),
            dyadic: dyadic,
        }
    }
}