aboutsummaryrefslogtreecommitdiff
path: root/rprt-engine/src/selection_functions/result_transformation.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rprt-engine/src/selection_functions/result_transformation.rs')
-rw-r--r--rprt-engine/src/selection_functions/result_transformation.rs47
1 files changed, 26 insertions, 21 deletions
diff --git a/rprt-engine/src/selection_functions/result_transformation.rs b/rprt-engine/src/selection_functions/result_transformation.rs
index c150916..b437afd 100644
--- a/rprt-engine/src/selection_functions/result_transformation.rs
+++ b/rprt-engine/src/selection_functions/result_transformation.rs
@@ -1,16 +1,20 @@
use crate::{
buffer::BufferID,
- selection::{Interval, Selection},
- selection_functions::types::{SFArguments, SFError, SFResult},
+ selection::{Interval, Selection, Vectorisable},
+ selection_functions::types::SelectionFunctionError,
state::EditorState,
};
-pub fn complement_transform(es: &EditorState, result: Selection) -> SFResult {
- fn complement_rank0(
+struct Complement;
+
+impl Vectorisable<(), SelectionFunctionError> for Complement {
+ fn rank0(
+ &self,
+ _param: &(),
es: &EditorState,
buffer_id: BufferID,
pos: usize,
- ) -> Result<Selection, SFError> {
+ ) -> Result<Selection, SelectionFunctionError> {
if let Some(buf) = es.get_buffer(buffer_id) {
let mut ranges = Vec::new();
if pos > 0 {
@@ -22,15 +26,17 @@ pub fn complement_transform(es: &EditorState, result: Selection) -> SFResult {
}
Ok(Selection::Vectors { buffer_id, ranges })
} else {
- Err(SFError::BufferNotFound(buffer_id))
+ Err(SelectionFunctionError::BufferNotFound(buffer_id))
}
}
- fn complement_rank1(
+ fn rank1(
+ &self,
+ _param: &(),
es: &EditorState,
buffer_id: BufferID,
interval: &Interval,
- ) -> Result<Selection, SFError> {
+ ) -> Result<Selection, SelectionFunctionError> {
if let Some(buf) = es.get_buffer(buffer_id) {
let max_pos = buf.max_pos();
if interval.start == 0 && interval.end >= max_pos {
@@ -50,25 +56,24 @@ pub fn complement_transform(es: &EditorState, result: Selection) -> SFResult {
}
Ok(Selection::Vectors { buffer_id, ranges })
} else {
- Err(SFError::BufferNotFound(buffer_id))
+ Err(SelectionFunctionError::BufferNotFound(buffer_id))
}
}
+}
- result.vectorise(es, complement_rank0, complement_rank1)
+pub fn complement_transform(
+ es: &EditorState,
+ result: Selection,
+) -> Result<Selection, SelectionFunctionError> {
+ Complement.vectorise(&(), es, &result)
}
-pub fn conditional_transform(mut result: Selection, arg: &SFArguments) -> SFResult {
+pub fn conditional_transform(
+ mut result: Selection,
+ left: Option<&Selection>,
+) -> Result<Selection, SelectionFunctionError> {
if !result.is_empty() {
- Ok(match arg {
- // TODO: think about this niladic case?
- SFArguments::Niladic { .. } => Selection::empty(),
- SFArguments::Monadic { es: _, left } => left.clone(),
- SFArguments::Dyadic {
- es: _,
- left,
- right: _,
- } => left.clone(),
- })
+ Ok(left.cloned().unwrap_or_else(Selection::empty))
} else {
Ok(result)
}