screeps/enums/action_error_codes/
structurefactory_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/// [StructureFactory::produce](crate::StructureFactory::produce).
10///
11/// [Screeps API Docs](https://docs.screeps.com/api/#StructureFactory.produce).
12///
13/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L1434)
14#[derive(
15    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
16)]
17#[repr(i8)]
18pub enum ProduceErrorCode {
19    NotOwner = -1,
20    Busy = -4,
21    NotEnoughResources = -6,
22    InvalidTarget = -7,
23    Full = -8,
24    InvalidArgs = -10,
25    Tired = -11,
26    RclNotEnough = -14,
27}
28
29impl FromReturnCode for ProduceErrorCode {
30    type Error = Self;
31
32    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
33        let maybe_result = Self::try_result_from_i8(val);
34        #[cfg(feature = "unsafe-return-conversion")]
35        unsafe {
36            maybe_result.unwrap_unchecked()
37        }
38        #[cfg(not(feature = "unsafe-return-conversion"))]
39        maybe_result.unwrap()
40    }
41
42    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
43        match val {
44            0 => Some(Ok(())),
45            -1 => Some(Err(ProduceErrorCode::NotOwner)),
46            -4 => Some(Err(ProduceErrorCode::Busy)),
47            -6 => Some(Err(ProduceErrorCode::NotEnoughResources)),
48            -7 => Some(Err(ProduceErrorCode::InvalidTarget)),
49            -8 => Some(Err(ProduceErrorCode::Full)),
50            -10 => Some(Err(ProduceErrorCode::InvalidArgs)),
51            -11 => Some(Err(ProduceErrorCode::Tired)),
52            -14 => Some(Err(ProduceErrorCode::RclNotEnough)),
53            _ => None,
54        }
55    }
56}
57
58impl fmt::Display for ProduceErrorCode {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        let msg: &'static str = match self {
61            ProduceErrorCode::NotOwner => "you are not the owner of this structure",
62            ProduceErrorCode::Busy => {
63                "the factory is not operated by the pwr_operate_factory power"
64            }
65            ProduceErrorCode::NotEnoughResources => {
66                "the structure does not have the required amount of resources"
67            }
68            ProduceErrorCode::InvalidTarget => {
69                "the factory cannot produce the commodity of this level"
70            }
71            ProduceErrorCode::Full => "the factory cannot contain the produce",
72            ProduceErrorCode::InvalidArgs => "the arguments provided are incorrect",
73            ProduceErrorCode::Tired => "the factory is still cooling down",
74            ProduceErrorCode::RclNotEnough => {
75                "your room controller level is insufficient to use the factory"
76            }
77        };
78
79        write!(f, "{}", msg)
80    }
81}
82
83impl Error for ProduceErrorCode {}
84
85impl From<ProduceErrorCode> for ErrorCode {
86    fn from(value: ProduceErrorCode) -> Self {
87        // Safety: ProduceErrorCode is repr(i8), so we can cast it to get the
88        // discriminant value, which will match the raw return code value that ErrorCode
89        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
90        // Safety: ProduceErrorCode discriminants are always error code values, and thus
91        // the Result returned here will always be an `Err` variant, so we can always
92        // extract the error without panicking
93        Self::result_from_i8(value as i8).unwrap_err()
94    }
95}