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

This commit is contained in:
Eric Zhang 2021-06-01 17:44:33 -05:00
parent 67cec1ec4b
commit 85da408aae
12 changed files with 58 additions and 35 deletions

36
Cargo.lock generated
View File

@ -598,6 +598,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57b31bc8699a7bd5e43f63cfa9a1e48e2330cdf27f533370d5640fef63ab6910"
dependencies = [
"bytecount",
"serde",
]
[[package]]
@ -832,7 +833,24 @@ dependencies = [
]
[[package]]
name = "rustpad-core"
name = "rustpad-server"
version = "0.1.0"
dependencies = [
"dotenv",
"futures",
"log",
"operational-transform",
"parking_lot",
"pretty_env_logger",
"serde",
"serde_json",
"tokio",
"tokio-stream",
"warp",
]
[[package]]
name = "rustpad-wasm"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
@ -844,22 +862,6 @@ dependencies = [
"wasm-bindgen-test",
]
[[package]]
name = "rustpad-server"
version = "0.1.0"
dependencies = [
"dotenv",
"futures",
"log",
"parking_lot",
"pretty_env_logger",
"serde",
"serde_json",
"tokio",
"tokio-stream",
"warp",
]
[[package]]
name = "ryu"
version = "1.0.5"

View File

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

View File

@ -9,12 +9,12 @@ WORKDIR /home/rust/src
RUN apk --no-cache add curl musl-dev
RUN curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
COPY . .
RUN wasm-pack build rustpad-core
RUN wasm-pack build rustpad-wasm
FROM node:alpine as frontend
WORKDIR /usr/src/app
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
COPY . .
RUN npm run build

View File

@ -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:
```
wasm-pack build rustpad-core
wasm-pack build rustpad-wasm
```
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
```
wasm-pack test rustpad-core --chrome --headless
wasm-pack test rustpad-wasm --chrome --headless
```
## Deployment

12
package-lock.json generated
View File

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

View File

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

View File

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

View File

@ -1,5 +1,5 @@
[package]
name = "rustpad-core"
name = "rustpad-wasm"
version = "0.1.0"
authors = ["Eric Zhang <ekzhang1@gmail.com>"]
edition = "2018"
@ -12,7 +12,7 @@ default = ["console_error_panic_hook"]
[dependencies]
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_json = "1.0.64"
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }

View File

@ -3,6 +3,7 @@
#![warn(missing_docs)]
use operational_transform::OperationSeq;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
pub mod utils;
@ -10,7 +11,7 @@ pub mod utils;
/// This is an wrapper around `operational_transform::OperationSeq`, which is
/// necessary for Wasm compatibility through `wasm-bindgen`.
#[wasm_bindgen]
#[derive(Default, Clone, Debug, PartialEq)]
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct OpSeq(OperationSeq);
/// 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)]
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]
impl OpSeq {
/// Creates a default empty `OpSeq`.

View File

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

View File

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