Przeglądaj źródła

Rename `rustpad-core` to `rustpad-wasm`, add Serde

Eric Zhang 4 lat temu
rodzic
commit
85da408aae

+ 15 - 13
Cargo.lock

@@ -598,6 +598,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "57b31bc8699a7bd5e43f63cfa9a1e48e2330cdf27f533370d5640fef63ab6910"
 checksum = "57b31bc8699a7bd5e43f63cfa9a1e48e2330cdf27f533370d5640fef63ab6910"
 dependencies = [
 dependencies = [
  "bytecount",
  "bytecount",
+ "serde",
 ]
 ]
 
 
 [[package]]
 [[package]]
@@ -831,19 +832,6 @@ dependencies = [
  "winapi",
  "winapi",
 ]
 ]
 
 
-[[package]]
-name = "rustpad-core"
-version = "0.1.0"
-dependencies = [
- "console_error_panic_hook",
- "js-sys",
- "operational-transform",
- "serde",
- "serde_json",
- "wasm-bindgen",
- "wasm-bindgen-test",
-]
-
 [[package]]
 [[package]]
 name = "rustpad-server"
 name = "rustpad-server"
 version = "0.1.0"
 version = "0.1.0"
@@ -851,6 +839,7 @@ dependencies = [
  "dotenv",
  "dotenv",
  "futures",
  "futures",
  "log",
  "log",
+ "operational-transform",
  "parking_lot",
  "parking_lot",
  "pretty_env_logger",
  "pretty_env_logger",
  "serde",
  "serde",
@@ -860,6 +849,19 @@ dependencies = [
  "warp",
  "warp",
 ]
 ]
 
 
+[[package]]
+name = "rustpad-wasm"
+version = "0.1.0"
+dependencies = [
+ "console_error_panic_hook",
+ "js-sys",
+ "operational-transform",
+ "serde",
+ "serde_json",
+ "wasm-bindgen",
+ "wasm-bindgen-test",
+]
+
 [[package]]
 [[package]]
 name = "ryu"
 name = "ryu"
 version = "1.0.5"
 version = "1.0.5"

+ 1 - 1
Cargo.toml

@@ -2,5 +2,5 @@
 
 
 members = [
 members = [
     "rustpad-server",
     "rustpad-server",
-    "rustpad-core",
+    "rustpad-wasm",
 ]
 ]

+ 2 - 2
Dockerfile

@@ -9,12 +9,12 @@ WORKDIR /home/rust/src
 RUN apk --no-cache add curl musl-dev
 RUN apk --no-cache add curl musl-dev
 RUN curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
 RUN curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
 COPY . .
 COPY . .
-RUN wasm-pack build rustpad-core
+RUN wasm-pack build rustpad-wasm
 
 
 FROM node:alpine as frontend
 FROM node:alpine as frontend
 WORKDIR /usr/src/app
 WORKDIR /usr/src/app
 COPY package.json package-lock.json ./
 COPY package.json package-lock.json ./
-COPY --from=wasm /home/rust/src/rustpad-core/pkg rustpad-core/pkg
+COPY --from=wasm /home/rust/src/rustpad-wasm/pkg rustpad-wasm/pkg
 RUN npm ci
 RUN npm ci
 COPY . .
 COPY . .
 RUN npm run build
 RUN npm run build

+ 2 - 2
README.md

@@ -20,7 +20,7 @@ To run this application, you need to install Rust, `wasm-pack`, and Node.js.
 Then, build the WebAssembly portion of the app:
 Then, build the WebAssembly portion of the app:
 
 
 ```
 ```
-wasm-pack build rustpad-core
+wasm-pack build rustpad-wasm
 ```
 ```
 
 
 When that is complete, you can install dependencies for the frontend React
 When that is complete, you can install dependencies for the frontend React
@@ -53,7 +53,7 @@ To run unit tests and integration tests for the server, use the standard
 headless browser with
 headless browser with
 
 
 ```
 ```
-wasm-pack test rustpad-core --chrome --headless
+wasm-pack test rustpad-wasm --chrome --headless
 ```
 ```
 
 
 ## Deployment
 ## Deployment

