data_model/
maze.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use crate::Error;
use crate::MazeDefinition;

#[allow(dead_code)]
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
/// Represents a maze
pub struct Maze {
    pub id: String,
    pub name: String,
    /// MazeDefinition, containing the layout of the maze
    pub definition: MazeDefinition,
}

impl PartialEq for Maze {
    fn eq(&self, other: &Self) -> bool {
        if self.id != other.id {
            return false;
        }

        if let (Ok(self_json), Ok(other_json)) = (self.to_json(), other.to_json()) {
            return self_json == other_json;
        }

        false
    }
}

impl 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);
    pub fn new(definition: MazeDefinition) -> Maze {
        Maze {
            id: "".to_string(),
            name: "".to_string(),
            definition,
        }
    }
    /// 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);
    /// ```
    pub fn reset(&mut self) -> &mut Self {
        self.definition.reset();
        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);
    pub fn from_vec(grid: Vec<Vec<char>>) -> Self {
        Maze {
            id: "".to_string(),
            name: "".to_string(),
            definition: MazeDefinition::from_vec(grid),
        }
    }
    /// 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
    ///        );
    ///     }
    /// }
    pub fn to_json(&self) -> Result<String, Error> {
        Ok(serde_json::to_string(&self)?)
    }
    /// 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
    ///        );
    ///     }
    /// }
    pub fn from_json(&mut self, json: &str) -> Result<(), Error> {
        let temp: Maze = serde_json::from_str(json)?;
        *self = temp;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_create_new_maze_from_vector() {
        #[rustfmt::skip]
        let grid: Vec<Vec<char>> = vec![
            vec![' ', ' ', ' '],
            vec![' ', ' ', ' ']
        ];
        let maze = Maze::from_vec(grid);
        assert_eq!(maze.definition.row_count(), 2);
        assert_eq!(maze.definition.col_count(), 3);
    }

    #[test]
    #[should_panic(
        expected = "grid vector contains rows with different numbers of columns (expected 3 for all rows)"
    )]
    fn cannot_create_new_from_vector_with_diff_row_counts() {
        #[rustfmt::skip]
        let grid: Vec<Vec<char>> = vec![
            vec![' ', ' ', ' '],
            vec![' ', ' ', ' ', ' ']
        ];
        let _d = MazeDefinition::from_vec(grid);
    }

    #[test]
    fn can_create_new_from_definition() {
        let maze = Maze::new(MazeDefinition::new(2, 3));
        assert_eq!(maze.definition.row_count(), 2);
        assert_eq!(maze.definition.col_count(), 3);
    }

    #[test]
    fn can_reset_to_empty() {
        let mut maze = Maze::new(MazeDefinition::new(2, 3));
        assert_eq!(maze.definition.row_count(), 2);
        assert_eq!(maze.definition.col_count(), 3);
        assert!(!maze.definition.is_empty());
        assert!(maze.reset().definition.is_empty())
    }

    #[test]
    fn can_serialize_empty() {
        let maze = Maze::new(MazeDefinition::new(0, 0));
        let s = maze.to_json().expect("Failed to serialize");
        assert_eq!(s, r#"{"id":"","name":"","definition":{"grid":[]}}"#);
    }

    #[test]
    fn can_serialize_non_empty() {
        #[rustfmt::skip]
        let grid: Vec<Vec<char>> = vec![
            vec![' ', 'W', ' '],
            vec![' ', ' ', 'W']
        ];
        let maze = Maze::new(MazeDefinition::from_vec(grid));
        let s = maze.to_json().expect("Failed to serialize");
        assert_eq!(
            s,
            r#"{"id":"","name":"","definition":{"grid":[[" ","W"," "],[" "," ","W"]]}}"#
        );
    }

    #[test]
    fn can_deserialize_empty() {
        let mut maze = Maze::new(MazeDefinition::new(10, 10));
        let s = r#"{"id":"maze_id", "name":"maze_name","definition":{"grid":[]}}"#;
        maze.from_json(s).expect("Failed to deserialize");
        assert!(maze.definition.is_empty());
    }

    #[test]
    fn can_deserialize_non_empty() {
        let mut maze = Maze::new(MazeDefinition::new(10, 10));
        let s = r#"{"id":"maze_id", "name":"maze_name","definition":{"grid":[[" ","W"," "],[" "," ","W"]]}}"#;
        maze.from_json(s).expect("Failed to deserialize");
        assert_eq!(maze.definition.row_count(), 2);
        assert_eq!(maze.definition.col_count(), 3);
    }

    #[test]
    fn cannot_load_json_with_invalid_content_eof() {
        run_from_json_test_with_invalid_content("{", ExpectedSerdeErrorKind::UnexpectedEof);
    }

    #[test]
    fn cannot_load_json_with_invalid_content_syntax_1() {
        run_from_json_test_with_invalid_content("{x", ExpectedSerdeErrorKind::Syntax);
    }

    #[test]
    fn cannot_load_json_with_invalid_content_syntax_2() {
        run_from_json_test_with_invalid_content(r#"{"x"}"#, ExpectedSerdeErrorKind::Syntax);
    }

    #[test]
    fn cannot_load_json_with_invalid_content_syntax_3() {
        run_from_json_test_with_invalid_content(r#"{"x":}"#, ExpectedSerdeErrorKind::Syntax);
    }

    #[test]
    fn cannot_load_json_with_invalid_content_syntax_4() {
        run_from_json_test_with_invalid_content("}", ExpectedSerdeErrorKind::Syntax);
    }

    #[test]
    fn cannot_load_json_with_invalid_content_syntax_5() {
        run_from_json_test_with_invalid_content("{{}", ExpectedSerdeErrorKind::Syntax);
    }

    #[test]
    fn cannot_load_json_with_invalid_content_data_1() {
        run_from_json_test_with_invalid_content(
            r#"{"definition1":{"grid":[[" ","W"," "],[" "," ","W"]]}}"#,
            ExpectedSerdeErrorKind::Data,
        );
    }

    #[test]
    fn cannot_load_json_with_invalid_content_data_2() {
        run_from_json_test_with_invalid_content(
            r#"{"definition":{"grid2":[[" ","W"," "],[" "," ","W"]]}}"#,
            ExpectedSerdeErrorKind::Data,
        );
    }

    #[test]
    fn cannot_load_json_with_invalid_content_data_3() {
        run_from_json_test_with_invalid_content(
            r#"{"definition":{"grid":"invalid data"}}"#,
            ExpectedSerdeErrorKind::Data,
        );
    }

    // Helper functions and definitions
    enum ExpectedSerdeErrorKind {
        Data,
        Syntax,
        UnexpectedEof,
    }

    fn run_from_json_test_with_invalid_content(
        content: &str,
        expected_error_kind: ExpectedSerdeErrorKind,
    ) {
        let mut maze = Maze::new(MazeDefinition::new(0, 0));
        match maze.from_json(content) {
            Ok(_) => {
                panic!("Unexpectedly loaded json despite having invalid content");
            }
            Err(error) => match error {
                Error::Serialization(ref serdejson_error) => match expected_error_kind {
                    ExpectedSerdeErrorKind::Data => {
                        if !serdejson_error.is_data() {
                            panic!(
                                "Serde data error expected (got SerdeJson error: {})",
                                serdejson_error
                            );
                        }
                    }
                    ExpectedSerdeErrorKind::Syntax => {
                        if !serdejson_error.is_syntax() {
                            panic!(
                                "Serde syntax error expected (got SerdeJson error: {})",
                                serdejson_error
                            );
                        }
                    }
                    ExpectedSerdeErrorKind::UnexpectedEof => {
                        if !serdejson_error.is_eof() {
                            panic!(
                                "Serde unexpected EOF error expected (got SerdeJson error: {})",
                                serdejson_error
                            );
                        }
                    }
                },
                _ => panic!("Unxpected error encountered (got error: {})", error),
            },
        }
    }
}