pub struct MazeWasm {
pub maze: Maze,
}Fields§
§maze: MazeImplementations§
Source§impl MazeWasm
impl MazeWasm
Sourcepub fn new() -> Result<MazeWasm, JsValue>
pub fn new() -> Result<MazeWasm, JsValue>
Creates a new maze instance
§Returns
A new maze instance
§Examples
Create a new maze and print its dimensions (which will be 0 rows x 0 columns)
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
import init, { MazeWasm } from 'maze_wasm.js';
/// try {
let maze = new MazeWasm();
console.log("Successfully created maze. Dimensions: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn reset(&mut self) -> MazeWasm
pub fn reset(&mut self) -> MazeWasm
Resets the maze instance to empty
§Returns
The empty maze instance
§Examples
Create a new maze, resize it to 10 rows x 5 columns and print out its dimensions. Then, reset it and print out its dimensions again (which will now be 0 rows x 0 columns).
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.resize(10, 5);
console.log("After resize(), dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
maze.reset();
console.log("After reset(), dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn resize(
&mut self,
new_row_count: JsValue,
new_col_count: JsValue,
) -> Result<(), JsValue>
pub fn resize( &mut self, new_row_count: JsValue, new_col_count: JsValue, ) -> Result<(), JsValue>
Resizes the maze instance
§Arguments
new_row_count- New number of rowsnew_col_count- New number of columns
§Returns
This function will return an error if the maze could not be resized
§Examples
Create a new maze, print its dimensions, resize it to 10 rows x 5 columns and then print out its dimensions again.
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm ();
console.log("After creation, dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s )");
maze.resize(10, 5);
console.log("After resize(), dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn insert_rows(
&mut self,
start_row: JsValue,
count: JsValue,
) -> Result<(), JsValue>
pub fn insert_rows( &mut self, start_row: JsValue, count: JsValue, ) -> Result<(), JsValue>
Inserts one or more empty rows into the maze instance
§Arguments
start_row- Start row index (zero-based)count- Number of rows to insert
§Returns
This function will return an error in the following situations:
- If the target rows are out of range
§Examples
Create a new maze, print its dimensions, insert 5 rows and then print out its dimensions again (which will now be 5 rows x 0 columns).
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
console.log("After creation, dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
maze.insert_rows(0, 5);
console.log("After insert_rows(), dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn delete_rows(
&mut self,
start_row: JsValue,
count: JsValue,
) -> Result<(), JsValue>
pub fn delete_rows( &mut self, start_row: JsValue, count: JsValue, ) -> Result<(), JsValue>
Deletes one or more consecutive rows from the maze instance
§Arguments
start_row- Start row index (zero-based)count- Number of rows to delete
§Returns
This function will return an error in the following situations:
- If the definition is empty
- If the target rows are out of range
§Examples
Create a new maze, insert 5 rows and print out its dimensions. Then, delete rows 2 to 4 and print out the dimensions again (which will now be 2 rows x 0 columns).
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
console.log("After creation, dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
maze.insert_rows(0, 5);
console.log("After insert_rows(), dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
maze.delete_rows(1, 3);
console.log("After delete_rows(), dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn insert_cols(
&mut self,
start_col: JsValue,
count: JsValue,
) -> Result<(), JsValue>
pub fn insert_cols( &mut self, start_col: JsValue, count: JsValue, ) -> Result<(), JsValue>
Inserts one or more empty columns into the maze instance
§Arguments
start_col- Start column index (zero-based)count- Number of columns to insert
§Returns
This function will return an error in the following situations:
- If the definition is empty
- If the target columns are out of range
§Examples
Create a new maze, insert 1 row and print out its dimensions. Then, insert 10 colums and print out the dimensions again (which will now be 1 row x 10 columns).
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
console.log("After creation, dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
maze.insert_rows(0, 1);
console.log("After insert_rows(), dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
maze.insert_cols(0, 10);
console.log("After insert_cols(), dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn delete_cols(
&mut self,
start_col: JsValue,
count: JsValue,
) -> Result<(), JsValue>
pub fn delete_cols( &mut self, start_col: JsValue, count: JsValue, ) -> Result<(), JsValue>
Deletes one or more consecutive columns from the maze instance
§Arguments
start_col- Start column index (zero-based)count- Number of columns to delete
§Returns
This function will return an error in the following situations:
- If the definition is empty
- If the target columns are out of range
§Examples
Create a new maze, resize it to 10 rows x 5 column and print out its dimensions. Then, delete columns 2 to 4 and print out the dimensions again (which will now be 10 rows x 2 columns).
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.resize(10, 5);
console.log("After resize(), dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
maze.delete_cols(1, 3);
console.log("After delete_cols(), dimensions are: ", maze.get_row_count(), "row(s) x ", maze.get_col_count(), " column(s)");
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks whether the maze instance is empty
§Returns
Boolean
§Examples
Create a new maze and print out whether it is empty (true). Then, resize it to
1 row x 2 columns and again print out whether it is empty (false).
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
console.log("After creation, is_empty() = ", maze.is_empty());
maze.resize(1,2);
console.log("After resize(), is_empty() = ", maze.is_empty());
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn get_row_count(&self) -> usize
pub fn get_row_count(&self) -> usize
Returns the number of rows associated with the maze instance
§Returns
Number of rows
§Examples
Create a new maze and print out the number rows (0). Then, resize it to 10 rows x 5 columns and then print out the number of rows again (10).
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
console.log("After creation, get_row_count() = ", maze.get_row_count());
maze.resize(10, 5);
console.log("After resize(), get_row_count() = ", maze.get_row_count());
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn get_col_count(&self) -> usize
pub fn get_col_count(&self) -> usize
Returns the number of columns associated with the maze instance
§Returns
Number of columns
§Examples
Create a new maze and print out the number columns (0). Then, resize it to 10 rows x 5 columns and then print out the number of columns again (5).
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
console.log("After creation, get_col_count() = ", maze.get_col_count());
maze.resize(10, 5);
console.log("After resize(), get_col_count() = ", maze.get_col_count());
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn get_cell(&self, row: JsValue, col: JsValue) -> Result<Object, JsValue>
pub fn get_cell(&self, row: JsValue, col: JsValue) -> Result<Object, JsValue>
Returns cell information for the given location within the maze instance
§Arguments
row- Row index (zero-based)col- Column index (zero-based)
§Returns
This function will return an error in the following situations:
- If the target location is out of range
If sucessful, a cell information object will be returned with the following properties:
cell_type- The type (MazeCellTypeWasm) associated with the cell
§Examples
Create a new maze and resize it to 10 rows x 5 columns. Then, print out the cell information for the cell at row = 1, column = 2.
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.resize(10, 5);
console.log("get_cell(1, 2) = ", maze.get_cell(1, 2));
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn set_start_cell(
&mut self,
start_row: JsValue,
start_col: JsValue,
) -> Result<(), JsValue>
pub fn set_start_cell( &mut self, start_row: JsValue, start_col: JsValue, ) -> Result<(), JsValue>
Sets the start cell location within the maze instance
§Arguments
start_row- Start cell row index (zero-based)start_col- Start cell column index (zero-based)
§Returns
This function will return an error in the following situations:
- If the target location is out of range
§Examples
Create a new maze, resize it to 10 rows x 5 columns and print out the cell
information for the cell at row = 1, column = 2 (cell_type will be MazeCellTypeWasm::Empty).
Then, set the start cell to that same location and print out the cell information again
(cell_type will now be MazeCellTypeWasm::Start).
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.resize(10, 5);
console.log("Before set_start_cell(), get_cell(1, 2) = ", maze.get_cell(1, 2));
maze.set_start_cell(1, 2);
console.log("After set_start_cell(), get_cell(1, 2) = ", maze.get_cell(1, 2));
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn get_start_cell(&mut self) -> Result<Object, JsValue>
pub fn get_start_cell(&mut self) -> Result<Object, JsValue>
Returns the start cell associated with the maze instance (if any)
§Returns
This function will return an error in the following situations:
- If the start cell does not exist
If sucessful, an object will be returned with the following properties:
row- Start cell row index (zero-based)col- Start cell column index (zero-based)
§Examples
Create a new maze, resize it to 10 rows x 5 columns and set the start cell to be at row = 1, column = 2. Then, retreive and print out of the details for the start cell.
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.resize(10, 5);
maze.set_start_cell(1, 2);
console.log("get_start_cell() = ", maze.get_start_cell());
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn set_finish_cell(
&mut self,
finish_row: JsValue,
finish_col: JsValue,
) -> Result<(), JsValue>
pub fn set_finish_cell( &mut self, finish_row: JsValue, finish_col: JsValue, ) -> Result<(), JsValue>
Sets the finish cell location within the maze instance
§Arguments
finish_row- Finish cell row index (zero-based)finish_col- Finish cell column index (zero-based)
§Returns
This function will return an error in the following situations:
- If the target location is out of range
§Examples
Create a new maze, resize it to 10 rows x 5 columns and print out the cell
information for the cell at row = 3, column = 4 (cell_type will be MazeCellTypeWasm::Empty).
Then, set the finish cell to that same location and print out the cell information again
(cell_type will now be MazeCellTypeWasm::Finish).
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.resize(10, 5);
console.log("Before set_finish_cell(), get_cell(3, 4) = ", maze.get_cell(3, 4));
maze.set_finish_cell(3, 4);
console.log("After set_finish_cell(), get_cell(3, 4) = ", maze.get_cell(3, 4));
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn get_finish_cell(&mut self) -> Result<Object, JsValue>
pub fn get_finish_cell(&mut self) -> Result<Object, JsValue>
Returns the finish cell associated with the maze instance (if any)
§Returns
This function will return an error in the following situations:
- If the finish cell does not exist
If sucessful, an object will be returned with the following properties:
row- Finish cell row index (zero-based)col- Finish cell column index (zero-based)
§Examples
Create a new maze, resize it to 10 rows x 5 columns and set the finish cell to be at row = 9, column = 4. Then, retreive and print out of the details for the finish cell.
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.resize(10, 5);
maze.set_finish_cell(9, 4);
console.log("get_finish_cell() = ", maze.get_finish_cell());
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn set_wall_cells(
&mut self,
start_row: JsValue,
start_col: JsValue,
end_row: JsValue,
end_col: JsValue,
) -> Result<(), JsValue>
pub fn set_wall_cells( &mut self, start_row: JsValue, start_col: JsValue, end_row: JsValue, end_col: JsValue, ) -> Result<(), JsValue>
Sets a range of cells within the maze instance to be walls (cell_type = MazeCellTypeWasm::Wall)
§Arguments
start_row- Start row index (zero-based)start_col- Start column index (zero-based)finish_row- Finish row index (zero-based)finish_col- Finish column index (zero-based)
§Returns
This function will return an error in the following situations:
- If the target location is out of range
§Examples
Create a new maze, resize it to 10 rows x 5 columns and then set
cells 2 to 4 to be walls in the first row. Then print the cell
information for the top row, which will have cells (0, 0) and (0, 4)
as MazeCellTypeWasm::Empty and cells (0, 1) to (0, 3) as
MazeCellTypeWasm::Wall.
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.resize(10, 5);
maze.set_wall_cells(0, 1, 0, 3);
for (let col = 0; col < 5; col ++) {
console.log(`After set_walls_cell(), cell_type at (0, ${col}) = `, maze.get_cell(0, col).cell_type);
}
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn clear_cells(
&mut self,
start_row: JsValue,
start_col: JsValue,
end_row: JsValue,
end_col: JsValue,
) -> Result<(), JsValue>
pub fn clear_cells( &mut self, start_row: JsValue, start_col: JsValue, end_row: JsValue, end_col: JsValue, ) -> Result<(), JsValue>
Clears a range of cells within the maze instance, setting their cell_type = MazeCellTypeWasm::Empty
§Arguments
start_row- Start row index (zero-based)start_col- Start column index (zero-based)finish_row- Finish row index (zero-based)finish_col- Finish column index (zero-based)
§Returns
This function will return an error in the following situations:
- If the target location is out of range
§Examples
Create a new maze, resize it to 10 rows x 5 columns and then set
cells 2 to 4 to be walls in the first row. Then print the cell_type
for the top row, which will have cells (0, 0) and (0, 4)
as MazeCellTypeWasm::Empty and cells (0, 1) to (0, 3) as
MazeCellTypeWasm::Wall. Finally, clear cells (0, 2) to (3, 4) and
reprint the cell_type for the top row, which will now have
just once cell (0, 1) as MazeCellTypeWasm::Wall.
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.resize(10, 5);
maze.set_wall_cells(0, 1, 0, 3);
for (let col = 0; col < 5; col ++) {
console.log(`After set_walls_cell(), cell_type at (0, ${col}) = `, maze.get_cell(0, col).cell_type);
}
maze.clear_cells(0, 2, 3, 4);
for (let col = 0; col < 5; col ++) {
console.log(`After clear_walls(), cell_type at (0, ${col}) = `, maze.get_cell(0, col).cell_type);
}
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn to_json(&self) -> Result<String, JsValue>
pub fn to_json(&self) -> Result<String, JsValue>
This function will return the JSON string representation for the maze instance
§Returns
JSON string representing the maze, or an error if the JSON could not be generated
§Examples
Create a new maze, resize it to 6 rows x 5 columns and then set cells 2 to 4 to be walls in the first 3 rows. The conver to JSON and print the result.
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.resize(6, 5);
maze.set_wall_cells(0, 1, 2, 4);
let json = maze.to_json();
console.log("to_json() returned: ", json);
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn from_json(&mut self, json_string: JsValue) -> Result<(), JsValue>
pub fn from_json(&mut self, json_string: JsValue) -> Result<(), JsValue>
Initializes the maze instance by reading the JSON string content provided
§Returns
This function will return an error if the JSON could not be read
§Examples
Create a new maze and initialise it from a JSON string. Then, print
the cell_type for each cell.
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.from_json(`{
\"id\":\"maze_id\",
\"name\":\"test\",
\"definition\": {
\"grid\":[
[\"S\", \"W\", \" \", \" \", \"W\"],
[\" \", \"W\", \" \", \"W\", \" \"],
[\" \", \" \", \" \", \"W\", \"F\"],
[\"W\", \" \", \"W\", \" \", \" \"],
[\" \", \" \", \" \", \"W\", \" \"],
[\"W\", \"W\", \" \", \" \", \" \"],
[\"W\", \"W\", \" \", \"W\", \" \"]
]
}}`);
for (let row = 0; row < maze.get_row_count(); row ++) {
for (let col = 0; col < maze.get_col_count(); col ++) {
console.log(`After from_json(), cell_type at (${row}, ${col}) = `, maze.get_cell(row, col).cell_type);
}
}
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Sourcepub fn solve(&self) -> Result<MazeSolutionWasm, JsValue>
pub fn solve(&self) -> Result<MazeSolutionWasm, JsValue>
Attempts to solve the path between the start and end points defined within the maze instance
§Returns
A maze solution (MazeSolutionWasm) if successful, else an error if the maze could not be solved
§Examples
Initialize a maze from a JSON string, then attempt to solve it and, if successful, print the maze solution path’s points
// Javascript <script> content:
import init, { MazeWasm } from 'maze_wasm.js';
async function run() {
await init();
try {
let maze = new MazeWasm();
maze.from_json(`{
\"id\":\"maze_id\",
\"name\":\"test\",
\"definition\": {
\"grid\":[
[\"S\", \"W\", \" \", \" \", \"W\"],
[\" \", \"W\", \" \", \"W\", \" \"],
[\" \", \" \", \" \", \"W\", \"F\"],
[\"W\", \" \", \"W\", \" \", \" \"],
[\" \", \" \", \" \", \"W\", \" \"],
[\"W\", \"W\", \" \", \" \", \" \"],
[\"W\", \"W\", \" \", \"W\", \" \"]
]
}}`);
let solution = maze.solve();
let solutionPoints = solution.get_path_points();
console.log("Maze solve() succeeded. Solution points are: ", solutionPoints);
} catch (e) {
console.error("Operation failed: ", e);
}
}
run();Trait Implementations§
Source§impl FromWasmAbi for MazeWasm
impl FromWasmAbi for MazeWasm
Source§impl IntoWasmAbi for MazeWasm
impl IntoWasmAbi for MazeWasm
Source§impl LongRefFromWasmAbi for MazeWasm
impl LongRefFromWasmAbi for MazeWasm
Source§impl OptionFromWasmAbi for MazeWasm
impl OptionFromWasmAbi for MazeWasm
Source§impl OptionIntoWasmAbi for MazeWasm
impl OptionIntoWasmAbi for MazeWasm
Source§impl RefFromWasmAbi for MazeWasm
impl RefFromWasmAbi for MazeWasm
Source§impl RefMutFromWasmAbi for MazeWasm
impl RefMutFromWasmAbi for MazeWasm
Source§impl TryFromJsValue for MazeWasm
impl TryFromJsValue for MazeWasm
Source§impl VectorFromWasmAbi for MazeWasm
impl VectorFromWasmAbi for MazeWasm
Source§impl VectorIntoWasmAbi for MazeWasm
impl VectorIntoWasmAbi for MazeWasm
Auto Trait Implementations§
impl Freeze for MazeWasm
impl RefUnwindSafe for MazeWasm
impl Send for MazeWasm
impl Sync for MazeWasm
impl Unpin for MazeWasm
impl UnwindSafe for MazeWasm
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
Source§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
IntoWasmAbi::AbiSource§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
IntoWasmAbi::into_abi, except that it may throw and never
return in the case of Err.