screeps/enums/action_error_codes/
structure_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 DestroyErrorCode {
18 NotOwner = -1,
19 Busy = -4,
20 InvalidTarget = -7,
21}
22
23impl FromReturnCode for DestroyErrorCode {
24 type Error = Self;
25
26 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
27 let maybe_result = Self::try_result_from_i8(val);
28 #[cfg(feature = "unsafe-return-conversion")]
29 unsafe {
30 maybe_result.unwrap_unchecked()
31 }
32 #[cfg(not(feature = "unsafe-return-conversion"))]
33 maybe_result.unwrap()
34 }
35
36 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
37 match val {
38 0 => Some(Ok(())),
39 -1 => Some(Err(DestroyErrorCode::NotOwner)),
40 -4 => Some(Err(DestroyErrorCode::Busy)),
41 -7 => Some(Err(DestroyErrorCode::InvalidTarget)),
42 _ => None,
43 }
44 }
45}
46
47impl fmt::Display for DestroyErrorCode {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 let msg: &'static str = match self {
50 DestroyErrorCode::NotOwner => {
51 "you are not the owner of this structure, and it's not in your room"
52 }
53 DestroyErrorCode::Busy => "hostile creeps are in the room",
54 DestroyErrorCode::InvalidTarget => "room property invalid",
55 };
56
57 write!(f, "{}", msg)
58 }
59}
60
61impl Error for DestroyErrorCode {}
62
63impl From<DestroyErrorCode> for ErrorCode {
64 fn from(value: DestroyErrorCode) -> Self {
65 Self::result_from_i8(value as i8).unwrap_err()
72 }
73}
74
75#[derive(
82 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
83)]
84#[repr(i8)]
85pub enum StructureNotifyWhenAttackedErrorCode {
86 NotOwner = -1,
87 InvalidTarget = -7,
88 InvalidArgs = -10,
89}
90
91impl FromReturnCode for StructureNotifyWhenAttackedErrorCode {
92 type Error = Self;
93
94 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
95 let maybe_result = Self::try_result_from_i8(val);
96 #[cfg(feature = "unsafe-return-conversion")]
97 unsafe {
98 maybe_result.unwrap_unchecked()
99 }
100 #[cfg(not(feature = "unsafe-return-conversion"))]
101 maybe_result.unwrap()
102 }
103
104 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
105 match val {
106 0 => Some(Ok(())),
107 -1 => Some(Err(StructureNotifyWhenAttackedErrorCode::NotOwner)),
108 -7 => Some(Err(StructureNotifyWhenAttackedErrorCode::InvalidTarget)),
109 -10 => Some(Err(StructureNotifyWhenAttackedErrorCode::InvalidArgs)),
110 _ => None,
111 }
112 }
113}
114
115impl fmt::Display for StructureNotifyWhenAttackedErrorCode {
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 let msg: &'static str = match self {
118 StructureNotifyWhenAttackedErrorCode::NotOwner => {
119 "you are not the owner of this structure"
120 }
121 StructureNotifyWhenAttackedErrorCode::InvalidTarget => "room property invalid",
122 StructureNotifyWhenAttackedErrorCode::InvalidArgs => {
123 "enable argument is not a boolean value"
124 }
125 };
126
127 write!(f, "{}", msg)
128 }
129}
130
131impl Error for StructureNotifyWhenAttackedErrorCode {}
132
133impl From<StructureNotifyWhenAttackedErrorCode> for ErrorCode {
134 fn from(value: StructureNotifyWhenAttackedErrorCode) -> Self {
135 Self::result_from_i8(value as i8).unwrap_err()
142 }
143}