From f3acb5cc0faf739f09112607bb98b6594184bd8b Mon Sep 17 00:00:00 2001 From: tslil Date: Mon, 24 Aug 2026 20:39:57 +0100 Subject: The big transposition-ing: move from functions as data to data of functions Basically Rust will fight you all the way if you treat it like Haskell, instead work with traits and impls on ZSTs. --- .../selection_functions/result_transformation.rs | 47 ++++++++++++---------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'rprt-engine/src/selection_functions/result_transformation.rs') 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 { + ) -> Result { 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 { + ) -> Result { 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 { + 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 { 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) } -- cgit v1.2.3