screeps/enums/action_error_codes/
roomposition_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(
16 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
17)]
18#[repr(i8)]
19pub enum RoomPositionCreateConstructionSiteErrorCode {
20 NotOwner = -1,
21 InvalidTarget = -7,
22 Full = -8,
23 NotInRange = -9,
24 InvalidArgs = -10,
25 RclNotEnough = -14,
26}
27
28impl FromReturnCode for RoomPositionCreateConstructionSiteErrorCode {
29 type Error = Self;
30
31 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
32 let maybe_result = Self::try_result_from_i8(val);
33 #[cfg(feature = "unsafe-return-conversion")]
34 unsafe {
35 maybe_result.unwrap_unchecked()
36 }
37 #[cfg(not(feature = "unsafe-return-conversion"))]
38 maybe_result.unwrap()
39 }
40
41 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
42 match val {
43 0 => Some(Ok(())),
44 -1 => Some(Err(RoomPositionCreateConstructionSiteErrorCode::NotOwner)),
45 -7 => Some(Err(
46 RoomPositionCreateConstructionSiteErrorCode::InvalidTarget,
47 )),
48 -8 => Some(Err(RoomPositionCreateConstructionSiteErrorCode::Full)),
49 -9 => Some(Err(RoomPositionCreateConstructionSiteErrorCode::NotInRange)),
50 -10 => Some(Err(
51 RoomPositionCreateConstructionSiteErrorCode::InvalidArgs,
52 )),
53 -14 => Some(Err(
54 RoomPositionCreateConstructionSiteErrorCode::RclNotEnough,
55 )),
56 _ => None,
57 }
58 }
59}
60
61impl fmt::Display for RoomPositionCreateConstructionSiteErrorCode {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 let msg: &'static str = match self {
64 RoomPositionCreateConstructionSiteErrorCode::NotOwner => "the room is claimed or reserved by a hostile player",
65 RoomPositionCreateConstructionSiteErrorCode::InvalidTarget => "the structure cannot be placed at the specified location",
66 RoomPositionCreateConstructionSiteErrorCode::Full => "you have too many construction sites. the maximum number of construction sites per player is 100",
67 RoomPositionCreateConstructionSiteErrorCode::NotInRange => "room not visible",
68 RoomPositionCreateConstructionSiteErrorCode::InvalidArgs => "the location is incorrect",
69 RoomPositionCreateConstructionSiteErrorCode::RclNotEnough => "room controller level insufficient. learn more",
70 };
71
72 write!(f, "{}", msg)
73 }
74}
75
76impl Error for RoomPositionCreateConstructionSiteErrorCode {}
77
78impl From<RoomPositionCreateConstructionSiteErrorCode> for ErrorCode {
79 fn from(value: RoomPositionCreateConstructionSiteErrorCode) -> Self {
80 Self::result_from_i8(value as i8).unwrap_err()
87 }
88}
89
90#[derive(
97 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
98)]
99#[repr(i8)]
100pub enum RoomPositionCreateFlagErrorCode {
101 NameExists = -3,
102 Full = -8,
103 NotInRange = -9,
104 InvalidArgs = -10,
105}
106
107impl FromReturnCode for RoomPositionCreateFlagErrorCode {
108 type Error = Self;
109
110 fn result_from_i8(val: i8) -> Result<(), Self::Error> {
111 let maybe_result = Self::try_result_from_i8(val);
112 #[cfg(feature = "unsafe-return-conversion")]
113 unsafe {
114 maybe_result.unwrap_unchecked()
115 }
116 #[cfg(not(feature = "unsafe-return-conversion"))]
117 maybe_result.unwrap()
118 }
119
120 fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
121 match val {
122 -3 => Some(Err(RoomPositionCreateFlagErrorCode::NameExists)),
123 -8 => Some(Err(RoomPositionCreateFlagErrorCode::Full)),
124 -9 => Some(Err(RoomPositionCreateFlagErrorCode::NotInRange)),
125 -10 => Some(Err(RoomPositionCreateFlagErrorCode::InvalidArgs)),
126 _ => None,
127 }
128 }
129}
130
131impl fmt::Display for RoomPositionCreateFlagErrorCode {
132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133 let msg: &'static str = match self {
134 RoomPositionCreateFlagErrorCode::NameExists => {
135 "there is a flag with the same name already"
136 }
137 RoomPositionCreateFlagErrorCode::Full => {
138 "you have too many flags. the maximum number of flags per player is 10000"
139 }
140 RoomPositionCreateFlagErrorCode::NotInRange => "room not visible",
141 RoomPositionCreateFlagErrorCode::InvalidArgs => {
142 "the location or the color constant is incorrect"
143 }
144 };
145
146 write!(f, "{}", msg)
147 }
148}
149
150impl Error for RoomPositionCreateFlagErrorCode {}
151
152impl From<RoomPositionCreateFlagErrorCode> for ErrorCode {
153 fn from(value: RoomPositionCreateFlagErrorCode) -> Self {
154 Self::result_from_i8(value as i8).unwrap_err()
161 }
162}