screeps/enums/action_error_codes/
game_map_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 [game::map::find_exit](crate::game::map::find_exit).
9///
10/// [Screeps API Docs](https://docs.screeps.com/api/#Game.map.findExit).
11///
12/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/map.js#L188)
13#[derive(
14    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
15)]
16#[repr(i8)]
17pub enum FindExitErrorCode {
18    NoPath = -2,
19    InvalidArgs = -10,
20}
21
22impl FromReturnCode for FindExitErrorCode {
23    type Error = Self;
24
25    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
26        let maybe_result = Self::try_result_from_i8(val);
27        #[cfg(feature = "unsafe-return-conversion")]
28        unsafe {
29            maybe_result.unwrap_unchecked()
30        }
31        #[cfg(not(feature = "unsafe-return-conversion"))]
32        maybe_result.unwrap()
33    }
34
35    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
36        match val {
37            -2 => Some(Err(FindExitErrorCode::NoPath)),
38            -10 => Some(Err(FindExitErrorCode::InvalidArgs)),
39            _ => None,
40        }
41    }
42}
43
44impl fmt::Display for FindExitErrorCode {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        let msg: &'static str = match self {
47            FindExitErrorCode::NoPath => "path can not be found",
48            FindExitErrorCode::InvalidArgs => "the location is incorrect",
49        };
50
51        write!(f, "{}", msg)
52    }
53}
54
55impl Error for FindExitErrorCode {}
56
57impl From<FindExitErrorCode> for ErrorCode {
58    fn from(value: FindExitErrorCode) -> Self {
59        // Safety: FindExitErrorCode is repr(i8), so we can cast it to get the
60        // discriminant value, which will match the raw return code value that ErrorCode
61        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
62        // Safety: FindExitErrorCode discriminants are always error code values, and
63        // thus the Result returned here will always be an `Err` variant, so we can
64        // always extract the error without panicking
65        Self::result_from_i8(value as i8).unwrap_err()
66    }
67}
68
69/// Error codes used by [game::map::find_route](crate::game::map::find_route).
70///
71/// [Screeps API Docs](https://docs.screeps.com/api/#Game.map.findRoute).
72///
73/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/map.js#L69)
74#[derive(
75    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
76)]
77#[repr(i8)]
78pub enum FindRouteErrorCode {
79    NoPath = -2,
80}
81
82impl FromReturnCode for FindRouteErrorCode {
83    type Error = Self;
84
85    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
86        let maybe_result = Self::try_result_from_i8(val);
87        #[cfg(feature = "unsafe-return-conversion")]
88        unsafe {
89            maybe_result.unwrap_unchecked()
90        }
91        #[cfg(not(feature = "unsafe-return-conversion"))]
92        maybe_result.unwrap()
93    }
94
95    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
96        match val {
97            -2 => Some(Err(FindRouteErrorCode::NoPath)),
98            _ => None,
99        }
100    }
101}
102
103impl fmt::Display for FindRouteErrorCode {
104    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105        let msg: &'static str = match self {
106            FindRouteErrorCode::NoPath => "path can not be found",
107        };
108
109        write!(f, "{}", msg)
110    }
111}
112
113impl Error for FindRouteErrorCode {}
114
115impl From<FindRouteErrorCode> for ErrorCode {
116    fn from(value: FindRouteErrorCode) -> Self {
117        // Safety: FindRouteErrorCode is repr(i8), so we can cast it to get the
118        // discriminant value, which will match the raw return code value that ErrorCode
119        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
120        // Safety: FindRouteErrorCode discriminants are always error code values, and
121        // thus the Result returned here will always be an `Err` variant, so we can
122        // always extract the error without panicking
123        Self::result_from_i8(value as i8).unwrap_err()
124    }
125}