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

#[derive(Error, Debug)]
pub enum SFError {
    #[error("Could not find buffer {0}")]
    BufferNotFound(BufferID),
}

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
            };
        let niladic = Box::new(move |editor_state: &EditorState| {
            (sfc.niladic)(editor_state).map_err(VectoriseError::ProcessingError)
        });
        let monadic = Box::new(Selection::vectorise(sfc.monadic_rank0, sfc.monadic_rank1));
        Self {
            niladic,
            monadic,
            dyadic,
        }
    }
}

impl SelectionFunction<VectoriseError<SFError>> {
    pub fn empty() -> Self {
        SFComponents {
            niladic: Box::new(|_| Ok(Selection::empty())),
            monadic_rank0: Box::new(|_, _, _| Ok(Selection::empty())),
            monadic_rank1: Box::new(|_, _, _| Ok(Selection::empty())),
            dyadic: None,
        }
        .into()
    }

    pub fn end_sequential() -> Self {
        fn get_end_position(es: &EditorState, buffer_id: BufferID) -> Result<Selection, SFError> {
            if let Some(buf) = es.get_buffer(buffer_id) {
                Ok(Selection::Position {
                    buffer_id: buffer_id,
                    pos: buf.max_pos(),
                })
            } else {
                Err(SFError::BufferNotFound(buffer_id))
            }
        }

        SFComponents {
            niladic: Box::new(|es| get_end_position(es, es.current_buffer_id)),
            monadic_rank0: Box::new(|es, buffer_id, _| get_end_position(es, buffer_id)),
            monadic_rank1: Box::new(|es, buffer_id, _| get_end_position(es, buffer_id)),
            dyadic: None,
        }
        .into()
    }

    pub fn end_structural() -> Self {
        SFComponents {
            niladic: Box::new(|es| {
                if let Some(buf) = es.current_buffer() {
                    Ok(Selection::Position {
                        buffer_id: es.current_buffer_id,
                        pos: buf.max_pos(),
                    })
                } else {
                    Err(SFError::BufferNotFound(es.current_buffer_id))
                }
            }),
            monadic_rank0: Box::new(|_, buffer_id, pos| Ok(Selection::Position { buffer_id, pos })),
            monadic_rank1: Box::new(|_, buffer_id, int| {
                Ok(Selection::Position {
                    buffer_id,
                    pos: int.end,
                })
            }),
            dyadic: None,
        }
        .into()
    }
}