screeps/enums/action_error_codes/
flag_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(Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive)]
14pub enum FlagRemoveErrorCode {}
15
16impl FromReturnCode for FlagRemoveErrorCode {
17 type Error = Self;
18
19 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
20 let maybe_result = Self::try_result_from_i8(val);
21 #[cfg(feature = "unsafe-return-conversion")]
22 unsafe {
23 maybe_result.unwrap_unchecked()
24 }
25 #[cfg(not(feature = "unsafe-return-conversion"))]
26 maybe_result.unwrap()
27 }
28
29 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
30 match val {
31 0 => Some(Ok(())),
32 _ => None,
33 }
34 }
35}
36
37impl fmt::Display for FlagRemoveErrorCode {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 let msg: &'static str = "Zero-variant enum";
40
41 write!(f, "{}", msg)
42 }
43}
44
45impl Error for FlagRemoveErrorCode {}
46
47#[derive(
53 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
54)]
55#[repr(i8)]
56pub enum SetColorErrorCode {
57 InvalidArgs = -10,
58}
59
60impl FromReturnCode for SetColorErrorCode {
61 type Error = Self;
62
63 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
64 let maybe_result = Self::try_result_from_i8(val);
65 #[cfg(feature = "unsafe-return-conversion")]
66 unsafe {
67 maybe_result.unwrap_unchecked()
68 }
69 #[cfg(not(feature = "unsafe-return-conversion"))]
70 maybe_result.unwrap()
71 }
72
73 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
74 match val {
75 0 => Some(Ok(())),
76 -10 => Some(Err(SetColorErrorCode::InvalidArgs)),
77 _ => None,
78 }
79 }
80}
81
82impl fmt::Display for SetColorErrorCode {
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 let msg: &'static str = match self {
85 SetColorErrorCode::InvalidArgs => {
86 "color or secondarycolor is not a valid color constant"
87 }
88 };
89
90 write!(f, "{}", msg)
91 }
92}
93
94impl Error for SetColorErrorCode {}
95
96impl From<SetColorErrorCode> for ErrorCode {
97 fn from(value: SetColorErrorCode) -> Self {
98 Self::result_from_i8(value as i8).unwrap_err()
105 }
106}
107
108#[derive(
114 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
115)]
116#[repr(i8)]
117pub enum SetPositionErrorCode {
118 InvalidTarget = -7,
119}
120
121impl FromReturnCode for SetPositionErrorCode {
122 type Error = Self;
123
124 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
125 let maybe_result = Self::try_result_from_i8(val);
126 #[cfg(feature = "unsafe-return-conversion")]
127 unsafe {
128 maybe_result.unwrap_unchecked()
129 }
130 #[cfg(not(feature = "unsafe-return-conversion"))]
131 maybe_result.unwrap()
132 }
133
134 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
135 match val {
136 0 => Some(Ok(())),
137 -7 => Some(Err(SetPositionErrorCode::InvalidTarget)),
138 _ => None,
139 }
140 }
141}
142
143impl fmt::Display for SetPositionErrorCode {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 let msg: &'static str = match self {
146 SetPositionErrorCode::InvalidTarget => "the target provided is invalid",
147 };
148
149 write!(f, "{}", msg)
150 }
151}
152
153impl Error for SetPositionErrorCode {}
154
155impl From<SetPositionErrorCode> for ErrorCode {
156 fn from(value: SetPositionErrorCode) -> Self {
157 Self::result_from_i8(value as i8).unwrap_err()
164 }
165}