diff --git a/src/operation_transformation/edited_text.rs b/src/operation_transformation/edited_text.rs index 30643b1..4b2bdc2 100644 --- a/src/operation_transformation/edited_text.rs +++ b/src/operation_transformation/edited_text.rs @@ -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 { 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)) + } } } } diff --git a/src/operation_transformation/operation.rs b/src/operation_transformation/operation.rs index 05aad3f..ef0a674 100644 --- a/src/operation_transformation/operation.rs +++ b/src/operation_transformation/operation.rs @@ -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, }, }; diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..5dde9b3 --- /dev/null +++ b/src/types.rs @@ -0,0 +1,3 @@ +pub mod history; +pub mod side; +pub mod text_with_history; diff --git a/src/utils/history.rs b/src/types/history.rs similarity index 79% rename from src/utils/history.rs rename to src/types/history.rs index cfba9c8..e30ae90 100644 --- a/src/utils/history.rs +++ b/src/types/history.rs @@ -1,7 +1,10 @@ +#[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 { @@ -16,6 +19,7 @@ pub enum History { /// 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, diff --git a/src/utils/side.rs b/src/types/side.rs similarity index 100% rename from src/utils/side.rs rename to src/types/side.rs diff --git a/src/types/text_with_history.rs b/src/types/text_with_history.rs new file mode 100644 index 0000000..06aff78 --- /dev/null +++ b/src/types/text_with_history.rs @@ -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() } +} diff --git a/src/utils.rs b/src/utils.rs index 58d55b8..2e05a70 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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; diff --git a/src/wasm/types.rs b/src/wasm/types.rs index d2aa486..f18d224 100644 --- a/src/wasm/types.rs +++ b/src/wasm/types.rs @@ -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 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() } -}