screeps/enums/action_error_codes/
room_error_codes.rs

1use std::{error::Error, fmt};
2
3use num_derive::FromPrimitive;
4use serde_repr::{Deserialize_repr, Serialize_repr};
5
6use crate::{constants::ErrorCode, FromReturnCode};
7
8/// Error codes used by
9/// [Room::create_construction_site](crate::Room::create_construction_site).
10///
11/// [Screeps API Docs](https://docs.screeps.com/api/#Room.createConstructionSite).
12///
13/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/rooms.js#L1029)
14#[derive(
15    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
16)]
17#[repr(i8)]
18pub enum RoomCreateConstructionSiteErrorCode {
19    NotOwner = -1,
20    InvalidTarget = -7,
21    Full = -8,
22    InvalidArgs = -10,
23    RclNotEnough = -14,
24}
25
26impl FromReturnCode for RoomCreateConstructionSiteErrorCode {
27    type Error = Self;
28
29    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
30        let maybe_result = Self::try_result_from_i8(val);
31        #[cfg(feature = "unsafe-return-conversion")]
32        unsafe {
33            maybe_result.unwrap_unchecked()
34        }
35        #[cfg(not(feature = "unsafe-return-conversion"))]
36        maybe_result.unwrap()
37    }
38
39    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
40        match val {
41            0 => Some(Ok(())),
42            -1 => Some(Err(RoomCreateConstructionSiteErrorCode::NotOwner)),
43            -7 => Some(Err(RoomCreateConstructionSiteErrorCode::InvalidTarget)),
44            -8 => Some(Err(RoomCreateConstructionSiteErrorCode::Full)),
45            -10 => Some(Err(RoomCreateConstructionSiteErrorCode::InvalidArgs)),
46            -14 => Some(Err(RoomCreateConstructionSiteErrorCode::RclNotEnough)),
47            _ => None,
48        }
49    }
50}
51
52impl fmt::Display for RoomCreateConstructionSiteErrorCode {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        let msg: &'static str = match self {
55            RoomCreateConstructionSiteErrorCode::NotOwner => "the room is claimed or reserved by a hostile player",
56            RoomCreateConstructionSiteErrorCode::InvalidTarget => "the structure cannot be placed at the specified location",
57            RoomCreateConstructionSiteErrorCode::Full => "you have too many construction sites. the maximum number of construction sites per player is 100",
58            RoomCreateConstructionSiteErrorCode::InvalidArgs => "the location is incorrect",
59            RoomCreateConstructionSiteErrorCode::RclNotEnough => "room controller level insufficient. learn more",
60        };
61
62        write!(f, "{}", msg)
63    }
64}
65
66impl Error for RoomCreateConstructionSiteErrorCode {}
67
68impl From<RoomCreateConstructionSiteErrorCode> for ErrorCode {
69    fn from(value: RoomCreateConstructionSiteErrorCode) -> Self {
70        // Safety: RoomCreateConstructionSiteErrorCode is repr(i8), so we can cast it to
71        // get the discriminant value, which will match the raw return code value that
72        // ErrorCode expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
73        // Safety: RoomCreateConstructionSiteErrorCode discriminants are always error
74        // code values, and thus the Result returned here will always be an `Err`
75        // variant, so we can always extract the error without panicking
76        Self::result_from_i8(value as i8).unwrap_err()
77    }
78}
79
80/// Error codes used by [Room::create_flag](crate::Room::create_flag).
81///
82/// [Screeps API Docs](https://docs.screeps.com/api/#Room.createFlag).
83///
84/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/rooms.js#L978)
85#[derive(
86    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
87)]
88#[repr(i8)]
89pub enum RoomCreateFlagErrorCode {
90    NameExists = -3,
91    Full = -8,
92    InvalidArgs = -10,
93}
94
95impl FromReturnCode for RoomCreateFlagErrorCode {
96    type Error = Self;
97
98    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
99        let maybe_result = Self::try_result_from_i8(val);
100        #[cfg(feature = "unsafe-return-conversion")]
101        unsafe {
102            maybe_result.unwrap_unchecked()
103        }
104        #[cfg(not(feature = "unsafe-return-conversion"))]
105        maybe_result.unwrap()
106    }
107
108    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
109        match val {
110            -3 => Some(Err(RoomCreateFlagErrorCode::NameExists)),
111            -8 => Some(Err(RoomCreateFlagErrorCode::Full)),
112            -10 => Some(Err(RoomCreateFlagErrorCode::InvalidArgs)),
113            _ => None,
114        }
115    }
116}
117
118impl fmt::Display for RoomCreateFlagErrorCode {
119    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120        let msg: &'static str = match self {
121            RoomCreateFlagErrorCode::NameExists => "there is a flag with the same name already",
122            RoomCreateFlagErrorCode::Full => {
123                "you have too many flags. the maximum number of flags per player is 10000"
124            }
125            RoomCreateFlagErrorCode::InvalidArgs => {
126                "the location or the name or the color constant is incorrect"
127            }
128        };
129
130        write!(f, "{}", msg)
131    }
132}
133
134impl Error for RoomCreateFlagErrorCode {}
135
136impl From<RoomCreateFlagErrorCode> for ErrorCode {
137    fn from(value: RoomCreateFlagErrorCode) -> Self {
138        // Safety: RoomCreateFlagErrorCode is repr(i8), so we can cast it to get the
139        // discriminant value, which will match the raw return code value that ErrorCode
140        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
141        // Safety: RoomCreateFlagErrorCode discriminants are always error code values,
142        // and thus the Result returned here will always be an `Err` variant, so we can
143        // always extract the error without panicking
144        Self::result_from_i8(value as i8).unwrap_err()
145    }
146}
147
148/// Error codes used by [Room::find_exit_to](crate::Room::find_exit_to).
149///
150/// [Screeps API Docs](https://docs.screeps.com/api/#Room.findExitTo).
151///
152/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/rooms.js#L1130)
153#[derive(
154    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
155)]
156#[repr(i8)]
157pub enum FindExitToErrorCode {
158    NoPath = -2,
159    InvalidArgs = -10,
160}
161
162impl FromReturnCode for FindExitToErrorCode {
163    type Error = Self;
164
165    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
166        let maybe_result = Self::try_result_from_i8(val);
167        #[cfg(feature = "unsafe-return-conversion")]
168        unsafe {
169            maybe_result.unwrap_unchecked()
170        }
171        #[cfg(not(feature = "unsafe-return-conversion"))]
172        maybe_result.unwrap()
173    }
174
175    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
176        match val {
177            -2 => Some(Err(FindExitToErrorCode::NoPath)),
178            -10 => Some(Err(FindExitToErrorCode::InvalidArgs)),
179            _ => None,
180        }
181    }
182}
183
184impl fmt::Display for FindExitToErrorCode {
185    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186        let msg: &'static str = match self {
187            FindExitToErrorCode::NoPath => "path can not be found",
188            FindExitToErrorCode::InvalidArgs => "the location is incorrect",
189        };
190
191        write!(f, "{}", msg)
192    }
193}
194
195impl Error for FindExitToErrorCode {}
196
197impl From<FindExitToErrorCode> for ErrorCode {
198    fn from(value: FindExitToErrorCode) -> Self {
199        // Safety: FindExitToErrorCode is repr(i8), so we can cast it to get the
200        // discriminant value, which will match the raw return code value that ErrorCode
201        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
202        // Safety: FindExitToErrorCode discriminants are always error code values, and
203        // thus the Result returned here will always be an `Err` variant, so we can
204        // always extract the error without panicking
205        Self::result_from_i8(value as i8).unwrap_err()
206    }
207}