1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::{error::Error, fmt};
use num_derive::FromPrimitive;
use serde_repr::{Deserialize_repr, Serialize_repr};
use crate::{constants::ErrorCode, FromReturnCode};
/// Error codes used by [Spawning::cancel](crate::Spawning::cancel).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#StructureSpawn.Spawning.cancel).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L1328)
#[derive(
Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum CancelErrorCode {
NotOwner = -1,
}
impl FromReturnCode for CancelErrorCode {
type Error = Self;
fn result_from_i8(val: i8) -> Result<(), Self::Error> {
let maybe_result = Self::try_result_from_i8(val);
#[cfg(feature = "unsafe-return-conversion")]
unsafe {
maybe_result.unwrap_unchecked()
}
#[cfg(not(feature = "unsafe-return-conversion"))]
maybe_result.unwrap()
}
fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
match val {
0 => Some(Ok(())),
-1 => Some(Err(CancelErrorCode::NotOwner)),
_ => None,
}
}
}
impl fmt::Display for CancelErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg: &'static str = match self {
CancelErrorCode::NotOwner => "you are not the owner of this spawn",
};
write!(f, "{}", msg)
}
}
impl Error for CancelErrorCode {}
impl From<CancelErrorCode> for ErrorCode {
fn from(value: CancelErrorCode) -> Self {
// Safety: CancelErrorCode is repr(i8), so we can cast it to get the
// discriminant 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
// Safety: CancelErrorCode discriminants are always error code values, and thus
// the Result returned here will always be an `Err` variant, so we can always
// extract the error without panicking
Self::result_from_i8(value as i8).unwrap_err()
}
}
/// Error codes used by
/// [Spawning::set_directions](crate::Spawning::set_directions).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#StructureSpawn.Spawning.setDirections).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/structures.js#L1312)
#[derive(
Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum SetDirectionsErrorCode {
NotOwner = -1,
InvalidArgs = -10,
}
impl FromReturnCode for SetDirectionsErrorCode {
type Error = Self;
fn result_from_i8(val: i8) -> Result<(), Self::Error> {
let maybe_result = Self::try_result_from_i8(val);
#[cfg(feature = "unsafe-return-conversion")]
unsafe {
maybe_result.unwrap_unchecked()
}
#[cfg(not(feature = "unsafe-return-conversion"))]
maybe_result.unwrap()
}
fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
match val {
0 => Some(Ok(())),
-1 => Some(Err(SetDirectionsErrorCode::NotOwner)),
-10 => Some(Err(SetDirectionsErrorCode::InvalidArgs)),
_ => None,
}
}
}
impl fmt::Display for SetDirectionsErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg: &'static str = match self {
SetDirectionsErrorCode::NotOwner => "you are not the owner of this spawn",
SetDirectionsErrorCode::InvalidArgs => "the directions is array is invalid",
};
write!(f, "{}", msg)
}
}
impl Error for SetDirectionsErrorCode {}
impl From<SetDirectionsErrorCode> for ErrorCode {
fn from(value: SetDirectionsErrorCode) -> Self {
// Safety: SetDirectionsErrorCode is repr(i8), so we can cast it to get the
// discriminant 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
// Safety: SetDirectionsErrorCode discriminants are always error code values,
// and thus the Result returned here will always be an `Err` variant, so we can
// always extract the error without panicking
Self::result_from_i8(value as i8).unwrap_err()
}
}