screeps/enums/action_error_codes/
structurepowerspawn_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/// [StructurePowerSpawn::process_power](crate::StructurePowerSpawn::process_power).
10///
11///
12/// [Screeps API Docs](https://docs.screeps.com/api/#StructurePowerSpawn.processPower).
13///
14/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L613)
15#[derive(
16    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
17)]
18#[repr(i8)]
19pub enum ProcessPowerErrorCode {
20    NotOwner = -1,
21    NotEnoughResources = -6,
22    RclNotEnough = -14,
23}
24
25impl FromReturnCode for ProcessPowerErrorCode {
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(ProcessPowerErrorCode::NotOwner)),
42            -6 => Some(Err(ProcessPowerErrorCode::NotEnoughResources)),
43            -14 => Some(Err(ProcessPowerErrorCode::RclNotEnough)),
44            _ => None,
45        }
46    }
47}
48
49impl fmt::Display for ProcessPowerErrorCode {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        let msg: &'static str = match self {
52            ProcessPowerErrorCode::NotOwner => "you are not the owner of this structure",
53            ProcessPowerErrorCode::NotEnoughResources => {
54                "the structure does not have enough energy or power resource units"
55            }
56            ProcessPowerErrorCode::RclNotEnough => {
57                "room controller level insufficient to use this structure"
58            }
59        };
60
61        write!(f, "{}", msg)
62    }
63}
64
65impl Error for ProcessPowerErrorCode {}
66
67impl From<ProcessPowerErrorCode> for ErrorCode {
68    fn from(value: ProcessPowerErrorCode) -> Self {
69        // Safety: ProcessPowerErrorCode 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: ProcessPowerErrorCode 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}