data_model

Struct Maze

Source
pub struct Maze {
    pub id: String,
    pub name: String,
    pub definition: MazeDefinition,
}
Expand description

Represents a maze

Fields§

§id: String§name: String§definition: MazeDefinition

MazeDefinition, containing the layout of the maze

Implementations§

Source§

impl Maze

Source

pub fn new(definition: MazeDefinition) -> Maze

Creates a new maze instance with the given definition

§Arguments
  • definition - Maze definition
§Returns

A new maze 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;
use data_model::Maze;
let grid: Vec<Vec<char>> = vec![
   vec!['S', ' ', 'W'],
   vec![' ', 'F', 'W']
];
let def = MazeDefinition::from_vec(grid);
let maze = Maze::new(def);
assert_eq!(maze.definition.row_count(), 2);
assert_eq!(maze.definition.col_count(), 3);
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 2 rows and 3 columns, verify its dimensions, reset it and then confirm it is empty

use data_model::Maze;
let grid: Vec<Vec<char>> = vec![
   vec!['S', ' ', 'W'],
   vec![' ', 'F', 'W']
];
let mut maze = Maze::from_vec(grid);
assert_eq!(maze.definition.row_count(), 2);
assert_eq!(maze.definition.col_count(), 3);
maze.reset();
assert_eq!(maze.definition.is_empty(), true);
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 maze instance

§Examples

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

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

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

Generates the JSON string representation for the maze

§Returns

JSON string representing the maze definition

§Examples

Create a 2 row x 3 column definition with a start, finish and a wall in the last column and then convert it to JSON and print it

use data_model::Maze;
let grid: Vec<Vec<char>> = vec![
   vec!['S', ' ', 'W'],
   vec![' ', 'F', 'W']
];
let maze = Maze::from_vec(grid);
assert_eq!(maze.definition.row_count(), 2);
assert_eq!(maze.definition.col_count(), 3);
match maze.to_json() {
    Ok(json) => {
        println!("JSON: {}", json);
    }
    Err(error) => {
       panic!(
           "failed to convert maze to JSON => {}",
          error
       );
    }
}
Source

pub fn from_json(&mut self, json: &str) -> Result<(), Error>

Initializes a 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 an empty maze and then reinitialize it from a JSON string definition containing 2 rows and 3 columns

use data_model::MazeDefinition;
use data_model::Maze;
let mut maze = Maze::new(MazeDefinition::new(0, 0));
let json = r#"{"id":"maze_id","name":"maze_name", "definition":{"grid":[[" ","W"," "],[" "," ","W"]]}}"#;
match maze.from_json(json) {
    Ok(()) => {
        println!(
            "JSON successfully read into Maze => new rows = {}, new columns = {}",
            maze.definition.row_count(),
            maze.definition.col_count()
        );
    }
    Err(error) => {
       panic!(
           "failed to read JSON into maze => {}",
          error
       );
    }
}

Trait Implementations§

Source§

impl Clone for Maze

Source§

fn clone(&self) -> Maze

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 Maze

Source§

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

Source§

impl Debug for Maze

Source§

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

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

impl<'de> Deserialize<'de> for Maze

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 PartialEq for Maze

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Maze

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 Maze

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§

§

impl Freeze for Maze

§

impl RefUnwindSafe for Maze

§

impl Send for Maze

§

impl Sync for Maze

§

impl Unpin for Maze

§

impl UnwindSafe for Maze

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>,