Use serde to serialize responses (#96)

This commit is contained in:
TrueDoctor 2021-05-02 22:46:47 +02:00 committed by Keavon Chambers
parent f10a3c63d1
commit f8ebff033d
13 changed files with 99 additions and 45 deletions

50
Cargo.lock generated
View File

@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]] [[package]]
name = "arrayvec" name = "arrayvec"
version = "0.5.2" version = "0.5.2"
@ -50,6 +52,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"kurbo", "kurbo",
"log", "log",
"serde",
] ]
[[package]] [[package]]
@ -60,6 +63,7 @@ dependencies = [
"graphite-document-core", "graphite-document-core",
"graphite-proc-macros", "graphite-proc-macros",
"log", "log",
"serde",
"thiserror", "thiserror",
] ]
@ -84,10 +88,17 @@ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"graphite-editor-core", "graphite-editor-core",
"log", "log",
"serde",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-test", "wasm-bindgen-test",
] ]
[[package]]
name = "itoa"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.50" version = "0.3.50"
@ -139,12 +150,49 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]] [[package]]
name = "scoped-tls" name = "scoped-tls"
version = "1.0.0" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"
[[package]]
name = "serde"
version = "1.0.125"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.125"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.68" version = "1.0.68"
@ -189,6 +237,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83240549659d187488f91f33c0f8547cbfef0b2088bc470c116d1d260ef623d9" checksum = "83240549659d187488f91f33c0f8547cbfef0b2088bc470c116d1d260ef623d9"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"serde",
"serde_json",
"wasm-bindgen-macro", "wasm-bindgen-macro",
] ]

View File

@ -328,12 +328,13 @@ export default defineComponent({
}, },
}, },
mounted() { mounted() {
registerResponseHandler(ResponseType["Tool::UpdateCanvas"], (responseData) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any
this.viewportSvg = responseData; registerResponseHandler(ResponseType["Tool::UpdateCanvas"], (responseData: any) => {
this.viewportSvg = responseData.Tool.UpdateCanvas.document;
}); });
registerResponseHandler(ResponseType["Tool::SetActiveTool"], (responseData) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any
const [activeTool] = responseData; registerResponseHandler(ResponseType["Tool::SetActiveTool"], (responseData: any) => {
this.activeTool = activeTool; this.activeTool = responseData.Tool.SetActiveTool.tool_name;
}); });
window.addEventListener("keyup", (e: KeyboardEvent) => this.keyUp(e)); window.addEventListener("keyup", (e: KeyboardEvent) => this.keyUp(e));

View File

