data_model

Struct MazeDefinition

Source
pub struct MazeDefinition {
    pub grid: Vec<Vec<char>>,
}
Expand description

Represents a maze definition

Fields§

§grid: Vec<Vec<char>>

Implementations§

Source§

impl MazeDefinition

Source

pub fn new(row_count: usize, col_count: usize) -> Self

Creates a maze definition instance with the given number of rows x columns empty cells

§Arguments
  • row_count - Number of rows
  • col_count - Number of columns
§Returns

A new maze definition instance

§Examples

Create a definition with 3 rows and 4 columns and then verify its dimensions

use data_model::MazeDefinition;
let definition = MazeDefinition::new(3, 4);
assert_eq!(definition.row_count(), 3);
assert_eq!(definition.col_count(), 4);
Source

pub fn reset(&mut self) -> &mut Self

Resets a maze definition instance to empty

§Returns

The maze definition instance

§Examples

Create a definition with 3 rows and 4 columns, verify its dimensions, reset it and then confirm it is empty

use data_model::MazeDefinition;
let mut definition = MazeDefinition::new(3, 4);
assert_eq!(definition.row_count(), 3);
assert_eq!(definition.col_count(), 4);
assert_eq!(definition.reset().is_empty(), true);
Source

pub fn resize( &mut self, new_row_count: usize, new_col_count: usize, ) -> &mut Self

Resizes a maze definition instance

§Arguments
  • new_row_count - New number of rows
  • new_col_count - New number of columns
§Returns

The maze definition instance

§Examples

Create an empty maze definition, resize it to 3 rows and 4 columns and then verify its dimensions

use data_model::MazeDefinition;
let mut definition = MazeDefinition::new(0, 0);
assert_eq!(definition.row_count(), 0);
assert_eq!(definition.col_count(), 0);
definition.resize(3, 4);
println!("Resize successful");
assert_eq!(definition.row_count(), 3);
assert_eq!(definition.col_count(), 4);
Source

pub fn row_count(&self) -> usize

Returns the number of rows associated with the definition instance

§Returns

Number of rows

§Examples
use data_model::MazeDefinition;
let definition = MazeDefinition::new(3, 4);
assert_eq!(definition.row_count(), 3);
Source

pub fn col_count(&self) -> usize

Returns the number of columns associated with the definition instance

§Returns

Number of columns

§Examples
use data_model::MazeDefinition;
let definition = MazeDefinition::new(3, 4);
assert_eq!(definition.col_count(), 4);
Source

pub fn is_empty(&self) -> bool

Checks whether the definition instance is empty

§Returns

Boolean

§Examples
use data_model::MazeDefinition;
let definition = MazeDefinition::new(3, 4);
assert_eq!(definition.is_empty(), false);
Source

pub fn is_valid_char(ch: char) -> bool

Checks whether the given character is valid for use within the definition

§Returns

Boolean

§Examples

Print whether ‘X’ (false) and ‘S’ (true) are valid characters

use data_model::MazeDefinition;
let x_is_valid = MazeDefinition::is_valid_char('X');
println!("Character 'X' is valid => {}", x_is_valid);
let s_is_valid = MazeDefinition::is_valid_char('S');
println!("Character 'S' is valid => {}", s_is_valid);
Source

pub fn verify_not_empty(&self) -> Result<(), Error>

Verifies whether the definition instance is empty, returning an error if it is

§Returns

This function will return an error if the definition is empty

§Examples

Create an empty maze definition and then verify it

use data_model::MazeDefinition;
let definition = MazeDefinition::new(0, 0);
match definition.verify_not_empty() {
    Err(e) => println!("Verification failed: {}", e.to_string()),
    Ok(()) => println!("MazeDefinition is not empty"),
}
Source

pub fn from_vec(grid: Vec<Vec<char>>) -> Self

Creates a new maze definition for the given vector of cell definition character rows, where:

  • 'S': Represents the starting cell (limited to one).
  • 'F': Represents the finishing cell (limited to one).
  • 'W': Represents a wall.
  • ' ': Represents an empty cell.
§Arguments
  • grid - Vector of row-column cell states
