diff --git a/backend/reconcile/Cargo.toml b/backend/reconcile/Cargo.toml index 9ecadee..5303f95 100644 --- a/backend/reconcile/Cargo.toml +++ b/backend/reconcile/Cargo.toml @@ -5,8 +5,6 @@ edition = "2021" [dependencies] ropey = { version = "1.6.1", default-features = false, features = ["simd"] } -thiserror = {workspace = true} -log = {workspace = true} # optional dependencies serde = { version = "1.0.215", optional = true } diff --git a/backend/reconcile/src/errors.rs b/backend/reconcile/src/errors.rs deleted file mode 100644 index 0a43c8e..0000000 --- a/backend/reconcile/src/errors.rs +++ /dev/null @@ -1,7 +0,0 @@ -use thiserror::Error; - -#[derive(Error, Debug)] -pub enum SyncLibError { - #[error("Failed to apply operation because {0}")] - OperationApplicationError(String), -} diff --git a/backend/reconcile/src/lib.rs b/backend/reconcile/src/lib.rs index 7bad0a1..46398e1 100644 --- a/backend/reconcile/src/lib.rs +++ b/backend/reconcile/src/lib.rs @@ -1,10 +1,8 @@ mod diffs; -mod errors; mod operation_transformation; mod tokenizer; mod utils; -pub use errors::SyncLibError; pub use operation_transformation::reconcile; pub use operation_transformation::reconcile_with_tokenizer; pub use operation_transformation::EditedText; diff --git a/backend/reconcile/src/operation_transformation/edited_text.rs b/backend/reconcile/src/operation_transformation/edited_text.rs index dcc11bc..efd225b 100644 --- a/backend/reconcile/src/operation_transformation/edited_text.rs +++ b/backend/reconcile/src/operation_transformation/edited_text.rs @@ -1,6 +1,5 @@ use super::Operation; use crate::diffs::raw_operation::RawOperation; -use crate::errors::SyncLibError; use crate::operation_transformation::merge_context::MergeContext; use crate::tokenizer::word_tokenizer::word_tokenizer; use crate::tokenizer::Tokenizer; @@ -229,15 +228,15 @@ where /// # Errors /// /// Returns an SyncLibError::OperationError if the operations cannot be applied to the text. - pub fn apply(&self) -> Result { + pub fn apply(&self) -> String { let mut text = Rope::from_str(self.text); self.operations .iter() - .try_fold( + .fold( &mut text, |rope_text, OrderedOperation { operation, .. }| operation.apply(rope_text), ) - .map(|rope| rope.to_string()) + .to_string() } } @@ -258,7 +257,7 @@ mod tests { insta::assert_debug_snapshot!(operations); - let new_right = operations.apply().unwrap(); + let new_right = operations.apply(); assert_eq!(new_right.to_string(), right); } @@ -271,7 +270,7 @@ mod tests { assert_eq!(operations.operations.len(), 0); - let new_right = operations.apply().unwrap(); + let new_right = operations.apply(); assert_eq!(new_right.to_string(), text); } @@ -289,6 +288,6 @@ mod tests { println!("{:#?}", operations_2); let operations = operations_1.merge(operations_2); - assert_eq!(operations.apply().unwrap(), expected); + assert_eq!(operations.apply(), expected); } } diff --git a/backend/reconcile/src/operation_transformation/mod.rs b/backend/reconcile/src/operation_transformation/mod.rs index 234abc8..4805f87 100644 --- a/backend/reconcile/src/operation_transformation/mod.rs +++ b/backend/reconcile/src/operation_transformation/mod.rs @@ -5,9 +5,9 @@ mod operation; pub use edited_text::EditedText; pub use operation::Operation; -use crate::{errors::SyncLibError, tokenizer::Tokenizer}; +use crate::tokenizer::Tokenizer; -pub fn reconcile(original: &str, left: &str, right: &str) -> Result { +pub fn reconcile(original: &str, left: &str, right: &str) -> String { let left_operations = EditedText::from_strings(original, left); let right_operations = EditedText::from_strings(original, right); @@ -20,7 +20,7 @@ pub fn reconcile_with_tokenizer( left: &str, right: &str, tokenizer: &Tokenizer, -) -> Result +) -> String where T: PartialEq + Clone, { @@ -175,11 +175,11 @@ mod test { }) .collect::>(); - reconcile(&contents[0], &contents[1], &contents[2]).unwrap(); + reconcile(&contents[0], &contents[1], &contents[2]); } fn test_merge_both_ways(original: &str, edit_1: &str, edit_2: &str, expected: &str) { - assert_eq!(reconcile(original, edit_1, edit_2).unwrap(), expected); - assert_eq!(reconcile(original, edit_2, edit_1).unwrap(), expected); + assert_eq!(reconcile(original, edit_1, edit_2), expected); + assert_eq!(reconcile(original, edit_2, edit_1), expected); } } diff --git a/backend/reconcile/src/operation_transformation/operation.rs b/backend/reconcile/src/operation_transformation/operation.rs index e669bc9..8e5e5d8 100644 --- a/backend/reconcile/src/operation_transformation/operation.rs +++ b/backend/reconcile/src/operation_transformation/operation.rs @@ -1,5 +1,5 @@ use crate::utils::find_common_overlap::find_common_overlap; -use crate::{errors::SyncLibError, Token}; +use crate::Token; use ropey::Rope; use std::fmt::Debug; use std::fmt::Display; @@ -86,22 +86,15 @@ where /// /// When compiled in debug mode, panics if a delete operation is attempted on a range /// of text that does not match the text to be deleted. - pub fn apply<'a>(&self, rope_text: &'a mut Rope) -> Result<&'a mut Rope, SyncLibError> { + pub fn apply<'a>(&self, rope_text: &'a mut Rope) -> &'a mut Rope { match self { - Operation::Insert { text, .. } => rope_text - .try_insert( - self.start_index(), - &text - .iter() - .map(|token| token.original()) - .collect::(), - ) - .map_err(|err| { - SyncLibError::OperationApplicationError(format!( - "Failed to insert text: {}", - err - )) - }), + Operation::Insert { text, .. } => rope_text.insert( + self.start_index(), + &text + .iter() + .map(|token| token.original()) + .collect::(), + ), Operation::Delete { #[cfg(debug_assertions)] deleted_text, @@ -120,16 +113,11 @@ where ); } - rope_text.try_remove(self.range()).map_err(|err| { - SyncLibError::OperationApplicationError(format!( - "Failed to remove text: {}", - err - )) - }) + rope_text.remove(self.range()); } - }?; + }; - Ok(rope_text) + rope_text } /// Returns the index of the first character that the operation affects. @@ -368,26 +356,22 @@ mod tests { } #[test] - fn test_apply_delete_with_create() -> Result<(), SyncLibError> { + fn test_apply_delete_with_create() { let mut rope = Rope::from_str("hello world"); let operation = Operation::<()>::create_delete_with_text(5, " world".to_string()).unwrap(); - operation.apply(&mut rope)?; + operation.apply(&mut rope); assert_eq!(rope.to_string(), "hello"); - - Ok(()) } #[test] - fn test_apply_insert() -> Result<(), SyncLibError> { + fn test_apply_insert() { let mut rope = Rope::from_str("hello"); let operation = Operation::create_insert(5, vec![" my friend".into()]).unwrap(); - operation.apply(&mut rope)?; + operation.apply(&mut rope); assert_eq!(rope.to_string(), "hello my friend"); - - Ok(()) } }