screeps/enums/action_error_codes/
spawning_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 CancelErrorCode {
18 NotOwner = -1,
19}
20
21impl FromReturnCode for CancelErrorCode {
22 type Error = Self;
23
24 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
25 let maybe_result = Self::try_result_from_i8(val);
26 #[cfg(feature = "unsafe-return-conversion")]
27 unsafe {
28 maybe_result.unwrap_unchecked()
29 }
30 #[cfg(not(feature = "unsafe-return-conversion"))]
31 maybe_result.unwrap()
32 }
33
34 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
35 match val {
36 0 => Some(Ok(())),
37 -1 => Some(Err(CancelErrorCode::NotOwner)),
38 _ => None,
39 }
40 }
41}
42
43impl fmt::Display for CancelErrorCode {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 let msg: &'static str = match self {
46 CancelErrorCode::NotOwner => "you are not the owner of this spawn",
47 };
48
49 write!(f, "{}", msg)
50 }
51}
52
53impl Error for CancelErrorCode {}
54
55impl From<CancelErrorCode> for ErrorCode {
56 fn from(value: CancelErrorCode) -> Self {
57 Self::result_from_i8(value as i8).unwrap_err()
64 }
65}
66
67#[derive(
74 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
75)]
76#[repr(i8)]
77pub enum SetDirectionsErrorCode {
78 NotOwner = -1,
79 InvalidArgs = -10,
80}
81
82impl FromReturnCode for SetDirectionsErrorCode {
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 0 => Some(Ok(())),
98 -1 => Some(Err(SetDirectionsErrorCode::NotOwner)),
99 -10 => Some(Err(SetDirectionsErrorCode::InvalidArgs)),
100 _ => None,
101 }
102 }
103}
104
105impl fmt::Display for SetDirectionsErrorCode {
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 let msg: &'static str = match self {
108 SetDirectionsErrorCode::NotOwner => "you are not the owner of this spawn",
109 SetDirectionsErrorCode::InvalidArgs => "the directions is array is invalid",
110 };
111
112 write!(f, "{}", msg)
113 }
114}
115
116impl Error for SetDirectionsErrorCode {}
117
118impl From<SetDirectionsErrorCode> for ErrorCode {
119 fn from(value: SetDirectionsErrorCode) -> Self {
120 Self::result_from_i8(value as i8).unwrap_err()
127 }
128}