screeps/enums/action_error_codes/
structuretower_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 [StructureTower::attack](crate::StructureTower::attack).
9///
10/// [Screeps API Docs](https://docs.screeps.com/api/#StructureTower.attack).
11///
12/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L766)
13#[derive(
14    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
15)]
16#[repr(i8)]
17pub enum TowerAttackErrorCode {
18    NotOwner = -1,
19    NotEnoughEnergy = -6,
20    InvalidTarget = -7,
21    RclNotEnough = -14,
22}
23
24impl FromReturnCode for TowerAttackErrorCode {
25    type Error = Self;
26
27    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
28        let maybe_result = Self::try_result_from_i8(val);
29        #[cfg(feature = "unsafe-return-conversion")]
30        unsafe {
31            maybe_result.unwrap_unchecked()
32        }
33        #[cfg(not(feature = "unsafe-return-conversion"))]
34        maybe_result.unwrap()
35    }
36
37    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
38        match val {
39            0 => Some(Ok(())),
40            -1 => Some(Err(TowerAttackErrorCode::NotOwner)),
41            -6 => Some(Err(TowerAttackErrorCode::NotEnoughEnergy)),
42            -7 => Some(Err(TowerAttackErrorCode::InvalidTarget)),
43            -14 => Some(Err(TowerAttackErrorCode::RclNotEnough)),
44            _ => None,
45        }
46    }
47}
48
49impl fmt::Display for TowerAttackErrorCode {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        let msg: &'static str = match self {
52            TowerAttackErrorCode::NotOwner => "you are not the owner of this structure",
53            TowerAttackErrorCode::NotEnoughEnergy => "the tower does not have enough energy",
54            TowerAttackErrorCode::InvalidTarget => "the target is not a valid attackable object",
55            TowerAttackErrorCode::RclNotEnough => {
56                "room controller level insufficient to use this structure"
57            }
58        };
59
60        write!(f, "{}", msg)
61    }
62}
63
64impl Error for TowerAttackErrorCode {}
65
66impl From<TowerAttackErrorCode> for ErrorCode {
67    fn from(value: TowerAttackErrorCode) -> Self {
68        // Safety: TowerAttackErrorCode is repr(i8), so we can cast it to get the
69        // discriminant value, which will match the raw return code value that ErrorCode
70        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
71        // Safety: TowerAttackErrorCode discriminants are always error code values, and
72        // thus the Result returned here will always be an `Err` variant, so we can
73        // always extract the error without panicking
74        Self::result_from_i8(value as i8).unwrap_err()
75    }
76}
77
78/// Error codes used by [StructureTower::heal](crate::StructureTower::heal).
79///
80/// [Screeps API Docs](https://docs.screeps.com/api/#StructureTower.heal).
81///
82/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L786)
83#[derive(
84    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
85)]
86#[repr(i8)]
87pub enum TowerHealErrorCode {
88    NotOwner = -1,
89    NotEnoughEnergy = -6,
90    InvalidTarget = -7,
91    RclNotEnough = -14,
92}
93
94impl FromReturnCode for TowerHealErrorCode {
95    type Error = Self;
96
97    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
98        let maybe_result = Self::try_result_from_i8(val);
99        #[cfg(feature = "unsafe-return-conversion")]
100        unsafe {
101            maybe_result.unwrap_unchecked()
102        }
103        #[cfg(not(feature = "unsafe-return-conversion"))]
104        maybe_result.unwrap()
105    }
106
107    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
108        match val {
109            0 => Some(Ok(())),
110            -1 => Some(Err(TowerHealErrorCode::NotOwner)),
111            -6 => Some(Err(TowerHealErrorCode::NotEnoughEnergy)),
112            -7 => Some(Err(TowerHealErrorCode::InvalidTarget)),
113            -14 => Some(Err(TowerHealErrorCode::RclNotEnough)),
114            _ => None,
115        }
116    }
117}
118
119impl fmt::Display for TowerHealErrorCode {
120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121        let msg: &'static str = match self {
122            TowerHealErrorCode::NotOwner => "you are not the owner of this structure",
123            TowerHealErrorCode::NotEnoughEnergy => "the tower does not have enough energy",
124            TowerHealErrorCode::InvalidTarget => "the target is not a valid creep object",
125            TowerHealErrorCode::RclNotEnough => {
126                "room controller level insufficient to use this structure"
127            }
128        };
129
130        write!(f, "{}", msg)
131    }
132}
133
134impl Error for TowerHealErrorCode {}
135
136impl From<TowerHealErrorCode> for ErrorCode {
137    fn from(value: TowerHealErrorCode) -> Self {
138        // Safety: TowerHealErrorCode 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: TowerHealErrorCode discriminants are always error code values, and
142        // 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 [StructureTower::repair](crate::StructureTower::repair).
149///
150/// [Screeps API Docs](https://docs.screeps.com/api/#StructureTower.repair).
151///
152/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L806)
153#[derive(
154    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
155)]
156#[repr(i8)]
157pub enum TowerRepairErrorCode {
158    NotOwner = -1,
159    NotEnoughEnergy = -6,
160    InvalidTarget = -7,
161    RclNotEnough = -14,
162}
163
164impl FromReturnCode for TowerRepairErrorCode {
165    type Error = Self;
166
167    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
168        let maybe_result = Self::try_result_from_i8(val);
169        #[cfg(feature = "unsafe-return-conversion")]
170        unsafe {
171            maybe_result.unwrap_unchecked()
172        }
173        #[cfg(not(feature = "unsafe-return-conversion"))]
174        maybe_result.unwrap()
175    }
176
177    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
178        match val {
179            0 => Some(Ok(())),
180            -1 => Some(Err(TowerRepairErrorCode::NotOwner)),
181            -6 => Some(Err(TowerRepairErrorCode::NotEnoughEnergy)),
182            -7 => Some(Err(TowerRepairErrorCode::InvalidTarget)),
183            -14 => Some(Err(TowerRepairErrorCode::RclNotEnough)),
184            _ => None,
185        }
186    }
187}
188
189impl fmt::Display for TowerRepairErrorCode {
190    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191        let msg: &'static str = match self {
192            TowerRepairErrorCode::NotOwner => "you are not the owner of this structure",
193            TowerRepairErrorCode::NotEnoughEnergy => "the tower does not have enough energy",
194            TowerRepairErrorCode::InvalidTarget => "the target is not a valid repairable object",
195            TowerRepairErrorCode::RclNotEnough => {
196                "room controller level insufficient to use this structure"
197            }
198        };
199
200        write!(f, "{}", msg)
201    }
202}
203
204impl Error for TowerRepairErrorCode {}
205
206impl From<TowerRepairErrorCode> for ErrorCode {
207    fn from(value: TowerRepairErrorCode) -> Self {
208        // Safety: TowerRepairErrorCode is repr(i8), so we can cast it to get the
209        // discriminant value, which will match the raw return code value that ErrorCode
210        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
211        // Safety: TowerRepairErrorCode discriminants are always error code values, and
212        // thus the Result returned here will always be an `Err` variant, so we can
213        // always extract the error without panicking
214        Self::result_from_i8(value as i8).unwrap_err()
215    }
216}