§Returns

A new definition instance

§Examples

Create a 2 row x 3 column definition with a start, finish and a wall in the last column

use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
   vec!['S', ' ', 'W'],
   vec![' ', 'F', 'W']
];
let definition = MazeDefinition::from_vec(grid);
assert_eq!(definition.row_count(), 2);
assert_eq!(definition.col_count(), 3);
Source

pub fn to_state(&self) -> Vec<Vec<MazeCellState>>

Converts the definition instance to a vector of row cell states

§Returns

A vector of row-column cell states

§Examples

Create a maze definition with 3 rows and 4 columns, convert it to a row-column state vector and then confirm that the number of rows in the state vector is the same as the number of rows in the definition (3).

use data_model::MazeDefinition;
let definition = MazeDefinition::new(3, 4);
let state = definition.to_state();
assert_eq!(state.len(), definition.row_count());
assert_eq!(state.len(), 3);
Source

pub fn is_valid(&self, pt: &MazePoint) -> bool

Checks that a point is valid for the definition instance

§Arguments
  • pt - MazePoint to validate
§Returns

Boolean

§Examples

Create a maze definition with 3 rows and 4 columns and confirm that [2,1] is valid, but that [3,1] is not

use data_model::MazeDefinition;
use data_model::MazePoint;
let definition = MazeDefinition::new(3, 4);
assert_eq!(definition.is_valid( &MazePoint {row: 2, col: 1}), true);
assert_eq!(definition.is_valid( &MazePoint {row: 3, col: 1}), false);
Source

pub fn to_display_chars(&self) -> Vec<Vec<char>>

Converts the definition instance to a vector of display characters

§Returns

Vector containing the rows of display characters

§Examples

Create a maze definition with 3 rows and 4 columns and print it

use data_model::MazeDefinition;
let definition = MazeDefinition::new(3, 4);
println!("{:?}", definition.to_display_chars());
Source

pub fn delete_cols( &mut self, start_col: usize, count: usize, ) -> Result<(), Error>

Deletes one or more consecutive columns from the definition 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 maze definition with 2 rows and 4 columns with a start, finish and a wall at the end of each row, delete the second and third columns and print the result

