From ec7d2e4bb123c74ab4e85014522d70ecfcdb0f74 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 16 Feb 2022 09:07:58 -0500 Subject: [PATCH] refactoring --- src/lib.rs | 35 ++++++----------------------------- src/misc.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 src/misc.rs diff --git a/src/lib.rs b/src/lib.rs index 5499ffb..e307d2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,12 +4,12 @@ use plotters_canvas::CanvasBackend; use std::panic; use wasm_bindgen::prelude::*; use web_sys::HtmlCanvasElement; +mod misc; +use crate::misc::{Chart, DrawResult}; #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; -pub type DrawResult = Result>; - #[wasm_bindgen] extern "C" { // Use `js_namespace` here to bind `console.log(..)` instead of just @@ -18,18 +18,7 @@ extern "C" { fn log(s: &str); } -/// Result of screen to chart coordinates conversion. -#[wasm_bindgen] -pub struct Point { - pub x: f32, - pub y: f32, -} - -#[wasm_bindgen] -impl Point { - pub fn new(x: f32, y: f32) -> Self { Self { x, y } } -} - +// Manages Chart generation and caching of values #[wasm_bindgen] pub struct ChartManager { func_str: String, @@ -69,6 +58,7 @@ impl ChartManager { // Used in order to hook into `panic!()` to log in the browser's console pub fn init_panic_hook() { panic::set_hook(Box::new(console_error_panic_hook::hook)); } + #[inline(always)] fn get_back_cache(&self) -> Vec<(f32, f32)> { log("Using back_cache"); match &self.back_cache { @@ -77,6 +67,7 @@ impl ChartManager { } } + #[inline(always)] fn get_front_cache(&self) -> (Vec<(f32, f32, f32)>, f32) { log("Using front_cache"); match &self.front_cache { @@ -85,6 +76,7 @@ impl ChartManager { } } + #[inline(always)] fn draw( &mut self, element: HtmlCanvasElement, ) -> DrawResult<(impl Fn((i32, i32)) -> Option<(f32, f32)>, f32)> { @@ -229,18 +221,3 @@ impl ChartManager { (data2, area) } } - -#[wasm_bindgen] -pub struct Chart { - convert: Box Option<(f32, f32)>>, - area: f32, -} - -#[wasm_bindgen] -impl Chart { - pub fn get_area(&self) -> f32 { self.area } - - pub fn coord(&self, x: i32, y: i32) -> Option { - (self.convert)((x, y)).map(|(x, y)| Point::new(x, y)) - } -} diff --git a/src/misc.rs b/src/misc.rs new file mode 100644 index 0000000..925400a --- /dev/null +++ b/src/misc.rs @@ -0,0 +1,30 @@ +use wasm_bindgen::prelude::*; + +pub type DrawResult = Result>; + +/// Result of screen to chart coordinates conversion. +#[wasm_bindgen] +pub struct Point { + pub x: f32, + pub y: f32, +} + +#[wasm_bindgen] +impl Point { + pub fn new(x: f32, y: f32) -> Self { Self { x, y } } +} + +#[wasm_bindgen] +pub struct Chart { + pub(crate) convert: Box Option<(f32, f32)>>, + pub(crate) area: f32, +} + +#[wasm_bindgen] +impl Chart { + pub fn get_area(&self) -> f32 { self.area } + + pub fn coord(&self, x: i32, y: i32) -> Option { + (self.convert)((x, y)).map(|(x, y)| Point::new(x, y)) + } +}