@ -23,7 +23,8 @@ export function registerResponseHandler(responseType: ResponseType, callback: Re
window.responseMap[responseType] = callback; window.responseMap[responseType] = callback;
} }
export function handleResponse(responseType: ResponseType, responseData: string) { // eslint-disable-next-line @typescript-eslint/no-explicit-any
export function handleResponse(responseType: ResponseType, responseData: any) {
const callback = window.responseMap[responseType]; const callback = window.responseMap[responseType];
if (callback) { if (callback) {

View File

@ -18,8 +18,9 @@ default = ["console_error_panic_hook"]
[dependencies] [dependencies]
console_error_panic_hook = { version = "0.1.6", optional = true } console_error_panic_hook = { version = "0.1.6", optional = true }
editor-core = { path = "../../../core/editor", package = "graphite-editor-core" } editor-core = { path = "../../../core/editor", package = "graphite-editor-core" }
wasm-bindgen = "0.2.73"
log = "0.4" log = "0.4"
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = { version = "0.2.73", features = ["serde-serialize"] }
[dev-dependencies] [dev-dependencies]
wasm-bindgen-test = "0.3.22" wasm-bindgen-test = "0.3.22"

View File

@ -4,13 +4,11 @@ pub mod utils;
pub mod window; pub mod window;
pub mod wrappers; pub mod wrappers;
use editor_core::{ use editor_core::{events::Response, Editor};
events::{DocumentResponse, Response, ToolResponse},
Editor, LayerId,
};
use std::cell::RefCell; use std::cell::RefCell;
use utils::WasmLog; use utils::WasmLog;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use wrappers::WasmResponse;
// the thread_local macro provides a way to initialize static variables with non-constant functions // the thread_local macro provides a way to initialize static variables with non-constant functions
thread_local! { pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new(Box::new(handle_response))) } thread_local! { pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new(Box::new(handle_response))) }
@ -23,37 +21,19 @@ pub fn init() {
log::set_max_level(log::LevelFilter::Debug); log::set_max_level(log::LevelFilter::Debug);
} }
fn path_to_string(path: Vec<LayerId>) -> String {
path.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(",")
}
fn handle_response(response: Response) { fn handle_response(response: Response) {
let response_type = response.to_string(); let response_type = response.to_string();
match response { send_response(response_type, response);
Response::Document(doc) => match doc {
DocumentResponse::ExpandFolder { path, children } => {
let children = children
.iter()
.map(|c| format!("name:{},visible:{},type:{}", c.name, c.visible, c.layer_type))
.collect::<Vec<String>>()
.join(";");
send_response(response_type, &[path_to_string(path), children])
}
DocumentResponse::CollapseFolder { path } => send_response(response_type, &[path_to_string(path)]),
DocumentResponse::DocumentChanged => log::error!("Wasm wrapper got request to update the document"),
},
Response::Tool(ToolResponse::UpdateCanvas { document }) => send_response(response_type, &[document]),
Response::Tool(ToolResponse::SetActiveTool { tool_name }) => send_response(response_type, &[tool_name]),
}
} }
fn send_response(response_type: String, response_data: &[String]) {
let data = response_data.iter().map(JsValue::from).collect(); fn send_response(response_type: String, response_data: Response) {
handleResponse(response_type, data); let response_data = JsValue::from_serde(&WasmResponse::new(response_data)).expect("Failed to serialize response");
handleResponse(response_type, response_data);
} }
#[wasm_bindgen(module = "/../src/response-handler.ts")] #[wasm_bindgen(module = "/../src/response-handler.ts")]
extern "C" { extern "C" {
fn handleResponse(responseType: String, responseData: Vec<JsValue>); fn handleResponse(responseType: String, responseData: JsValue);
} }
#[wasm_bindgen] #[wasm_bindgen]

View File

@ -2,6 +2,8 @@ use crate::shims::Error;
use editor_core::events; use editor_core::events;
use editor_core::tools::{SelectAppendMode, ToolType}; use editor_core::tools::{SelectAppendMode, ToolType};
use editor_core::Color as InnerColor; use editor_core::Color as InnerColor;
use events::Response;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
#[wasm_bindgen] #[wasm_bindgen]
@ -24,6 +26,15 @@ impl Color {
} }
} }
#[derive(Serialize, Deserialize)]
pub struct WasmResponse(Response);
impl WasmResponse {
pub fn new(response: Response) -> Self {
Self(response)
}
}
macro_rules! match_string_to_enum { macro_rules! match_string_to_enum {
(match ($e:expr) {$($var:ident),* $(,)?}) => { (match ($e:expr) {$($var:ident),* $(,)?}) => {
match $e { match $e {

View File

@ -11,3 +11,4 @@ license = "Apache-2.0"
[dependencies] [dependencies]
log = "0.4" log = "0.4"
kurbo = "0.8.0" kurbo = "0.8.0"
serde = { version = "1.0", features = ["derive"] }

View File

@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};
#[repr(C)] #[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)] #[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub struct Color { pub struct Color {
red: f32, red: f32,
green: f32, green: f32,

View File

@ -1,7 +1,8 @@
use crate::color::Color; use crate::color::Color;
use serde::{Deserialize, Serialize};
#[repr(C)] #[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)] #[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub struct Fill { pub struct Fill {
color: Option<Color>, color: Option<Color>,
} }
@ -21,7 +22,7 @@ impl Fill {
} }
#[repr(C)] #[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)] #[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub struct Stroke { pub struct Stroke {
color: Color, color: Color,
width: f32, width: f32,
@ -37,7 +38,7 @@ impl Stroke {
} }
#[repr(C)] #[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)] #[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub struct PathStyle { pub struct PathStyle {
stroke: Option<Stroke>, stroke: Option<Stroke>,
fill: Option<Fill>, fill: Option<Fill>,

View File

@ -1,6 +1,8 @@
use crate::{layers::style, LayerId}; use crate::{layers::style, LayerId};
#[derive(Debug, Clone, PartialEq)] use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum Operation { pub enum Operation {
AddCircle { AddCircle {
path: Vec<LayerId>, path: Vec<LayerId>,

View File

@ -1,14 +1,15 @@
use crate::LayerId; use crate::LayerId;
use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
#[derive(Debug, Clone)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LayerPanelEntry { pub struct LayerPanelEntry {
pub name: String, pub name: String,
pub visible: bool, pub visible: bool,
pub layer_type: LayerType, pub layer_type: LayerType,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LayerType { pub enum LayerType {
Folder, Folder,
Shape, Shape,
@ -25,7 +26,7 @@ impl fmt::Display for LayerType {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[repr(C)] #[repr(C)]
// TODO - Make Copy when possible // TODO - Make Copy when possible
pub enum DocumentResponse { pub enum DocumentResponse {

View File

@ -12,6 +12,7 @@ license = "Apache-2.0"
log = "0.4" log = "0.4"
bitflags = "1.2.1" bitflags = "1.2.1"
thiserror = "1.0.24" thiserror = "1.0.24"
serde = { version = "1.0", features = ["derive"] }
[dependencies.document-core] [dependencies.document-core]
path = "../document" path = "../document"

View File

@ -2,6 +2,8 @@ use crate::tools::ToolType;
use crate::Color; use crate::Color;
use bitflags::bitflags; use bitflags::bitflags;
use serde::{Deserialize, Serialize};
#[doc(inline)] #[doc(inline)]
pub use document_core::DocumentResponse; pub use document_core::DocumentResponse;
@ -29,7 +31,7 @@ pub enum Event {
KeyDown(Key), KeyDown(Key),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[repr(C)] #[repr(C)]
pub enum ToolResponse { pub enum ToolResponse {
SetActiveTool { tool_name: String }, SetActiveTool { tool_name: String },
@ -49,7 +51,7 @@ impl fmt::Display for ToolResponse {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[repr(C)] #[repr(C)]
// TODO - Make Copy when possible // TODO - Make Copy when possible
pub enum Response { pub enum Response {