43 lines
1.3 KiB
Rust
43 lines
1.3 KiB
Rust
#[cfg(feature = "serde")]
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{operation_transformation::Operation, Token};
|
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct OrderedOperation<T>
|
|
where
|
|
T: PartialEq + Clone + std::fmt::Debug,
|
|
{
|
|
pub order: usize,
|
|
pub operation: Operation<T>,
|
|
}
|
|
|
|
impl<T> OrderedOperation<T>
|
|
where
|
|
T: PartialEq + Clone + std::fmt::Debug,
|
|
{
|
|
pub fn get_sort_key(&self, insertion_index: usize) -> (usize, usize, usize, String) {
|
|
(
|
|
self.order,
|
|
match &self.operation {
|
|
Operation::Delete { .. } => 1,
|
|
Operation::Insert { .. } => 2,
|
|
Operation::Equal { .. } => 3,
|
|
},
|
|
insertion_index,
|
|
// Make sure that the ordering is deterministic regardless of which text
|
|
// is left or right.
|
|
match &self.operation {
|
|
Operation::Equal { length, .. } => length.to_string(),
|
|
Operation::Insert { text, .. } => {
|
|
text.iter().map(Token::original).collect::<String>()
|
|
}
|
|
Operation::Delete {
|
|
deleted_character_count,
|
|
..
|
|
} => deleted_character_count.to_string(),
|
|
},
|
|
)
|
|
}
|
|
}
|