screeps/enums/action_error_codes/
roomposition_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/// [RoomPosition::create_construction_site](crate::RoomPosition::create_construction_site).
10///
11///
12/// [Screeps API Docs](https://docs.screeps.com/api/#RoomPosition.createConstructionSite).
13///
14/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/rooms.js#L1630)
15#[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        // Safety: RoomPositionCreateConstructionSiteErrorCode is repr(i8), so we can
81        // cast it to get the discriminant value, which will match the raw return code
82        // value that ErrorCode expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
83        // Safety: RoomPositionCreateConstructionSiteErrorCode discriminants are always
84        // error code values, and thus the Result returned here will always be an `Err`
85        // variant, so we can always extract the error without panicking
86        Self::result_from_i8(value as i8).unwrap_err()
87    }
88}
89
90/// Error codes used by
91/// [RoomPosition::create_flag](crate::RoomPosition::create_flag).
92///
93/// [Screeps API Docs](https://docs.screeps.com/api/#RoomPosition.createFlag).
94///
95/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/rooms.js#L1622)
96#[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        // Safety: RoomPositionCreateFlagErrorCode is repr(i8), so we can cast it to get
155        // the discriminant value, which will match the raw return code value that
156        // ErrorCode expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
157        // Safety: RoomPositionCreateFlagErrorCode discriminants are always error code
158        // values, and thus the Result returned here will always be an `Err` variant, so
159        // we can always extract the error without panicking
160        Self::result_from_i8(value as i8).unwrap_err()
161    }
162}