Skip to content

Commit

Permalink
fix: formatting and lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwine36 committed Sep 13, 2024
1 parent 361fc08 commit b4a0e0a
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 50 deletions.
2 changes: 1 addition & 1 deletion crates/test-web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use wasm_bindgen::JsCast;
use wasm_draw::init_app;
// mod lib;
use web_sys::{window, Element, HtmlCanvasElement, HtmlElement};
use web_sys::{window, HtmlCanvasElement};

fn main() {
console_error_panic_hook::set_once();
Expand Down
1 change: 1 addition & 0 deletions crates/test-web/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 1 addition & 3 deletions crates/wasm-draw/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ use crate::state::get_element_dimensions;
use crate::state::Dimensions;
use crate::state::State;
use crate::tool::ToolType;
use serde::{Deserialize, Serialize};

use std::cell::RefCell;
use std::cmp::{max, min};
use std::rc::Rc;
use utilities::console_log;
use wasm_bindgen::prelude::*;
use web_sys::{
console, window, CanvasRenderingContext2d, Element, HtmlCanvasElement, HtmlElement, MouseEvent,
window, CanvasRenderingContext2d, HtmlCanvasElement, MouseEvent,
};

use crate::tool::Measurement;
Expand Down
5 changes: 1 addition & 4 deletions crates/wasm-draw/src/canvas.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
extern crate wasm_bindgen;

use crate::log;
use crate::state::State;
// use crate::tool::Line;
// use crate::tool::ToolBelt;
use std::cell::RefCell;
use std::rc::Rc;
use utilities::console_log;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement, MouseEvent};
use web_sys::{HtmlCanvasElement};
pub fn init_canvas(canvas: &HtmlCanvasElement, state: &Rc<RefCell<State>>) -> Result<(), JsValue> {
canvas.set_width(state.borrow().get_width());
canvas.set_height(state.borrow().get_height());
Expand Down
7 changes: 1 addition & 6 deletions crates/wasm-draw/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
extern crate wasm_bindgen;

use std::cell::RefCell;
use std::cmp::{max, min};
use std::rc::Rc;
use utilities::console_log;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{window, Element, HtmlCanvasElement, HtmlElement};
use web_sys::{window, HtmlCanvasElement};
mod app;
mod canvas;
mod draw_engine;
Expand Down
24 changes: 13 additions & 11 deletions crates/wasm-draw/src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use wasm_bindgen::prelude::*;
pub static COLORS: [(&str, &str); 6] = [
("Black", "#000000"),
Expand All @@ -10,12 +8,10 @@ pub static COLORS: [(&str, &str); 6] = [
("White", "#FFFFFF"),
];

static DEFAULT_COLOR: &str = COLORS[0].1;
use crate::log;
use js_sys::Array;
pub static DEFAULT_COLOR: &str = COLORS[0].1;

use serde::{Deserialize, Serialize};
use utilities::console_log;
use web_sys::{console, js_sys, window, Element, HtmlCanvasElement, HtmlElement, MouseEvent};

pub static PEN_SIZES: [f64; 4] = [1.0, 2.0, 4.0, 8.0];

static DEFAULT_PEN_SIZE: f64 = PEN_SIZES[0];
Expand Down Expand Up @@ -47,10 +43,16 @@ pub struct Settings {
pub tools: Vec<String>,
}

#[wasm_bindgen]
// #[wasm_bindgen]
impl Default for Settings {
fn default() -> Self {
Self::new()
}
}

impl Settings {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
// #[wasm_bindgen(constructor)]
pub fn new() -> Settings {
let colors = COLORS
.iter()
.map(|(name, hex)| Color {
Expand All @@ -59,7 +61,7 @@ impl Settings {
})
.collect();

Self {
Settings {
colors,
pen_sizes: PEN_SIZES.to_vec(),
tools: vec!["pen".to_string(), "line".to_string(), "circle".to_string()],
Expand Down
17 changes: 5 additions & 12 deletions crates/wasm-draw/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use wasm_bindgen::prelude::*;
pub static COLORS: [(&str, &str); 6] = [
("Black", "#000000"),
("Green", "#3DC06C"),
("Red", "#FF0000"),
("Blue", "#4F8DE4"),
("Yellow", "#FAE589"),
("White", "#FFFFFF"),
];

use serde::{Deserialize, Serialize};
static DEFAULT_COLOR: &str = COLORS[0].1;
use crate::settings::Settings;

use crate::settings::{Settings, DEFAULT_COLOR};
use crate::{log, tool::ToolType};
use utilities::console_log;
use web_sys::{console, window, Element, HtmlCanvasElement, HtmlElement, MouseEvent};
use web_sys::{HtmlCanvasElement, HtmlElement, MouseEvent};
pub static PEN_SIZES: [f64; 4] = [1.0, 2.0, 4.0, 8.0];

static DEFAULT_PEN_SIZE: f64 = PEN_SIZES[0];
Expand Down Expand Up @@ -56,7 +49,7 @@ pub struct State {
#[wasm_bindgen]
impl State {
pub fn new(canvas_el: &HtmlCanvasElement, settings: Settings) -> State {
let dimensions = get_element_dimensions(&canvas_el);
let dimensions = get_element_dimensions(canvas_el);
console_log!("dimensions: {:?}", dimensions);
State {
dimensions: dimensions.clone(),
Expand Down
32 changes: 21 additions & 11 deletions crates/wasm-draw/src/tool.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
use crate::log;
use crate::settings;
use crate::state::Dimensions;
use crate::state::State;
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::cmp::{max, min};
use std::rc::Rc;
use utilities::console_log;
use wasm_bindgen::prelude::*;
use web_sys::{console, window, CanvasRenderingContext2d, Element, HtmlCanvasElement, HtmlElement};
use web_sys::{CanvasRenderingContext2d};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[wasm_bindgen]
Expand Down Expand Up @@ -56,12 +48,10 @@ impl Measurement {
}

pub fn finish(&mut self) {
console_log!("finishing measurement");
if self.tool != ToolType::Pen && self.points.len() > 2 {
let last_point = self.points.last().unwrap();
let first_point = self.points.first().unwrap();
self.points = vec![first_point.clone(), last_point.clone()];
console_log!("finished measurement: {:?}", self);
}
}

Expand Down Expand Up @@ -100,3 +90,23 @@ impl Measurement {
context.stroke();
}
}

#[cfg(test)]
mod tests {
use super::*;
use rand::Rng;

#[test]
fn it_works() {
let mut measurement = Measurement::new(&"red".to_string(), 2.0, ToolType::Rectangle);

for _ in 0..100 {
let x = rand::thread_rng().gen_range(0.0..100.0);
let y = rand::thread_rng().gen_range(0.0..100.0);
measurement.add_point(x, y);
}
assert_eq!(measurement.points.len(), 100);
measurement.finish();
assert_eq!(measurement.points.len(), 2);
}
}
4 changes: 2 additions & 2 deletions crates/wasm-draw/src/toolbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use web_sys::{
HtmlImageElement,
};

use crate::state::{State, COLORS, PEN_SIZES};
use crate::state::{State};

const generic_box_styles: &str = "height: 50px; width: 50px; border-bottom: 1px solid #efefef; display: flex; align-items: center; justify-content: center;";

Expand Down Expand Up @@ -92,7 +92,7 @@ fn get_pen_size_element(
let state_copy = state.clone();

let handle_click = Closure::wrap(Box::new(move || {
state_copy.borrow_mut().update_pen_size(size as f64);
state_copy.borrow_mut().update_pen_size(size);
}) as Box<dyn FnMut()>);

el.add_event_listener_with_callback("click", handle_click.as_ref().unchecked_ref())?;
Expand Down
1 change: 1 addition & 0 deletions crates/wasm-draw/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit b4a0e0a

Please sign in to comment.