working pretty well
This commit is contained in:
parent
1ab2995047
commit
3dfc3c9680
2 changed files with 266 additions and 192 deletions
|
|
@ -9,13 +9,13 @@ use crate::errors::SyncLibError;
|
|||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub enum Operation {
|
||||
Insert {
|
||||
index: u64,
|
||||
index: i64,
|
||||
text: String,
|
||||
},
|
||||
|
||||
Delete {
|
||||
index: u64,
|
||||
deleted_character_count: u64,
|
||||
index: i64,
|
||||
deleted_character_count: i64,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -35,31 +35,60 @@ impl Display for Operation {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for Operation {
|
||||
fn default() -> Self {
|
||||
Operation::Insert {
|
||||
index: 0,
|
||||
text: "".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Operation {
|
||||
pub fn new(tag: ChangeTag, index: u64, text: &str) -> Self {
|
||||
match tag {
|
||||
pub fn create(tag: ChangeTag, index: i64, text: &str) -> Result<Self, SyncLibError> {
|
||||
if index < 0 {
|
||||
return Err(SyncLibError::NegativeOperationIndexError(format!(
|
||||
"Index {} is negative",
|
||||
index
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(match tag {
|
||||
ChangeTag::Insert => Operation::Insert {
|
||||
index,
|
||||
text: text.to_string(),
|
||||
},
|
||||
ChangeTag::Delete => Operation::Delete {
|
||||
index,
|
||||
deleted_character_count: text.chars().count() as u64,
|
||||
deleted_character_count: text.chars().count() as i64,
|
||||
},
|
||||
_ => panic!("Only insertion and deletions are supported"),
|
||||
}
|
||||
_ => {
|
||||
return Err(SyncLibError::OperationConversionError(format!(
|
||||
"Cannot convert editing operation because {:?}",
|
||||
tag
|
||||
)))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn create_insert(index: i64, text: &str) -> Result<Self, SyncLibError> {
|
||||
Self::create(ChangeTag::Insert, index, text)
|
||||
}
|
||||
|
||||
pub fn create_delete(index: i64, length: i64) -> Result<Self, SyncLibError> {
|
||||
if index < 0 {
|
||||
return Err(SyncLibError::NegativeOperationIndexError(format!(
|
||||
"Index {} is negative",
|
||||
index
|
||||
)));
|
||||
}
|
||||
|
||||
if length < 0 {
|
||||
return Err(SyncLibError::NegativeOperationIndexError(format!(
|
||||
"Length {} is negative",
|
||||
length
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(Operation::Delete {
|
||||
index,
|
||||
deleted_character_count: length,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn apply<'a>(&self, rope_text: &'a mut Rope) -> Result<&'a mut Rope, SyncLibError> {
|
||||
let index: usize = self.index() as usize;
|
||||
let index: usize = self.start_index() as usize;
|
||||
match self {
|
||||
Operation::Insert { text, .. } => rope_text.try_insert(index, &text).map_err(|err| {
|
||||
SyncLibError::OperationApplicationError(format!("Failed to insert text: {}", err))
|
||||
|
|
@ -80,14 +109,36 @@ impl Operation {
|
|||
Ok(rope_text)
|
||||
}
|
||||
|
||||
pub fn index(&self) -> u64 {
|
||||
/// Returns the index of the first character that the operation affects.
|
||||
pub fn start_index(&self) -> i64 {
|
||||
match self {
|
||||
Operation::Insert { index, .. } => *index,
|
||||
Operation::Delete { index, .. } => *index,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_index(&self, index: u64) -> Self {
|
||||
/// Returns the index of the last character that the operation affects.
|
||||
pub fn end_index(&self) -> i64 {
|
||||
self.start_index() + self.len() - 1
|
||||
}
|
||||
|
||||
/// Returns the number of affected characters.
|
||||
pub fn len(&self) -> i64 {
|
||||
match self {
|
||||
Operation::Insert { text, .. } => text.chars().count() as i64,
|
||||
Operation::Delete {
|
||||
deleted_character_count,
|
||||
..
|
||||
} => *deleted_character_count,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the range of indices of characters that the operation affects, inclusive.
|
||||
pub fn range(&self) -> std::ops::RangeInclusive<i64> {
|
||||
self.start_index()..=self.end_index()
|
||||
}
|
||||
|
||||
pub fn with_index(&self, index: i64) -> Self {
|
||||
match self {
|
||||
Operation::Insert { text, .. } => Operation::Insert {
|
||||
index,
|
||||
|
|
@ -103,15 +154,15 @@ impl Operation {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn with_shifted_index(&self, offset: i64) -> Result<Self, SyncLibError> {
|
||||
let new_index = self.index().saturating_add_signed(offset);
|
||||
Ok(self.with_index(new_index))
|
||||
pub fn with_shifted_index(&self, offset: i64) -> Self {
|
||||
let new_index = 0.max(self.start_index() + offset);
|
||||
self.with_index(new_index)
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for Operation {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
let result = self.index().cmp(&other.index());
|
||||
let result = self.start_index().cmp(&other.start_index());
|
||||
if result == Ordering::Equal {
|
||||
match (self, other) {
|
||||
(Operation::Insert { .. }, Operation::Delete { .. }) => Ordering::Greater,
|
||||
|
|
@ -134,6 +185,15 @@ impl PartialOrd for Operation {
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_creation_errors() {
|
||||
insta::assert_debug_snapshot!(Operation::create(ChangeTag::Insert, -1, "hi"));
|
||||
insta::assert_debug_snapshot!(Operation::create(ChangeTag::Equal, 0, "hi"));
|
||||
insta::assert_debug_snapshot!(Operation::create_insert(-1, "hi"));
|
||||
insta::assert_debug_snapshot!(Operation::create_delete(0, -1));
|
||||
insta::assert_debug_snapshot!(Operation::create_delete(-1, -1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_delete() -> Result<(), SyncLibError> {
|
||||
let mut rope = Rope::from_str("hello world");
|
||||
|
|
@ -149,6 +209,18 @@ mod tests {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_delete_with_create() -> Result<(), SyncLibError> {
|
||||
let mut rope = Rope::from_str("hello world");
|
||||
let operation = Operation::create(ChangeTag::Delete, 6, "world")?;
|
||||
|
||||
operation.apply(&mut rope)?;
|
||||
|
||||
assert_eq!(rope.to_string(), "hello");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_insert() -> Result<(), SyncLibError> {
|
||||
let mut rope = Rope::from_str("hello");
|
||||
|
|
@ -163,4 +235,16 @@ mod tests {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_insert_with_create() -> Result<(), SyncLibError> {
|
||||
let mut rope = Rope::from_str("hello");
|
||||
let operation = Operation::create(ChangeTag::Insert, 5, " my friend")?;
|
||||
|
||||
operation.apply(&mut rope)?;
|
||||
|
||||
assert_eq!(rope.to_string(), "hello my friend");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue