Remove errors

This commit is contained in:
Andras Schmelczer 2024-11-29 22:26:18 +00:00
commit 35be112de2
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
6 changed files with 28 additions and 56 deletions

View file

@ -5,8 +5,6 @@ edition = "2021"
[dependencies] [dependencies]
ropey = { version = "1.6.1", default-features = false, features = ["simd"] } ropey = { version = "1.6.1", default-features = false, features = ["simd"] }
thiserror = {workspace = true}
log = {workspace = true}
# optional dependencies # optional dependencies
serde = { version = "1.0.215", optional = true } serde = { version = "1.0.215", optional = true }

View file

@ -1,7 +0,0 @@
use thiserror::Error;
#[derive(Error, Debug)]
pub enum SyncLibError {
#[error("Failed to apply operation because {0}")]
OperationApplicationError(String),
}

View file

@ -1,10 +1,8 @@
mod diffs; mod diffs;
mod errors;
mod operation_transformation; mod operation_transformation;
mod tokenizer; mod tokenizer;
mod utils; mod utils;
pub use errors::SyncLibError;
pub use operation_transformation::reconcile; pub use operation_transformation::reconcile;
pub use operation_transformation::reconcile_with_tokenizer; pub use operation_transformation::reconcile_with_tokenizer;
pub use operation_transformation::EditedText; pub use operation_transformation::EditedText;

View file

@ -1,6 +1,5 @@
use super::Operation; use super::Operation;
use crate::diffs::raw_operation::RawOperation; use crate::diffs::raw_operation::RawOperation;
use crate::errors::SyncLibError;
use crate::operation_transformation::merge_context::MergeContext; use crate::operation_transformation::merge_context::MergeContext;
use crate::tokenizer::word_tokenizer::word_tokenizer; use crate::tokenizer::word_tokenizer::word_tokenizer;
use crate::tokenizer::Tokenizer; use crate::tokenizer::Tokenizer;
@ -229,15 +228,15 @@ where
/// # Errors /// # Errors
/// ///
/// Returns an SyncLibError::OperationError if the operations cannot be applied to the text. /// Returns an SyncLibError::OperationError if the operations cannot be applied to the text.
pub fn apply(&self) -> Result<String, SyncLibError> { pub fn apply(&self) -> String {
let mut text = Rope::from_str(self.text); let mut text = Rope::from_str(self.text);
self.operations self.operations
.iter() .iter()
.try_fold( .fold(
&mut text, &mut text,
|rope_text, OrderedOperation { operation, .. }| operation.apply(rope_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); insta::assert_debug_snapshot!(operations);
let new_right = operations.apply().unwrap(); let new_right = operations.apply();
assert_eq!(new_right.to_string(), right); assert_eq!(new_right.to_string(), right);
} }
@ -271,7 +270,7 @@ mod tests {
assert_eq!(operations.operations.len(), 0); assert_eq!(operations.operations.len(), 0);
let new_right = operations.apply().unwrap(); let new_right = operations.apply();
assert_eq!(new_right.to_string(), text); assert_eq!(new_right.to_string(), text);
} }
@ -289,6 +288,6 @@ mod tests {
println!("{:#?}", operations_2); println!("{:#?}", operations_2);
let operations = operations_1.merge(operations_2); let operations = operations_1.merge(operations_2);
assert_eq!(operations.apply().unwrap(), expected); assert_eq!(operations.apply(), expected);
} }
} }

View file

@ -5,9 +5,9 @@ mod operation;
pub use edited_text::EditedText; pub use edited_text::EditedText;
pub use operation::Operation; pub use operation::Operation;
use crate::{errors::SyncLibError, tokenizer::Tokenizer}; use crate::tokenizer::Tokenizer;
pub fn reconcile(original: &str, left: &str, right: &str) -> Result<String, SyncLibError> { pub fn reconcile(original: &str, left: &str, right: &str) -> String {
let left_operations = EditedText::from_strings(original, left); let left_operations = EditedText::from_strings(original, left);
let right_operations = EditedText::from_strings(original, right); let right_operations = EditedText::from_strings(original, right);
@ -20,7 +20,7 @@ pub fn reconcile_with_tokenizer<F, T>(
left: &str, left: &str,
right: &str, right: &str,
tokenizer: &Tokenizer<T>, tokenizer: &Tokenizer<T>,
) -> Result<String, SyncLibError> ) -> String
where where
T: PartialEq + Clone, T: PartialEq + Clone,
{ {
@ -175,11 +175,11 @@ mod test {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
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) { 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_1, edit_2), expected);
assert_eq!(reconcile(original, edit_2, edit_1).unwrap(), expected); assert_eq!(reconcile(original, edit_2, edit_1), expected);
} }
} }

View file

@ -1,5 +1,5 @@
use crate::utils::find_common_overlap::find_common_overlap; use crate::utils::find_common_overlap::find_common_overlap;
use crate::{errors::SyncLibError, Token}; use crate::Token;
use ropey::Rope; use ropey::Rope;
use std::fmt::Debug; use std::fmt::Debug;
use std::fmt::Display; use std::fmt::Display;
@ -86,22 +86,15 @@ where
/// ///
/// When compiled in debug mode, panics if a delete operation is attempted on a range /// 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. /// 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 { match self {
Operation::Insert { text, .. } => rope_text Operation::Insert { text, .. } => rope_text.insert(
.try_insert(
self.start_index(), self.start_index(),
&text &text
.iter() .iter()
.map(|token| token.original()) .map(|token| token.original())
.collect::<String>(), .collect::<String>(),
) ),
.map_err(|err| {
SyncLibError::OperationApplicationError(format!(
"Failed to insert text: {}",
err
))
}),
Operation::Delete { Operation::Delete {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
deleted_text, deleted_text,
@ -120,16 +113,11 @@ where
); );
} }
rope_text.try_remove(self.range()).map_err(|err| { rope_text.remove(self.range());
SyncLibError::OperationApplicationError(format!(
"Failed to remove text: {}",
err
))
})
} }
}?; };
Ok(rope_text) rope_text
} }
/// Returns the index of the first character that the operation affects. /// Returns the index of the first character that the operation affects.
@ -368,26 +356,22 @@ mod tests {
} }
#[test] #[test]
fn test_apply_delete_with_create() -> Result<(), SyncLibError> { fn test_apply_delete_with_create() {
let mut rope = Rope::from_str("hello world"); let mut rope = Rope::from_str("hello world");
let operation = Operation::<()>::create_delete_with_text(5, " world".to_string()).unwrap(); 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"); assert_eq!(rope.to_string(), "hello");
Ok(())
} }
#[test] #[test]
fn test_apply_insert() -> Result<(), SyncLibError> { fn test_apply_insert() {
let mut rope = Rope::from_str("hello"); let mut rope = Rope::from_str("hello");
let operation = Operation::create_insert(5, vec![" my friend".into()]).unwrap(); 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"); assert_eq!(rope.to_string(), "hello my friend");
Ok(())
} }
} }