Improve public API & add docs #4

Merged
schmelczer merged 37 commits from asch/better-api into main 2025-07-06 13:07:51 +01:00
8 changed files with 57 additions and 32 deletions
Showing only changes of commit b18a692d46 - Show all commits

Extract types into a separate module

Andras Schmelczer 2025-06-29 16:22:32 +01:00

View file

@ -10,7 +10,8 @@ use crate::{
},
raw_operation::RawOperation,
tokenizer::{Tokenizer, word_tokenizer::word_tokenizer},
utils::{history::History, side::Side, string_builder::StringBuilder},
types::{history::History, side::Side, text_with_history::TextWithHistory},
utils::string_builder::StringBuilder,
};
/// A text document and a sequence of operations that can be applied to the text
@ -228,7 +229,7 @@ where
}
#[must_use]
pub fn apply_with_history(&self) -> Vec<(History, String)> {
pub fn apply_with_history(&self) -> Vec<TextWithHistory> {
let mut builder: StringBuilder<'_> = StringBuilder::new(self.text);
let mut history = Vec::with_capacity(self.operations.len());
@ -237,10 +238,17 @@ where
builder = operation.apply(builder);
match operation {
Operation::Equal { .. } => history.push((History::Unchanged, builder.take())),
Operation::Equal { .. } => {
history.push(TextWithHistory::new(History::Unchanged, builder.take()))
}
Operation::Insert { side, .. } => match side {
Side::Left => history.push((History::AddedFromLeft, builder.take())),
Side::Right => history.push((History::AddedFromRight, builder.take())),
Side::Left => {
history.push(TextWithHistory::new(History::AddedFromLeft, builder.take()))
}
Side::Right => history.push(TextWithHistory::new(
History::AddedFromRight,
builder.take(),
)),
},
Operation::Delete {
deleted_character_count,
@ -250,8 +258,12 @@ where
} => {
let deleted = self.text[*order..*order + *deleted_character_count].to_string();
match side {
Side::Left => history.push((History::RemovedFromLeft, deleted)),
Side::Right => history.push((History::RemovedFromRight, deleted)),
Side::Left => {
history.push(TextWithHistory::new(History::RemovedFromLeft, deleted))
}
Side::Right => {
history.push(TextWithHistory::new(History::RemovedFromRight, deleted))
}
}
}
}

View file

@ -4,9 +4,9 @@ use core::fmt::{Debug, Display};
use serde::{Deserialize, Serialize};
use crate::{
Token,
Side, Token,
utils::{
find_longest_prefix_contained_within::find_longest_prefix_contained_within, side::Side,
find_longest_prefix_contained_within::find_longest_prefix_contained_within,
string_builder::StringBuilder,
},
};

3
src/types.rs Normal file
View file

@ -0,0 +1,3 @@
pub mod history;
pub mod side;
pub mod text_with_history;

29
src/types/history.rs Normal file
View file

@ -0,0 +1,29 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg(feature = "wasm")]
pub enum History {
Unchanged = "Unchanged",
AddedFromLeft = "AddedFromLeft",
AddedFromRight = "AddedFromRight",
RemovedFromLeft = "RemovedFromLeft",
RemovedFromRight = "RemovedFromRight",
}
/// Simple enum for describing the result of `reconcile` in a flat list.
/// When compiled to WASM, the enum values are the same as their names.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg(not(feature = "wasm"))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum History {
Unchanged,
AddedFromLeft,
AddedFromRight,
RemovedFromLeft,
RemovedFromRight,
}

22
src/types/side.rs Normal file
View file

@ -0,0 +1,22 @@
use std::fmt::Display;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// Pretty-printable flag to tell which conflicting edit (side)
/// an operation is associated with.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Side {
Left,
Right,
}
impl Display for Side {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Side::Left => write!(f, "Left"),
Side::Right => write!(f, "Right"),
}
}
}

View file

@ -0,0 +1,26 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
use crate::types::history::History;
/// Wrapper type to expose `(History, String)` to JS.
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct TextWithHistory {
history: History,
text: String,
}
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl TextWithHistory {
pub fn new(history: History, text: String) -> Self { TextWithHistory { history, text } }
#[must_use]
pub fn history(&self) -> History { self.history }
#[must_use]
pub fn text(&self) -> String { self.text.clone() }
}

View file

@ -1,7 +1,5 @@
pub mod common_prefix_len;
pub mod common_suffix_len;
pub mod find_longest_prefix_contained_within;
pub mod history;
pub mod myers_diff;
pub mod side;
pub mod string_builder;

View file

@ -1,3 +1,6 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
use crate::History;
@ -88,24 +91,3 @@ impl From<crate::CursorPosition> for JsCursorPosition {
}
}
}
/// Wrapper type to expose `(History, String)` to JS.
#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq)]
pub struct JsTextWithHistory {
history: History,
text: String,
}
impl From<(History, String)> for JsTextWithHistory {
fn from((history, text): (History, String)) -> Self { JsTextWithHistory { history, text } }
}
#[wasm_bindgen]
impl JsTextWithHistory {
#[must_use]
pub fn history(&self) -> History { self.history }
#[must_use]
pub fn text(&self) -> String { self.text.clone() }
}