maze_wasm/
wasm_common.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use data_model::{Maze, MazeDefinition};
#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;

//************************************************************************************************************
// Currently, we have to have duplicated definitions of MazeWasm for wasm-bindgen and wasm32 builds, due to 
// the fact that we cannot conditionally mark the maze field with #[wasm_bindgen(skip)] 
//- see https://github.com/anza-xyz/agave/pull/1658 for details on this issue
//************************************************************************************************************/

#[cfg(feature = "wasm-bindgen")]
#[wasm_bindgen]
pub struct MazeWasm {
    #[wasm_bindgen(skip)]
    pub maze: Maze,
}
#[cfg(not(feature = "wasm-bindgen"))]
#[repr(C)]
pub struct MazeWasm {
    pub maze: Maze,
}

impl Clone for MazeWasm {
    fn clone(&self) -> Self {
        MazeWasm {
            maze: self.maze.clone(),
        }
    }
}

#[cfg(feature = "wasm-bindgen")]
#[wasm_bindgen]
pub enum MazeCellTypeWasm {
    Empty,
    Start,
    Finish,
    Wall,
}

#[cfg(not(feature = "wasm-bindgen"))]
#[repr(C)]
pub enum MazeCellTypeWasm {
    Empty,
    Start,
    Finish,
    Wall,
}

/// Converts a cell type character to a MazeCellTypeWasm value
///
/// # Returns
///
/// `MazeCellTypeWasm`
///
pub fn to_cell_type_enum(cell_type: char) -> MazeCellTypeWasm {
    match cell_type {
        'S' => MazeCellTypeWasm::Start,
        'F' => MazeCellTypeWasm::Finish,
        'W' => MazeCellTypeWasm::Wall,
        _ => MazeCellTypeWasm::Empty,
    }
}
/// Creates an empty maze
///
/// # Returns
///
/// `Maze`
///
pub fn new_maze() -> Maze {
    let def = MazeDefinition::new(0, 0);
    Maze::new(def)
}