screeps/enums/action_error_codes/
flag_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 [Flag::remove](crate::Flag::remove).
9///
10/// [Screeps API Docs](https://docs.screeps.com/api/#Flag.remove).
11///
12/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/flags.js#L57)
13#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive)]
14pub enum FlagRemoveErrorCode {}
15
16impl FromReturnCode for FlagRemoveErrorCode {
17    type Error = Self;
18
19    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
20        let maybe_result = Self::try_result_from_i8(val);
21        #[cfg(feature = "unsafe-return-conversion")]
22        unsafe {
23            maybe_result.unwrap_unchecked()
24        }
25        #[cfg(not(feature = "unsafe-return-conversion"))]
26        maybe_result.unwrap()
27    }
28
29    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
30        match val {
31            0 => Some(Ok(())),
32            _ => None,
33        }
34    }
35}
36
37impl fmt::Display for FlagRemoveErrorCode {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        let msg: &'static str = "Zero-variant enum";
40
41        write!(f, "{}", msg)
42    }
43}
44
45impl Error for FlagRemoveErrorCode {}
46
47/// Error codes used by [Flag::set_color](crate::Flag::set_color).
48///
49/// [Screeps API Docs](https://docs.screeps.com/api/#Flag.setColor).
50///
51/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/flags.js#L76)
52#[derive(
53    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
54)]
55#[repr(i8)]
56pub enum SetColorErrorCode {
57    InvalidArgs = -10,
58}
59
60impl FromReturnCode for SetColorErrorCode {
61    type Error = Self;
62
63    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
64        let maybe_result = Self::try_result_from_i8(val);
65        #[cfg(feature = "unsafe-return-conversion")]
66        unsafe {
67            maybe_result.unwrap_unchecked()
68        }
69        #[cfg(not(feature = "unsafe-return-conversion"))]
70        maybe_result.unwrap()
71    }
72
73    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
74        match val {
75            0 => Some(Ok(())),
76            -10 => Some(Err(SetColorErrorCode::InvalidArgs)),
77            _ => None,
78        }
79    }
80}
81
82impl fmt::Display for SetColorErrorCode {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        let msg: &'static str = match self {
85            SetColorErrorCode::InvalidArgs => {
86                "color or secondarycolor is not a valid color constant"
87            }
88        };
89
90        write!(f, "{}", msg)
91    }
92}
93
94impl Error for SetColorErrorCode {}
95
96impl From<SetColorErrorCode> for ErrorCode {
97    fn from(value: SetColorErrorCode) -> Self {
98        // Safety: SetColorErrorCode is repr(i8), so we can cast it to get the
99        // discriminant value, which will match the raw return code value that ErrorCode
100        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
101        // Safety: SetColorErrorCode discriminants are always error code values, and
102        // thus the Result returned here will always be an `Err` variant, so we can
103        // always extract the error without panicking
104        Self::result_from_i8(value as i8).unwrap_err()
105    }
106}
107
108/// Error codes used by [Flag::set_position](crate::Flag::set_position).
109///
110/// [Screeps API Docs](https://docs.screeps.com/api/#Flag.setPosition).
111///
112/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/flags.js#L63)
113#[derive(
114    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
115)]
116#[repr(i8)]
117pub enum SetPositionErrorCode {
118    InvalidTarget = -7,
119}
120
121impl FromReturnCode for SetPositionErrorCode {
122    type Error = Self;
123
124    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
125        let maybe_result = Self::try_result_from_i8(val);
126        #[cfg(feature = "unsafe-return-conversion")]
127        unsafe {
128            maybe_result.unwrap_unchecked()
129        }
130        #[cfg(not(feature = "unsafe-return-conversion"))]
131        maybe_result.unwrap()
132    }
133
134    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
135        match val {
136            0 => Some(Ok(())),
137            -7 => Some(Err(SetPositionErrorCode::InvalidTarget)),
138            _ => None,
139        }
140    }
141}
142
143impl fmt::Display for SetPositionErrorCode {
144    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145        let msg: &'static str = match self {
146            SetPositionErrorCode::InvalidTarget => "the target provided is invalid",
147        };
148
149        write!(f, "{}", msg)
150    }
151}
152
153impl Error for SetPositionErrorCode {}
154
155impl From<SetPositionErrorCode> for ErrorCode {
156    fn from(value: SetPositionErrorCode) -> Self {
157        // Safety: SetPositionErrorCode is repr(i8), so we can cast it to get the
158        // discriminant value, which will match the raw return code value that ErrorCode
159        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
160        // Safety: SetPositionErrorCode discriminants are always error code values, and
161        // thus the Result returned here will always be an `Err` variant, so we can
162        // always extract the error without panicking
163        Self::result_from_i8(value as i8).unwrap_err()
164    }
165}