1use std::ops::Add;
2
3#[derive(Eq, PartialEq, Copy, Clone, Debug)]
5pub enum Direction {
6 Up,
7 Down,
8 Left,
9 Right,
10 Center,
11}
12
13#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
15pub(super) struct Coord {
16 pub(super) x: u8,
17 pub(super) y: u8,
18}
19
20impl Coord {
21 pub fn in_bounds(self, size: u8) -> bool {
22 self.x < size && self.y < size
23 }
24}
25
26impl Add<Direction> for Coord {
27 type Output = Result<Self, &'static str>;
28
29 fn add(self, dir: Direction) -> Result<Self, &'static str> {
30 match dir {
31 Direction::Up => {
32 if self.y == 0 {
33 Err("Already at top row")
34 } else {
35 Ok(Coord {
36 x: self.x,
37 y: self.y - 1,
38 })
39 }
40 }
41 Direction::Down => Ok(Coord {
42 x: self.x,
43 y: self.y + 1,
44 }),
45 Direction::Left => {
46 if self.x == 0 {
47 Err("Already at left column")
48 } else {
49 Ok(Coord {
50 x: self.x - 1,
51 y: self.y,
52 })
53 }
54 }
55 Direction::Right => Ok(Coord {
56 x: self.x + 1,
57 y: self.y,
58 }),
59 Direction::Center => Ok(self),
60 }
61 }
62}