1use crate::error::{CoreError, DatabaseError, Error};
5use axum::response::{IntoResponse, Response};
6use either::Either;
7use nil_server_database::error::DieselError;
8use std::ops::{ControlFlow, Try};
9
10pub type MaybeResponse<L> = Either<L, Response>;
11
12#[doc(hidden)]
13#[macro_export]
14macro_rules! res {
15 ($status:ident) => {{
16 use axum::body::Body;
17 use axum::http::StatusCode;
18 use axum::response::Response;
19
20 let status = StatusCode::$status;
21 let body = if (status.is_client_error() || status.is_server_error())
22 && let Some(reason) = status.canonical_reason()
23 {
24 Body::new(reason.to_string())
25 } else {
26 Body::empty()
27 };
28
29 Response::builder()
30 .status(status)
31 .body(body)
32 .unwrap()
33 }};
34 ($status:ident, $data:expr) => {{
35 use axum::http::StatusCode;
36 use axum::response::IntoResponse;
37
38 (StatusCode::$status, $data).into_response()
39 }};
40}
41
42impl From<Error> for Response {
43 fn from(err: Error) -> Self {
44 from_err(err)
45 }
46}
47
48impl IntoResponse for Error {
49 fn into_response(self) -> Response {
50 from_err(self)
51 }
52}
53
54pub(crate) fn from_err(err: impl Into<Error>) -> Response {
55 let err: Error = err.into();
56 tracing::error!(message = %err, error = ?err);
57 from_server_err(err)
58}
59
60#[expect(clippy::match_same_arms, clippy::needless_pass_by_value)]
61fn from_core_err(err: CoreError) -> Response {
62 use CoreError::*;
63
64 let text = err.to_string();
65 match err {
66 ArmyNotFound(..) => res!(NOT_FOUND, text),
67 ArmyNotIdle(..) => res!(BAD_REQUEST, text),
68 BotAlreadySpawned(..) => res!(CONFLICT, text),
69 BotNotFound(..) => res!(NOT_FOUND, text),
70 BuildingStatsNotFound(..) => res!(NOT_FOUND, text),
71 BuildingStatsNotFoundForLevel(..) => res!(NOT_FOUND, text),
72 CannotDecreaseBuildingLevel(..) => res!(BAD_REQUEST, text),
73 CannotIncreaseBuildingLevel(..) => res!(BAD_REQUEST, text),
74 CheatingNotAllowed => res!(BAD_REQUEST, text),
75 CityNotFound(..) => res!(NOT_FOUND, text),
76 FailedToDeserializeEvent => res!(INTERNAL_SERVER_ERROR, text),
77 FailedToReadSavedata => res!(INTERNAL_SERVER_ERROR, text),
78 FailedToSerializeEvent => res!(INTERNAL_SERVER_ERROR, text),
79 FailedToWriteSavedata => res!(INTERNAL_SERVER_ERROR, text),
80 Forbidden => res!(FORBIDDEN, text),
81 IndexOutOfBounds(..) => res!(BAD_REQUEST, text),
82 InsufficientResources => res!(BAD_REQUEST, text),
83 InsufficientUnits => res!(BAD_REQUEST, text),
84 ManeuverIsDone(..) => res!(BAD_REQUEST, text),
85 ManeuverIsPending(..) => res!(BAD_REQUEST, text),
86 ManeuverIsReturning(..) => res!(BAD_REQUEST, text),
87 ManeuverNotFound(..) => res!(NOT_FOUND, text),
88 MineStatsNotFound(..) => res!(NOT_FOUND, text),
89 MineStatsNotFoundForLevel(..) => res!(NOT_FOUND, text),
90 NoPlayer => res!(BAD_REQUEST, text),
91 NotWaitingPlayer(..) => res!(BAD_REQUEST, text),
92 OriginIsDestination(..) => res!(BAD_REQUEST, text),
93 PlayerAlreadySpawned(..) => res!(CONFLICT, text),
94 PlayerNotFound(..) => res!(NOT_FOUND, text),
95 PrecursorNotFound(..) => res!(NOT_FOUND, text),
96 ReportNotFound(..) => res!(NOT_FOUND, text),
97 RoundAlreadyStarted => res!(CONFLICT, text),
98 RoundHasPendingPlayers => res!(BAD_REQUEST, text),
99 RoundNotStarted => res!(BAD_REQUEST, text),
100 StorageStatsNotFound(..) => res!(NOT_FOUND, text),
101 StorageStatsNotFoundForLevel(..) => res!(NOT_FOUND, text),
102 UnexpectedUnit(..) => res!(BAD_REQUEST, text),
103 WallStatsNotFoundForLevel(..) => res!(NOT_FOUND, text),
104 WorldIsFull => res!(FORBIDDEN, text),
105 }
106}
107
108#[expect(clippy::match_same_arms)]
109fn from_database_err(err: DatabaseError) -> Response {
110 use DatabaseError::*;
111
112 match err {
113 Core(err) => from_core_err(err),
114 Diesel(err) => from_diesel_err(&err),
115 DieselConnection(..) => res!(INTERNAL_SERVER_ERROR),
116 GameNotFound(..) => res!(NOT_FOUND, err.to_string()),
117 InvalidPassword => res!(BAD_REQUEST, err.to_string()),
118 InvalidUsername(..) => res!(BAD_REQUEST, err.to_string()),
119 Io(..) => res!(INTERNAL_SERVER_ERROR),
120 Jiff(..) => res!(INTERNAL_SERVER_ERROR),
121 MigrationFailed(..) => res!(INTERNAL_SERVER_ERROR),
122 UserAlreadyExists(..) => res!(CONFLICT, err.to_string()),
123 UserNotFound(..) => res!(NOT_FOUND, err.to_string()),
124 Unknown(..) => res!(INTERNAL_SERVER_ERROR),
125 }
126}
127
128fn from_diesel_err(err: &DieselError) -> Response {
129 if let DieselError::NotFound = &err {
130 res!(NOT_FOUND)
131 } else {
132 res!(INTERNAL_SERVER_ERROR)
133 }
134}
135
136#[expect(clippy::match_same_arms)]
137fn from_server_err(err: Error) -> Response {
138 use Error::*;
139
140 match err {
141 Core(err) => from_core_err(err),
142 Database(err) => from_database_err(err),
143 IncorrectUserCredentials => res!(UNAUTHORIZED, err.to_string()),
144 IncorrectWorldCredentials(..) => res!(UNAUTHORIZED, err.to_string()),
145 Io(..) => res!(INTERNAL_SERVER_ERROR),
146 MaxCharactersExceeded { .. } => res!(BAD_REQUEST, err.to_string()),
147 MissingPassword => res!(BAD_REQUEST, err.to_string()),
148 Unknown(..) => res!(INTERNAL_SERVER_ERROR),
149 WorldLimitReached => res!(FORBIDDEN, err.to_string()),
150 WorldNotFound(..) => res!(NOT_FOUND, err.to_string()),
151 }
152}
153
154pub trait EitherExt<L, R> {
155 fn try_map_left<T, E, F>(self, f: F) -> Either<Response, R>
156 where
157 Self: Sized,
158 L: Try<Output = T, Residual = E>,
159 E: Into<Error>,
160 F: FnOnce(T) -> Response;
161}
162
163impl<L, R> EitherExt<L, R> for Either<L, R> {
164 fn try_map_left<T, E, F>(self, f: F) -> Either<Response, R>
165 where
166 Self: Sized,
167 L: Try<Output = T, Residual = E>,
168 E: Into<Error>,
169 F: FnOnce(T) -> Response,
170 {
171 match self {
172 Self::Left(left) => {
173 match left.branch() {
174 ControlFlow::Continue(value) => Either::Left(f(value)),
175 ControlFlow::Break(err) => Either::Left(from_err(err)),
176 }
177 }
178 Self::Right(right) => Either::Right(right),
179 }
180 }
181}
182
183#[doc(hidden)]
184#[macro_export]
185macro_rules! bail_if_city_is_not_owned_by {
186 ($world:expr, $player:expr, $coord:expr) => {
187 if !$world
188 .city($coord)?
189 .is_owned_by_player_and(|id| $player == id)
190 {
191 return $crate::res!(FORBIDDEN);
192 }
193 };
194}
195
196#[doc(hidden)]
197#[macro_export]
198macro_rules! bail_if_max_chars_exceeded {
199 ($value:expr, $max:expr) => {
200 let current = $value.chars().count();
201 if current > $max {
202 use $crate::error::Error;
203 let err = Error::MaxCharactersExceeded { max: $max, current };
204 return $crate::response::from_err(err);
205 }
206 };
207}
208
209#[doc(hidden)]
210#[macro_export]
211macro_rules! bail_if_player_is_not_pending {
212 ($world:expr, $player:expr) => {
213 if !$world.round().is_waiting_player($player) {
214 use nil_core::error::Error;
215 let err = Error::NotWaitingPlayer($player.clone());
216 return $crate::response::from_err(err);
217 }
218 };
219}
220
221#[doc(hidden)]
222#[macro_export]
223macro_rules! bail_if_player_ne {
224 ($current_player:expr, $player:expr) => {
225 if $current_player != $player {
226 return $crate::res!(FORBIDDEN);
227 }
228 };
229}