use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
   vec!['S', ' ', ' ', 'W'],
   vec![' ', 'F', ' ', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
definition.delete_cols(1,2).expect("delete_cols() failed");
println!("{:?}", definition.to_display_chars());
Source

pub fn insert_cols( &mut self, start_col: usize, count: usize, ) -> Result<(), Error>

Inserts one or more empty columns into the definition 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 maze definition with 2 rows and 4 columns, with a start, finish and a wall at the end of each row, insert 2 columns at the start of each row and print the result

use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
   vec!['S', ' ', ' ', 'W'],
   vec![' ', 'F', ' ', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
definition.insert_cols(0,2).expect("insert_cols() failed");
println!("{:?}", definition.to_display_chars());
Source

pub fn delete_rows( &mut self, start_row: usize, count: usize, ) -> Result<(), Error>

Deletes one or more consecutive rows from the definition 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 maze definition with 5 rows and 4 columns, delete the first and and second rows and print the result

use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
   vec!['W', ' ', ' ', 'W'],
   vec![' ', 'W', ' ', 'W'],
   vec![' ', ' ', 'W', 'W'],
   vec!['W', ' ', ' ', 'W'],
   vec![' ', 'W', ' ', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
definition.delete_rows(1,2).expect("delete_rows() failed");
println!("{:?}", definition.to_display_chars());
Source

pub fn insert_rows( &mut self, start_row: usize, count: usize, ) -> Result<(), Error>

Inserts one or more empty rows into the definition 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 maze definition with 5 rows and 4 columns, insert 2 rows after the fourth row and print the result

use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
   vec!['W', ' ', ' ', 'W'],
   vec![' ', 'W', ' ', 'W'],
   vec![' ', ' ', 'W', 'W'],
   vec!['W', ' ', ' ', 'W'],
   vec![' ', 'W', ' ', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
definition.insert_rows(3,2).expect("insert_rows() failed");
println!("{:?}", definition.to_display_chars());
Source

pub fn get_start(&self) -> Option<MazePoint>

Locates the starting position within the maze definition (if any)

§Returns

The starting position, else none

§Examples

Locate the starting position in a 2 row x 3 column definition

use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
   vec!['S', ' ', 'W'],
   vec![' ', 'F', 'W']
];
let definition = MazeDefinition::from_vec(grid);
match definition.get_start() {
    Some(start) => {
        println!("Start found at point {}", start);
    },
    None => {
        println!("Start not found");
    }
};
Source

pub fn set_start(&mut self, new_start: Option<MazePoint>) -> Result<(), Error>

Sets the starting position within the maze definition (if any)

§Returns

This function will return an error in the following situations:

  • If the new starting position is out of range
§Examples

Reset the starting position in a 2 row x 3 column definition

use data_model::MazeDefinition;
use data_model::MazePoint;
let grid: Vec<Vec<char>> = vec![
   vec!['S', ' ', 'W'],
   vec![' ', 'F', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
let new_start = MazePoint {row: 1, col: 2};
definition.set_start(Some(new_start)).expect("set_start() failed");
Source

pub fn get_finish(&self) -> Option<MazePoint>

Locates the finishing position within the maze definition (if any)

§Returns

The finishing position, else none

§Examples

Locate the finishing position in a 2 row x 3 column definition

use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
   vec!['S', ' ', 'W'],
   vec![' ', 'F', 'W']
];
let definition = MazeDefinition::from_vec(grid);
match definition.get_finish() {
    Some(finish) => {
        println!("Finish found at point {}", finish);
    },
    None => {
        println!("Finish not found");
    }
};
Source

pub fn set_finish(&mut self, new_finish: Option<MazePoint>) -> Result<(), Error>

Sets the finishing position within the maze definition (if any)

§Returns

This function will return an error in the following situations:

  • If the new finishing position is out of range
§Examples

Reset the finishing position in a 2 row x 3 column definition

use data_model::MazeDefinition;
use data_model::MazePoint;
let grid: Vec<Vec<char>> = vec![
   vec!['S', ' ', 'W'],
   vec![' ', 'F', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
let new_finish = MazePoint {row: 0, col: 2};
definition.set_start(Some(new_finish)).expect("new_finish() failed");
Source

pub fn set_value( &mut self, from: MazePoint, to: MazePoint, value: char, ) -> Result<(), Error>

Modify the value of each cell in a given region of the definition instance

§Arguments
  • from - Starting point of cell region to modify
  • to - Ending point of cell region to modify
  • value - Value to set. Must be either 'W' (wall) or ' ' (empty).
§Returns

This function will return an error in the following situations:

  • If the target points are out of range
  • if the character value is invalid
§Examples

Create a maze definition with 5 rows and 4 columns, then set the central region (1,1) to (3, 2) to be a wall and then print it

use data_model::MazeCellState;
use data_model::MazeDefinition;
use data_model::MazePoint;
let mut definition = MazeDefinition::new(5, 4);
let from = MazePoint { row: 1, col: 1, };
let to = MazePoint { row: 3, col: 2, };
definition.set_value( from, to, 'W').expect("set_value() failed");
println!("{:?}", definition.to_display_chars());

Trait Implementations§

Source§

impl Clone for MazeDefinition

Source§

fn clone(&self) -> MazeDefinition

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl ComposeSchema for MazeDefinition

Source§

fn compose(generics: Vec<RefOr<Schema>>) -> RefOr<Schema>

Source§

impl Debug for MazeDefinition

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for MazeDefinition

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for MazeDefinition

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl ToSchema for MazeDefinition

Source§

fn name() -> Cow<'static, str>

Return name of the schema. Read more
Source§

fn schemas(schemas: &mut Vec<(String, RefOr<Schema>)>)

Implement reference [utoipa::openapi::schema::Schema]s for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PartialSchema for T
where T: ComposeSchema + ?Sized,

§

fn schema() -> RefOr<Schema>

Return ref or schema of implementing type that can then be used to construct combined schemas.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,