Skip to main content

EditedText

Struct EditedText 

Source
pub struct EditedText<'a, T>
where T: PartialEq + Clone + Debug,
{ /* private fields */ }
Expand description

A text document with a sequence of operations derived from diffing it against an updated version. Supports merging two EditedText instances (from the same original) via Operational Transformation.

Created via from_strings, from_strings_with_tokenizer, or from_diff, then merged with another EditedText and applied to get the reconciled text.

Also tracks cursor positions from the updated text, repositioning them when operations are applied.

Implementations§

Source§

impl<'a> EditedText<'a, String>

Source

pub fn from_strings(original: &'a str, updated: &TextWithCursors) -> Self

Create an EditedText from the given original and updated strings. Uses the default word tokenizer (splits on word boundaries).

Source§

impl<'a, T> EditedText<'a, T>
where T: PartialEq + Clone + Debug,

Source

pub fn from_strings_with_tokenizer( original: &'a str, updated: &TextWithCursors, tokenizer: &Tokenizer<T>, ) -> Self

Create an EditedText from the given original and updated strings using the provided tokenizer

Source

pub fn merge(self, other: Self) -> Self

Merge two EditedText instances. The two instances must be derived from the same original text. The operations are merged using the principles of Operational Transformation. The cursors are updated accordingly to reflect the changes made by the merged operations.

§Panics

Panics if there’s an integer overflow (in isize) when calculating new cursor positions.

Source

pub fn apply(&self) -> TextWithCursors

Apply the operations to the text and return the resulting text

Source

pub fn apply_with_history(&self) -> Vec<SpanWithHistory>

Apply the operations to the text and return the resulting text in chunks together with the provenance describing where each chunk came from.

Returns all spans including deletions (not present in the merged text).

 use reconcile_text::{History, SpanWithHistory, BuiltinTokenizer, reconcile};

 let parent = "Merging text is hard!";
 let left = "Merging text is easy!"; // Changed "hard" to "easy"
 let right = "With reconcile, merging documents is hard!"; // Added prefix and changed word

 let result = reconcile(
     parent,
     &left.into(),
     &right.into(),
     &*BuiltinTokenizer::Word,
 );

 assert_eq!(
     result.apply_with_history(),
     vec![
         SpanWithHistory::new("Merging text".to_string(), History::RemovedFromRight,),
         SpanWithHistory::new(
             "With reconcile, merging documents".to_string(),
             History::AddedFromRight,
         ),
         SpanWithHistory::new(" ".to_string(), History::Unchanged,),
         SpanWithHistory::new("is".to_string(), History::Unchanged,),
         SpanWithHistory::new(" hard!".to_string(), History::RemovedFromLeft,),
         SpanWithHistory::new(" easy!".to_string(), History::AddedFromLeft,),
     ]
 );
Source

pub fn apply_with_all(&self) -> (TextWithCursors, Vec<SpanWithHistory>)

Apply the operations and return both the merged text with cursors and the provenance history in a single pass

Source

pub fn to_diff(&self) -> Result<Vec<NumberOrText>, DiffError>

Convert the EditedText into a terse representation ready for serialization. The result omits cursor positions and the original text. This is useful for sending text diffs over the network if there’s a clear consensus on the original text.

Inserts are strings, deletes are negative integers (character count), and retained spans are positive integers (character count).

§Errors

Returns DiffError::IntegerOverflow if a character count exceeds i64::MAX.

Source

pub fn from_diff( original_text: &'a str, diff: Vec<NumberOrText>, tokenizer: &Tokenizer<T>, ) -> Result<EditedText<'a, T>, DiffError>

Reconstruct an EditedText from a diff and the original text.

§Errors

Returns DiffError::LengthExceedsOriginal if the diff references a range that exceeds the original text length.

§Panics

Panics if there’s an integer overflow in i64.

Trait Implementations§

Source§

impl<'a, T> Clone for EditedText<'a, T>
where T: PartialEq + Clone + Debug + Clone,

Source§

fn clone(&self) -> EditedText<'a, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, T> Debug for EditedText<'a, T>
where T: PartialEq + Clone + Debug + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Default for EditedText<'a, T>
where T: PartialEq + Clone + Debug + Default,

Source§

fn default() -> EditedText<'a, T>

Returns the “default value” for a type. Read more
Source§

impl<'a, T> PartialEq for EditedText<'a, T>
where T: PartialEq + Clone + Debug + PartialEq,

Source§

fn eq(&self, other: &EditedText<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T> StructuralPartialEq for EditedText<'a, T>
where T: PartialEq + Clone + Debug,

Auto Trait Implementations§

§

impl<'a, T> Freeze for EditedText<'a, T>

§

impl<'a, T> RefUnwindSafe for EditedText<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for EditedText<'a, T>
where T: Send,

§

impl<'a, T> Sync for EditedText<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for EditedText<'a, T>
where T: Unpin,

§

impl<'a, T> UnsafeUnpin for EditedText<'a, T>

§

impl<'a, T> UnwindSafe for EditedText<'a, T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.