screeps/enums/action_error_codes/
structure_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 [Structure::destroy](crate::Structure::destroy).
9///
10/// [Screeps API Docs](https://docs.screeps.com/api/#Structure.destroy).
11///
12/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L72)
13#[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        // Safety: DestroyErrorCode is repr(i8), so we can cast it to get the
66        // discriminant value, which will match the raw return code value that ErrorCode
67        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
68        // Safety: DestroyErrorCode discriminants are always error code values, and thus
69        // the Result returned here will always be an `Err` variant, so we can always
70        // extract the error without panicking
71        Self::result_from_i8(value as i8).unwrap_err()
72    }
73}
74
75/// Error codes used by
76/// [Structure::notify_when_attacked](crate::Structure::notify_when_attacked).
77///
78/// [Screeps API Docs](https://docs.screeps.com/api/#Structure.notifyWhenAttacked).
79///
80/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L89)
81#[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        // Safety: StructureNotifyWhenAttackedErrorCode is repr(i8), so we can cast it
136        // to get the discriminant value, which will match the raw return code value
137        // that ErrorCode expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
138        // Safety: StructureNotifyWhenAttackedErrorCode discriminants are always error
139        // code values, and thus the Result returned here will always be an `Err`
140        // variant, so we can always extract the error without panicking
141        Self::result_from_i8(value as i8).unwrap_err()
142    }
143}