+ 6 - 6
package-lock.json

@@ -10,7 +10,7 @@
         "react": "^17.0.2",
         "react": "^17.0.2",
         "react-dom": "^17.0.2",
         "react-dom": "^17.0.2",
         "react-scripts": "4.0.3",
         "react-scripts": "4.0.3",
-        "rustpad-core": "file:./rustpad-core/pkg"
+        "rustpad-wasm": "file:./rustpad-wasm/pkg"
       },
       },
       "devDependencies": {
       "devDependencies": {
         "@types/react": "^17.0.8",
         "@types/react": "^17.0.8",
@@ -16643,8 +16643,8 @@
         "aproba": "^1.1.1"
         "aproba": "^1.1.1"
       }
       }
     },
     },
-    "node_modules/rustpad-core": {
-      "resolved": "rustpad-core/pkg",
+    "node_modules/rustpad-wasm": {
+      "resolved": "rustpad-wasm/pkg",
       "link": true
       "link": true
     },
     },
     "node_modules/safe-buffer": {
     "node_modules/safe-buffer": {
@@ -21213,7 +21213,7 @@
         "url": "https://github.com/sponsors/sindresorhus"
         "url": "https://github.com/sponsors/sindresorhus"
       }
       }
     },
     },
-    "rustpad-core/pkg": {
+    "rustpad-wasm/pkg": {
       "name": "rustpad-core",
       "name": "rustpad-core",
       "version": "0.1.0"
       "version": "0.1.0"
     }
     }
@@ -34141,8 +34141,8 @@
         "aproba": "^1.1.1"
         "aproba": "^1.1.1"
       }
       }
     },
     },
