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