screeps/enums/action_error_codes/
structurepowerspawn_error_codes.rs1use std::{error::Error, fmt};
2
3use num_derive::FromPrimitive;
4use serde_repr::{Deserialize_repr, Serialize_repr};
5
6use crate::{constants::ErrorCode, FromReturnCode};
7
8#[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 Self::result_from_i8(value as i8).unwrap_err()
76 }
77}