screeps/enums/action_error_codes/
structureterminal_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(
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 Self::result_from_i8(value as i8).unwrap_err()
80 }
81}