-    "rustpad-core": {
-      "version": "file:rustpad-core/pkg"
+    "rustpad-wasm": {
+      "version": "file:rustpad-wasm/pkg"
     },
     },
     "safe-buffer": {
     "safe-buffer": {
       "version": "5.1.2",
       "version": "5.1.2",

+ 1 - 1
package.json

@@ -13,7 +13,7 @@
     "react": "^17.0.2",
     "react": "^17.0.2",
     "react-dom": "^17.0.2",
     "react-dom": "^17.0.2",
     "react-scripts": "4.0.3",
     "react-scripts": "4.0.3",
-    "rustpad-core": "file:./rustpad-core/pkg"
+    "rustpad-wasm": "file:./rustpad-wasm/pkg"
   },
   },
   "devDependencies": {
   "devDependencies": {
     "@types/react": "^17.0.8",
     "@types/react": "^17.0.8",

+ 2 - 1
rustpad-server/Cargo.toml

@@ -8,10 +8,11 @@ edition = "2018"
 dotenv = "0.15.0"
 dotenv = "0.15.0"
 futures = "0.3.15"
 futures = "0.3.15"
 log = "0.4.14"
 log = "0.4.14"
+operational-transform = { version = "0.6.0", features = ["serde"] }
+parking_lot = "0.11.1"
 pretty_env_logger = "0.4.0"
 pretty_env_logger = "0.4.0"
 serde = { version = "1.0.126", features = ["derive"] }
 serde = { version = "1.0.126", features = ["derive"] }
 serde_json = "1.0.64"
 serde_json = "1.0.64"
 tokio = { version = "1.6.1", features = ["full"] }
 tokio = { version = "1.6.1", features = ["full"] }
 tokio-stream = "0.1.6"
 tokio-stream = "0.1.6"
 warp = "0.3.1"
 warp = "0.3.1"
-parking_lot = "0.11.1"

+ 2 - 2
rustpad-core/Cargo.toml → rustpad-wasm/Cargo.toml

@@ -1,5 +1,5 @@
 [package]
 [package]
-name = "rustpad-core"
+name = "rustpad-wasm"
 version = "0.1.0"
 version = "0.1.0"
 authors = ["Eric Zhang <ekzhang1@gmail.com>"]
 authors = ["Eric Zhang <ekzhang1@gmail.com>"]
 edition = "2018"
 edition = "2018"
@@ -12,7 +12,7 @@ default = ["console_error_panic_hook"]
 
 
 [dependencies]
 [dependencies]
 console_error_panic_hook = { version = "0.1", optional = true }
 console_error_panic_hook = { version = "0.1", optional = true }
-operational-transform = "0.6.0"
+operational-transform = { version = "0.6.0", features = ["serde"] }
 serde = { version = "1.0.126", features = ["derive"] }
 serde = { version = "1.0.126", features = ["derive"] }
 serde_json = "1.0.64"
 serde_json = "1.0.64"
 wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
 wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }

+ 21 - 1
rustpad-core/src/lib.rs → rustpad-wasm/src/lib.rs

@@ -3,6 +3,7 @@
 #![warn(missing_docs)]
 #![warn(missing_docs)]
 
 
 use operational_transform::OperationSeq;
 use operational_transform::OperationSeq;
+use serde::{Deserialize, Serialize};
 use wasm_bindgen::prelude::*;
 use wasm_bindgen::prelude::*;
 
 
 pub mod utils;
 pub mod utils;
@@ -10,7 +11,7 @@ pub mod utils;
 /// This is an wrapper around `operational_transform::OperationSeq`, which is
 /// This is an wrapper around `operational_transform::OperationSeq`, which is
 /// necessary for Wasm compatibility through `wasm-bindgen`.
 /// necessary for Wasm compatibility through `wasm-bindgen`.
 #[wasm_bindgen]
 #[wasm_bindgen]
-#[derive(Default, Clone, Debug, PartialEq)]
+#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
 pub struct OpSeq(OperationSeq);
 pub struct OpSeq(OperationSeq);
 
 
 /// This is a pair of `OpSeq` structs, which is needed to handle some return
 /// This is a pair of `OpSeq` structs, which is needed to handle some return
@@ -19,6 +20,25 @@ pub struct OpSeq(OperationSeq);
 #[derive(Default, Clone, Debug, PartialEq)]
 #[derive(Default, Clone, Debug, PartialEq)]
 pub struct OpSeqPair(OpSeq, OpSeq);
 pub struct OpSeqPair(OpSeq, OpSeq);
 
 
+impl OpSeq {
+    /// Transforms two operations A and B that happened concurrently and produces
+    /// two operations A' and B' (in an array) such that
+    ///     `apply(apply(S, A), B') = apply(apply(S, B), A')`.
+    /// This function is the heart of OT.
+    ///
+    /// Unlike `OpSeq::transform`, this function returns a raw tuple, which is
+    /// more efficient but cannot be exported by `wasm-bindgen`.
+    ///
+    /// # Error
+    ///
+    /// Returns `None` if the operations cannot be transformed due to
+    /// length conflicts.
+    pub fn transform_raw(&self, other: &OpSeq) -> Option<(OpSeq, OpSeq)> {
+        let (a, b) = self.0.transform(&other.0).ok()?;
+        Some((Self(a), Self(b)))
+    }
+}
+
 #[wasm_bindgen]
 #[wasm_bindgen]
 impl OpSeq {
 impl OpSeq {
     /// Creates a default empty `OpSeq`.
     /// Creates a default empty `OpSeq`.

+ 0 - 0
rustpad-core/src/utils.rs → rustpad-wasm/src/utils.rs


+ 1 - 1
rustpad-core/tests/web.rs → rustpad-wasm/tests/web.rs

@@ -2,7 +2,7 @@
 
 
 #![cfg(target_arch = "wasm32")]
 #![cfg(target_arch = "wasm32")]
 
 
-use rustpad_core::OpSeq;
+use rustpad_wasm::OpSeq;
 
 
 use wasm_bindgen_test::*;
 use wasm_bindgen_test::*;
 
 

+ 1 - 1
src/App.tsx

@@ -1,5 +1,5 @@
 import { useEffect, useState } from "react";
 import { useEffect, useState } from "react";
-import { set_panic_hook } from "rustpad-core";
+import { set_panic_hook } from "rustpad-wasm";
 
 
 function App() {
 function App() {
   const [input, setInput] = useState("");
   const [input, setInput] = useState("");