rpgx_wasm/eucl/
direction.rs1use crate::{prelude::WasmDelta, traits::WasmWrapper};
2use rpgx::prelude::Direction;
3use wasm_bindgen::prelude::*;
4
5#[wasm_bindgen(js_name = Direction)]
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub struct WasmDirection {
8 inner: Direction,
9}
10
11impl WasmWrapper<Direction> for WasmDirection {
12 fn from_inner(inner: Direction) -> Self {
13 WasmDirection { inner }
14 }
15
16 fn inner(&self) -> &Direction {
17 &self.inner
18 }
19
20 fn into_inner(self) -> Direction {
21 self.inner
22 }
23}
24
25#[wasm_bindgen(js_class = Direction)]
26impl WasmDirection {
27 #[wasm_bindgen(constructor)]
30 pub fn new(direction_str: String) -> WasmDirection {
31 let dir = match direction_str.to_lowercase().as_str() {
32 "up" => Direction::Up,
33 "down" => Direction::Down,
34 "left" => Direction::Left,
35 "right" => Direction::Right,
36 _ => panic!("Invalid direction string"),
37 };
38 WasmDirection { inner: dir }
39 }
40 #[wasm_bindgen(getter)]
41 pub fn name(&self) -> String {
42 match self.inner {
43 Direction::Up => "Up".to_string(),
44 Direction::Down => "Down".to_string(),
45 Direction::Left => "Left".to_string(),
46 Direction::Right => "Right".to_string(),
47 }
48 }
49
50 #[wasm_bindgen(js_name = fromDelta)]
52 pub fn from_delta(delta: &WasmDelta) -> Option<WasmDirection> {
53 Direction::from_delta(&delta.inner()).map(|d| WasmDirection { inner: d })
54 }
55
56 #[wasm_bindgen(js_name = toDelta)]
58 pub fn to_delta(&self) -> WasmDelta {
59 WasmDelta::from_inner(self.inner.to_delta())
60 }
61
62 #[wasm_bindgen(js_name = equals)]
64 pub fn equals(&self, other: &WasmDirection) -> bool {
65 self.inner == other.inner
66 }
67}