From 40323c33ee06bbe5b45b438ccd102abe533d210a Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Wed, 2 Apr 2025 21:07:33 +0100 Subject: [PATCH] Make JS API usable --- backend/sync_lib/src/cursor.rs | 67 +++++++++++++++++++++------------- backend/sync_lib/src/lib.rs | 8 ++-- backend/sync_lib/tests/web.rs | 34 ++++------------- 3 files changed, 54 insertions(+), 55 deletions(-) diff --git a/backend/sync_lib/src/cursor.rs b/backend/sync_lib/src/cursor.rs index 6b61a078..2f7135eb 100644 --- a/backend/sync_lib/src/cursor.rs +++ b/backend/sync_lib/src/cursor.rs @@ -1,26 +1,29 @@ -use reconcile::{CursorPosition, TextWithCursors}; use wasm_bindgen::prelude::*; /// Wrapper type to expose `TextWithCursors` to JS. #[wasm_bindgen] #[derive(Debug, Clone, PartialEq)] -pub struct OwnedTextWithCursors { +pub struct TextWithCursors { text: String, - cursors: Vec, + cursors: Vec, } -impl OwnedTextWithCursors { - pub fn new(text: impl Into, cursors: Vec) -> Self { - Self { - text: text.into(), - cursors, - } - } +#[wasm_bindgen] +impl TextWithCursors { + #[wasm_bindgen(constructor)] + #[must_use] + pub fn new(text: String, cursors: Vec) -> Self { Self { text, cursors } } + + #[must_use] + pub fn text(&self) -> String { self.text.clone() } + + #[must_use] + pub fn cursors(&self) -> Vec { self.cursors.clone() } } -impl From for TextWithCursors<'_> { - fn from(owned: OwnedTextWithCursors) -> Self { - TextWithCursors::new_owned( +impl From for reconcile::TextWithCursors<'_> { + fn from(owned: TextWithCursors) -> Self { + reconcile::TextWithCursors::new_owned( owned.text.to_string(), owned .cursors @@ -31,9 +34,9 @@ impl From for TextWithCursors<'_> { } } -impl From> for OwnedTextWithCursors { - fn from(text_with_cursors: TextWithCursors<'_>) -> Self { - OwnedTextWithCursors { +impl From> for TextWithCursors { + fn from(text_with_cursors: reconcile::TextWithCursors<'_>) -> Self { + TextWithCursors { text: text_with_cursors.text.into_owned(), cursors: text_with_cursors .cursors @@ -47,23 +50,37 @@ impl From> for OwnedTextWithCursors { /// Wrapper type to expose `CursorPosition` to JS. #[wasm_bindgen] #[derive(Debug, Clone, PartialEq)] -pub struct OwnedCursorPosition { - pub id: usize, - pub char_index: usize, +pub struct CursorPosition { + id: usize, + char_index: usize, } -impl From for CursorPosition { - fn from(owned: OwnedCursorPosition) -> Self { - CursorPosition { +#[wasm_bindgen] +impl CursorPosition { + #[wasm_bindgen(constructor)] + #[must_use] + pub fn new(id: usize, char_index: usize) -> Self { Self { id, char_index } } + + #[must_use] + pub fn id(&self) -> usize { self.id } + + #[wasm_bindgen(js_name = characterPosition)] + #[must_use] + pub fn char_index(&self) -> usize { self.char_index } +} + +impl From for reconcile::CursorPosition { + fn from(owned: CursorPosition) -> Self { + reconcile::CursorPosition { id: owned.id, char_index: owned.char_index, } } } -impl From for OwnedCursorPosition { - fn from(cursor: CursorPosition) -> Self { - OwnedCursorPosition { +impl From for CursorPosition { + fn from(cursor: reconcile::CursorPosition) -> Self { + CursorPosition { id: cursor.id, char_index: cursor.char_index, } diff --git a/backend/sync_lib/src/lib.rs b/backend/sync_lib/src/lib.rs index 6383983e..d2a54cf9 100644 --- a/backend/sync_lib/src/lib.rs +++ b/backend/sync_lib/src/lib.rs @@ -12,7 +12,7 @@ use core::str; use base64::{Engine as _, engine::general_purpose::STANDARD}; -use cursor::OwnedTextWithCursors; +use cursor::TextWithCursors; use errors::SyncLibError; use wasm_bindgen::prelude::*; @@ -110,9 +110,9 @@ pub fn merge_text(parent: &str, left: &str, right: &str) -> String { #[must_use] pub fn merge_text_with_cursors( parent: &str, - left: OwnedTextWithCursors, - right: OwnedTextWithCursors, -) -> OwnedTextWithCursors { + left: TextWithCursors, + right: TextWithCursors, +) -> TextWithCursors { set_panic_hook(); reconcile::reconcile_with_cursors(parent, left.into(), right.into()).into() diff --git a/backend/sync_lib/tests/web.rs b/backend/sync_lib/tests/web.rs index 0ce76b39..cf82aa7e 100644 --- a/backend/sync_lib/tests/web.rs +++ b/backend/sync_lib/tests/web.rs @@ -1,6 +1,6 @@ use insta::assert_debug_snapshot; use sync_lib::{ - cursor::{OwnedCursorPosition, OwnedTextWithCursors}, + cursor::{CursorPosition, TextWithCursors}, *, }; use wasm_bindgen_test::*; @@ -50,36 +50,18 @@ fn test_merge_text() { fn test_merge_text_with_cursors() { let result = merge_text_with_cursors( "hi", - OwnedTextWithCursors::new("hi world", vec![]), - OwnedTextWithCursors::new( - "hi", - vec![ - OwnedCursorPosition { - id: 0, - char_index: 1, - }, - OwnedCursorPosition { - id: 1, - char_index: 2, - }, - ], + TextWithCursors::new("hi world".to_owned(), vec![]), + TextWithCursors::new( + "hi".to_owned(), + vec![CursorPosition::new(0, 1), CursorPosition::new(1, 2)], ), ); assert_eq!( result, - OwnedTextWithCursors::new( - "hi world", - vec![ - OwnedCursorPosition { - id: 0, - char_index: 1, - }, - OwnedCursorPosition { - id: 1, - char_index: 2, - } - ] + TextWithCursors::new( + "hi world".to_owned(), + vec![CursorPosition::new(0, 1), CursorPosition::new(1, 2)] ), ); }