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