screeps/enums/action_error_codes/
structureobserver_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/// [StructureObserver::observe_room](crate::StructureObserver::observe_room).
10///
11/// [Screeps API Docs](https://docs.screeps.com/api/#StructureObserver.observeRoom).
12///
13/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L548)
14#[derive(
15    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
16)]
17#[repr(i8)]
18pub enum ObserveRoomErrorCode {
19    NotOwner = -1,
20    NotInRange = -9,
21    InvalidArgs = -10,
22    RclNotEnough = -14,
23}
24
25impl FromReturnCode for ObserveRoomErrorCode {
26    type Error = Self;
27
28    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
29        let maybe_result = Self::try_result_from_i8(val);
30        #[cfg(feature = "unsafe-return-conversion")]
31        unsafe {
32            maybe_result.unwrap_unchecked()
33        }
34        #[cfg(not(feature = "unsafe-return-conversion"))]
35        maybe_result.unwrap()
36    }
37
38    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
39        match val {
40            0 => Some(Ok(())),
41            -1 => Some(Err(ObserveRoomErrorCode::NotOwner)),
42            -9 => Some(Err(ObserveRoomErrorCode::NotInRange)),
43            -10 => Some(Err(ObserveRoomErrorCode::InvalidArgs)),
44            -14 => Some(Err(ObserveRoomErrorCode::RclNotEnough)),
45            _ => None,
46        }
47    }
48}
49
50impl fmt::Display for ObserveRoomErrorCode {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        let msg: &'static str = match self {
53            ObserveRoomErrorCode::NotOwner => "you are not the owner of this structure",
54            ObserveRoomErrorCode::NotInRange => "room roomname is not in observing range",
55            ObserveRoomErrorCode::InvalidArgs => "roomname argument is not a valid room name value",
56            ObserveRoomErrorCode::RclNotEnough => {
57                "room controller level insufficient to use this structure"
58            }
59        };
60
61        write!(f, "{}", msg)
62    }
63}
64
65impl Error for ObserveRoomErrorCode {}
66
67impl From<ObserveRoomErrorCode> for ErrorCode {
68    fn from(value: ObserveRoomErrorCode) -> Self {
69        // Safety: ObserveRoomErrorCode is repr(i8), so we can cast it to get the
70        // discriminant value, which will match the raw return code value that ErrorCode
71        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
72        // Safety: ObserveRoomErrorCode discriminants are always error code values, and
73        // thus the Result returned here will always be an `Err` variant, so we can
74        // always extract the error without panicking
75        Self::result_from_i8(value as i8).unwrap_err()
76    }
77}