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
use std::fmt::{self, Debug};
use thiserror::Error;
#[derive(Error)]
#[error("ship with id {id:?} already exists")]
pub struct AddShipError<I: Debug, S> {
id: I,
shape: S,
}
impl<I: Debug, S> AddShipError<I, S> {
pub(super) fn new(id: I, shape: S) -> Self {
Self { id, shape }
}
pub fn id(&self) -> &I {
&self.id
}
pub fn shape(&self) -> &S {
&self.shape
}
pub fn into_inner(self) -> (I, S) {
(self.id, self.shape)
}
}
impl<I: Debug, S> From<AddShipError<I, S>> for (I, S) {
fn from(err: AddShipError<I, S>) -> Self {
err.into_inner()
}
}
impl<I: Debug, S> Debug for AddShipError<I, S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
#[derive(Debug, Error, Copy, Clone, Eq, PartialEq)]
pub enum CannotPlaceReason {
#[error("ship was already placed")]
AlreadyPlaced,
#[error("the projection provided was not valid")]
InvalidProjection,
#[error("the requested position was already occupied")]
AlreadyOccupied,
}
#[derive(Error)]
#[error("could not place ship: {reason:?}")]
pub struct PlaceError<P> {
#[source]
reason: CannotPlaceReason,
placement: P,
}
impl<P> Debug for PlaceError<P> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl<P> PlaceError<P> {
pub(super) fn new(reason: CannotPlaceReason, placement: P) -> Self {
Self { reason, placement }
}
pub fn reason(&self) -> CannotPlaceReason {
self.reason
}
pub fn placement(&self) -> &P {
&self.placement
}
pub fn into_placement(self) -> P {
self.placement
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum CannotShootReason {
AlreadyDefeated,
OutOfBounds,
AlreadyShot,
}
#[derive(Debug, Error)]
#[error("could not shoot cell {coord:?}: {reason:?}")]
pub struct ShotError<C: Debug> {
reason: CannotShootReason,
coord: C,
}
impl<C: Debug> ShotError<C> {
pub(super) fn new(reason: CannotShootReason, coord: C) -> Self {
Self { reason, coord }
}
pub fn reason(&self) -> CannotShootReason {
self.reason
}
pub fn coord(&self) -> &C {
&self.coord
}
pub fn into_coord(self) -> C {
self.coord
}
}