Improve compact diff API #24

Merged
schmelczer merged 11 commits from asch/refactor-api into main 2025-11-16 15:43:19 +00:00
4 changed files with 23 additions and 10 deletions
Showing only changes of commit b0ad819ac6 - Show all commits

Fix lint & tests

Andras Schmelczer 2025-11-16 13:06:20 +00:00

View file

@ -371,7 +371,9 @@ where
Operation::Insert { text, .. } => { Operation::Insert { text, .. } => {
if let Some(prev_length) = previous_equal { if let Some(prev_length) = previous_equal {
result.push(NumberOrString::Number(prev_length as i64)); result.push(NumberOrString::Number(
i64::try_from(prev_length).unwrap_or(i64::MAX),
));
previous_equal = None; previous_equal = None;
} }
@ -387,17 +389,22 @@ where
.. ..
} => { } => {
if let Some(prev_length) = previous_equal { if let Some(prev_length) = previous_equal {
result.push(NumberOrString::Number(prev_length as i64)); result.push(NumberOrString::Number(
i64::try_from(prev_length).unwrap_or(i64::MAX),
));
previous_equal = None; previous_equal = None;
} }
result.push(NumberOrString::Number(-(*deleted_character_count as i64))); let count = i64::try_from(*deleted_character_count).unwrap_or(i64::MAX);
result.push(NumberOrString::Number(-count));
} }
} }
} }
if let Some(prev_length) = previous_equal { if let Some(prev_length) = previous_equal {
result.push(NumberOrString::Number(prev_length as i64)); result.push(NumberOrString::Number(
i64::try_from(prev_length).unwrap_or(i64::MAX),
));
} }
result result
@ -417,7 +424,7 @@ where
match simple_operation { match simple_operation {
NumberOrString::Number(length) => { NumberOrString::Number(length) => {
if length >= 0 { if length >= 0 {
let length = length as usize; let length = usize::try_from(length).unwrap_or(usize::MAX);
let original_characters: String = let original_characters: String =
original_text.chars().skip(order).take(length).collect(); original_text.chars().skip(order).take(length).collect();
@ -428,7 +435,7 @@ where
order += token.get_original_length(); order += token.get_original_length();
} }
} else { } else {
let length = -length as usize; let length = usize::try_from(-length).unwrap_or(usize::MAX);
operations.push(Operation::create_delete(order, length)); operations.push(Operation::create_delete(order, length));
order += length; order += length;
} }

View file

@ -125,7 +125,7 @@ impl TextWithCursorsAndHistory {
/// Returns the UTF8 parsed string if it's a text, or `None` if it's likely /// Returns the UTF8 parsed string if it's a text, or `None` if it's likely
/// binary. /// binary.
#[must_use] #[must_use]
pub fn string_or_nothing(data: &[u8]) -> Option<String> { fn string_or_nothing(data: &[u8]) -> Option<String> {
if data.contains(&0) { if data.contains(&0) {
// Even though the NUL character is valid in UTF-8, it's highly suspicious in // Even though the NUL character is valid in UTF-8, it's highly suspicious in
// human-readable text. // human-readable text.

View file

@ -3,7 +3,7 @@ mod example_document;
use std::{fs, path::Path}; use std::{fs, path::Path};
use example_document::ExampleDocument; use example_document::ExampleDocument;
use reconcile_text::{BuiltinTokenizer, EditedText, reconcile}; use reconcile_text::{BuiltinTokenizer, reconcile};
use serde::Deserialize; use serde::Deserialize;
#[test] #[test]
@ -37,6 +37,8 @@ fn test_document_one_way_with_cursors() {
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
#[test] #[test]
fn test_document_one_way_with_serialisation() { fn test_document_one_way_with_serialisation() {
use reconcile_text::EditedText;
for doc in &get_all_documents() { for doc in &get_all_documents() {
let parent = doc.parent(); let parent = doc.parent();
let left_operations = let left_operations =

View file

@ -59,8 +59,12 @@ fn test_merge_binary() {
fn test_get_compact_diff() { fn test_get_compact_diff() {
let parent = "hello "; let parent = "hello ";
let changed = "world"; let changed = "world";
let result = get_compact_diff(parent, &changed.into(), BuiltinTokenizer::Word); let result = get_compact_diff(parent, &changed.into(), BuiltinTokenizer::Word);
assert_eq!(result.len(), 2); assert_eq!(result.len(), 2);
assert_eq!(result[0].as_f64().unwrap(), -6.0); let first: i64 = result[0].clone().try_into().unwrap();
assert_eq!(result[1].as_string().unwrap(), "world"); let second: String = result[1].clone().try_into().unwrap();
assert_eq!(first, -6);
assert_eq!(second, "world");
} }