screeps/enums/action_error_codes/
game_map_error_codes.rs1use std::{error::Error, fmt};
2
3use num_derive::FromPrimitive;
4use serde_repr::{Deserialize_repr, Serialize_repr};
5
6use crate::{constants::ErrorCode, FromReturnCode};
7
8#[derive(
14 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
15)]
16#[repr(i8)]
17pub enum FindExitErrorCode {
18 NoPath = -2,
19 InvalidArgs = -10,
20}
21
22impl FromReturnCode for FindExitErrorCode {
23 type Error = Self;
24
25 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
26 let maybe_result = Self::try_result_from_i8(val);
27 #[cfg(feature = "unsafe-return-conversion")]
28 unsafe {
29 maybe_result.unwrap_unchecked()
30 }
31 #[cfg(not(feature = "unsafe-return-conversion"))]
32 maybe_result.unwrap()
33 }
34
35 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
36 match val {
37 -2 => Some(Err(FindExitErrorCode::NoPath)),
38 -10 => Some(Err(FindExitErrorCode::InvalidArgs)),
39 _ => None,
40 }
41 }
42}
43
44impl fmt::Display for FindExitErrorCode {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 let msg: &'static str = match self {
47 FindExitErrorCode::NoPath => "path can not be found",
48 FindExitErrorCode::InvalidArgs => "the location is incorrect",
49 };
50
51 write!(f, "{}", msg)
52 }
53}
54
55impl Error for FindExitErrorCode {}
56
57impl From<FindExitErrorCode> for ErrorCode {
58 fn from(value: FindExitErrorCode) -> Self {
59 Self::result_from_i8(value as i8).unwrap_err()
66 }
67}
68
69#[derive(
75 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
76)]
77#[repr(i8)]
78pub enum FindRouteErrorCode {
79 NoPath = -2,
80}
81
82impl FromReturnCode for FindRouteErrorCode {
83 type Error = Self;
84
85 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
86 let maybe_result = Self::try_result_from_i8(val);
87 #[cfg(feature = "unsafe-return-conversion")]
88 unsafe {
89 maybe_result.unwrap_unchecked()
90 }
91 #[cfg(not(feature = "unsafe-return-conversion"))]
92 maybe_result.unwrap()
93 }
94
95 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
96 match val {
97 -2 => Some(Err(FindRouteErrorCode::NoPath)),
98 _ => None,
99 }
100 }
101}
102
103impl fmt::Display for FindRouteErrorCode {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 let msg: &'static str = match self {
106 FindRouteErrorCode::NoPath => "path can not be found",
107 };
108
109 write!(f, "{}", msg)
110 }
111}
112
113impl Error for FindRouteErrorCode {}
114
115impl From<FindRouteErrorCode> for ErrorCode {
116 fn from(value: FindRouteErrorCode) -> Self {
117 Self::result_from_i8(value as i8).unwrap_err()
124 }
125}