From fe111a668478a9045d618ebd5a23346bf5d65265 Mon Sep 17 00:00:00 2001 From: ProTheory8 <59506423+protheory8@users.noreply.github.com> Date: Sat, 27 Mar 2021 12:43:58 +0500 Subject: [PATCH] Add camelcase rule, import Error type from JS and rename get_inner_color to inner. (#44) * Add camelcase rule to .eslintrc.js * Import Error type from JS, rename crate::wrappers::Color::get_inner_color to crate::wrappers::Color::inner --- client/web/.eslintrc.js | 1 + client/web/wasm/src/lib.rs | 1 + client/web/wasm/src/shims.rs | 10 ++++++++++ client/web/wasm/src/viewport.rs | 9 +++++---- client/web/wasm/src/wrappers.rs | 10 +++++++--- core/editor/src/color.rs | 1 + 6 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 client/web/wasm/src/shims.rs diff --git a/client/web/.eslintrc.js b/client/web/.eslintrc.js index c7edac884..f09f8675a 100644 --- a/client/web/.eslintrc.js +++ b/client/web/.eslintrc.js @@ -19,5 +19,6 @@ module.exports = { "linebreak-style": ["error", "unix"], indent: ["error", "tab"], quotes: ["error", "double"], + camelcase: ["error", { ignoreImports: true, ignoreDestructuring: true }] }, }; diff --git a/client/web/wasm/src/lib.rs b/client/web/wasm/src/lib.rs index 4204ebb08..dcdbcbc4e 100644 --- a/client/web/wasm/src/lib.rs +++ b/client/web/wasm/src/lib.rs @@ -1,3 +1,4 @@ +mod shims; pub mod utils; pub mod viewport; pub mod window; diff --git a/client/web/wasm/src/shims.rs b/client/web/wasm/src/shims.rs new file mode 100644 index 000000000..54cdbf50c --- /dev/null +++ b/client/web/wasm/src/shims.rs @@ -0,0 +1,10 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + #[derive(Clone, Debug)] + pub type Error; + + #[wasm_bindgen(constructor)] + pub fn new(msg: &str) -> Error; +} diff --git a/client/web/wasm/src/viewport.rs b/client/web/wasm/src/viewport.rs index 2eaabbb98..67a92f730 100644 --- a/client/web/wasm/src/viewport.rs +++ b/client/web/wasm/src/viewport.rs @@ -1,3 +1,4 @@ +use crate::shims::Error; use crate::wrappers::{translate_tool, Color}; use graphite_editor_core::tools::ToolState; use wasm_bindgen::prelude::*; @@ -8,10 +9,10 @@ pub static mut TOOL_STATE: ToolState = ToolState::default(); #[wasm_bindgen] pub fn select_tool(tool: String) -> Result<(), JsValue> { let tool_state = unsafe { &mut TOOL_STATE }; - if let Some(tool) = translate_tool(tool.as_str()) { + if let Some(tool) = translate_tool(&tool) { Ok(tool_state.select_tool(tool)) } else { - Err(JsValue::from(format!("Couldn't select {} because it was not recognized as a valid tool", tool))) + Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into()) } } @@ -25,6 +26,6 @@ pub fn on_mouse_move(x: u32, y: u32) { #[wasm_bindgen] pub fn update_colors(primary_color: Color, secondary_color: Color) { let tool_state = unsafe { &mut TOOL_STATE }; - tool_state.set_primary_color(primary_color.get_inner_color()); - tool_state.set_secondary_color(secondary_color.get_inner_color()); + tool_state.set_primary_color(primary_color.inner()); + tool_state.set_secondary_color(secondary_color.inner()); } diff --git a/client/web/wasm/src/wrappers.rs b/client/web/wasm/src/wrappers.rs index 49af141b9..976fbca1e 100644 --- a/client/web/wasm/src/wrappers.rs +++ b/client/web/wasm/src/wrappers.rs @@ -1,3 +1,4 @@ +use crate::shims::Error; use graphite_editor_core::tools::{SelectAppendMode, ToolType}; use graphite_editor_core::Color as InnerColor; use wasm_bindgen::prelude::*; @@ -8,13 +9,16 @@ pub struct Color(InnerColor); #[wasm_bindgen] impl Color { #[wasm_bindgen(constructor)] - pub fn new(red: f32, green: f32, blue: f32, alpha: f32) -> Self { - Self(InnerColor::from_rgbaf32(red, green, blue, alpha).unwrap_throw()) + pub fn new(red: f32, green: f32, blue: f32, alpha: f32) -> Result { + match InnerColor::from_rgbaf32(red, green, blue, alpha) { + Ok(v) => Ok(Self(v)), + Err(e) => Err(Error::new(&e.to_string()).into()), + } } } impl Color { - pub fn get_inner_color(&self) -> InnerColor { + pub fn inner(&self) -> InnerColor { self.0 } } diff --git a/core/editor/src/color.rs b/core/editor/src/color.rs index 70d351583..76f24db82 100644 --- a/core/editor/src/color.rs +++ b/core/editor/src/color.rs @@ -23,6 +23,7 @@ impl Color { } Ok(color) } + const fn from_unsafe(red: f32, green: f32, blue: f32) -> Color { Color { red, green, blue, alpha: 1. } }