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
//! An action.
use std::fmt;
use crate::{direction::Direction, error::ParseActionError};
/// Represents an action.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
pub enum Action {
/// Move action in a specified direction.
Move(Direction),
/// Push action in a specified direction.
Push(Direction),
}
impl Action {
/// Returns the direction associated with the action.
///
/// # Examples
///
/// ```
/// use soukoban::prelude::*;
///
/// let action = Action::Move(Direction::Up);
/// assert_eq!(action.direction(), Direction::Up);
/// ```
pub const fn direction(&self) -> Direction {
match *self {
Self::Move(direction) | Self::Push(direction) => direction,
}
}
/// Checks if the action is a move action.
///
/// # Examples
///
/// ```
/// use soukoban::prelude::*;
///
/// let action = Action::Move(Direction::Up);
/// assert!(action.is_move());
/// ```
pub const fn is_move(&self) -> bool {
matches!(&self, Self::Move(_))
}
/// Checks if the action is a shift action.
///
/// # Examples
///
/// ```
/// use soukoban::prelude::*;
///
/// let action = Action::Push(Direction::Up);
/// assert!(action.is_shift());
/// ```
pub const fn is_shift(&self) -> bool {
matches!(&self, Self::Push(_))
}
/// Rotates the action's direction 90° clockwise.
///
/// # Examples
///
/// ```
/// use soukoban::prelude::*;
///
/// let action = Action::Move(Direction::Up);
/// assert_eq!(action.rotate_cw(), Action::Move(Direction::Right));
/// ```
pub fn rotate_cw(self) -> Self {
self.map(Direction::rotate_cw)
}
/// Rotates the action's direction 90° counter-clockwise.
///
/// # Examples
///
/// ```
/// use soukoban::prelude::*;
///
/// let action = Action::Move(Direction::Up);
/// assert_eq!(action.rotate_ccw(), Action::Move(Direction::Left));
/// ```
pub fn rotate_ccw(self) -> Self {
self.map(Direction::rotate_ccw)
}
/// Flips the action's direction horizontally.
///
/// # Examples
///
/// ```
/// use soukoban::prelude::*;
///
/// let action = Action::Move(Direction::Left);
/// assert_eq!(action.flip_horizontal(), Action::Move(Direction::Right));
/// ```
pub fn flip_horizontal(self) -> Self {
self.map(Direction::flip_horizontal)
}
/// Flips the action's direction vertically.
///
/// # Examples
///
/// ```
/// use soukoban::prelude::*;
///
/// let action = Action::Move(Direction::Up);
/// assert_eq!(action.flip_vertical(), Action::Move(Direction::Down));
/// ```
pub fn flip_vertical(self) -> Self {
self.map(Direction::flip_vertical)
}
/// Applies a transformation function to the `Direction`.
fn map(self, f: impl Fn(Direction) -> Direction) -> Self {
match self {
Self::Move(direction) => Self::Move(f(direction)),
Self::Push(direction) => Self::Push(f(direction)),
}
}
}
impl TryFrom<char> for Action {
type Error = ParseActionError;
fn try_from(char: char) -> Result<Self, ParseActionError> {
let direction = match char.to_ascii_lowercase() {
'u' => Direction::Up,
'd' => Direction::Down,
'l' => Direction::Left,
'r' => Direction::Right,
_ => return Err(ParseActionError::InvalidCharacter(char)),
};
if char.is_ascii_uppercase() {
Ok(Self::Push(direction))
} else {
Ok(Self::Move(direction))
}
}
}
impl From<Action> for char {
fn from(action: Action) -> Self {
let char = match action.direction() {
Direction::Up => 'u',
Direction::Down => 'd',
Direction::Left => 'l',
Direction::Right => 'r',
};
if action.is_shift() {
char.to_ascii_uppercase()
} else {
char
}
}
}
impl fmt::Display for Action {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", char::from(*self))
}
}