screeps/enums/action_error_codes/
sharedcreep_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/// [SharedCreepProperties::drop](crate::SharedCreepProperties::drop).
10///
11/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.drop).
12///
13/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L404)
14#[derive(
15    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
16)]
17#[repr(i8)]
18pub enum DropErrorCode {
19    NotOwner = -1,
20    Busy = -4,
21    NotEnoughResources = -6,
22    InvalidArgs = -10,
23}
24
25impl FromReturnCode for DropErrorCode {
26    type Error = Self;
27
28    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
29        let maybe_result = Self::try_result_from_i8(val);
30        #[cfg(feature = "unsafe-return-conversion")]
31        unsafe {
32            maybe_result.unwrap_unchecked()
33        }
34        #[cfg(not(feature = "unsafe-return-conversion"))]
35        maybe_result.unwrap()
36    }
37
38    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
39        match val {
40            0 => Some(Ok(())),
41            -1 => Some(Err(DropErrorCode::NotOwner)),
42            -4 => Some(Err(DropErrorCode::Busy)),
43            -6 => Some(Err(DropErrorCode::NotEnoughResources)),
44            -10 => Some(Err(DropErrorCode::InvalidArgs)),
45            _ => None,
46        }
47    }
48}
49
50impl fmt::Display for DropErrorCode {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        let msg: &'static str = match self {
53            DropErrorCode::NotOwner => "you are not the owner of this creep",
54            DropErrorCode::Busy => "the creep is still being spawned",
55            DropErrorCode::NotEnoughResources => {
56                "the creep does not have the given amount of resources"
57            }
58            DropErrorCode::InvalidArgs => "the resourcetype is not a valid resource_* constants",
59        };
60
61        write!(f, "{}", msg)
62    }
63}
64
65impl Error for DropErrorCode {}
66
67impl From<DropErrorCode> for ErrorCode {
68    fn from(value: DropErrorCode) -> Self {
69        // Safety: DropErrorCode is repr(i8), so we can cast it to get the discriminant
70        // 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
71        // Safety: DropErrorCode discriminants are always error code values, and thus
72        // the Result returned here will always be an `Err` variant, so we can always
73        // extract the error without panicking
74        Self::result_from_i8(value as i8).unwrap_err()
75    }
76}
77
78/// Error codes used by
79/// [SharedCreepProperties::notify_when_attacked](crate::SharedCreepProperties::notify_when_attacked).
80///
81///
82/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.notifyWhenAttacked).
83///
84/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L988)
85#[derive(
86    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
87)]
88#[repr(i8)]
89pub enum NotifyWhenAttackedErrorCode {
90    NotOwner = -1,
91    Busy = -4,
92    InvalidArgs = -10,
93}
94
95impl FromReturnCode for NotifyWhenAttackedErrorCode {
96    type Error = Self;
97
98    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
99        let maybe_result = Self::try_result_from_i8(val);
100        #[cfg(feature = "unsafe-return-conversion")]
101        unsafe {
102            maybe_result.unwrap_unchecked()
103        }
104        #[cfg(not(feature = "unsafe-return-conversion"))]
105        maybe_result.unwrap()
106    }
107
108    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
109        match val {
110            0 => Some(Ok(())),
111            -1 => Some(Err(NotifyWhenAttackedErrorCode::NotOwner)),
112            -4 => Some(Err(NotifyWhenAttackedErrorCode::Busy)),
113            -10 => Some(Err(NotifyWhenAttackedErrorCode::InvalidArgs)),
114            _ => None,
115        }
116    }
117}
118
119impl fmt::Display for NotifyWhenAttackedErrorCode {
120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121        let msg: &'static str = match self {
122            NotifyWhenAttackedErrorCode::NotOwner => "you are not the owner of this creep",
123            NotifyWhenAttackedErrorCode::Busy => "the creep is still being spawned",
124            NotifyWhenAttackedErrorCode::InvalidArgs => "enable argument is not a boolean value",
125        };
126
127        write!(f, "{}", msg)
128    }
129}
130
131impl Error for NotifyWhenAttackedErrorCode {}
132
133impl From<NotifyWhenAttackedErrorCode> for ErrorCode {
134    fn from(value: NotifyWhenAttackedErrorCode) -> Self {
135        // Safety: NotifyWhenAttackedErrorCode is repr(i8), so we can cast it to get the
136        // discriminant value, which will match the raw return code value that ErrorCode
137        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
138        // Safety: NotifyWhenAttackedErrorCode discriminants are always error code
139        // values, and thus the Result returned here will always be an `Err` variant, so
140        // we can always extract the error without panicking
141        Self::result_from_i8(value as i8).unwrap_err()
142    }
143}
144
145/// Error codes used by
146/// [SharedCreepProperties::pickup](crate::SharedCreepProperties::pickup).
147///
148/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.pickup).
149///
150/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L566)
151#[derive(
152    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
153)]
154#[repr(i8)]
155pub enum PickupErrorCode {
156    NotOwner = -1,
157    Busy = -4,
158    InvalidTarget = -7,
159    Full = -8,
160    NotInRange = -9,
161}
162
163impl FromReturnCode for PickupErrorCode {
164    type Error = Self;
165
166    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
167        let maybe_result = Self::try_result_from_i8(val);
168        #[cfg(feature = "unsafe-return-conversion")]
169        unsafe {
170            maybe_result.unwrap_unchecked()
171        }
172        #[cfg(not(feature = "unsafe-return-conversion"))]
173        maybe_result.unwrap()
174    }
175
176    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
177        match val {
178            0 => Some(Ok(())),
179            -1 => Some(Err(PickupErrorCode::NotOwner)),
180            -4 => Some(Err(PickupErrorCode::Busy)),
181            -7 => Some(Err(PickupErrorCode::InvalidTarget)),
182            -8 => Some(Err(PickupErrorCode::Full)),
183            -9 => Some(Err(PickupErrorCode::NotInRange)),
184            _ => None,
185        }
186    }
187}
188
189impl fmt::Display for PickupErrorCode {
190    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191        let msg: &'static str = match self {
192            PickupErrorCode::NotOwner => "you are not the owner of this creep",
193            PickupErrorCode::Busy => "the creep is still being spawned",
194            PickupErrorCode::InvalidTarget => "the target is not a valid object to pick up",
195            PickupErrorCode::Full => "the creep cannot receive any more resource",
196            PickupErrorCode::NotInRange => "the target is too far away",
197        };
198
199        write!(f, "{}", msg)
200    }
201}
202
203impl Error for PickupErrorCode {}
204
205impl From<PickupErrorCode> for ErrorCode {
206    fn from(value: PickupErrorCode) -> Self {
207        // Safety: PickupErrorCode is repr(i8), so we can cast it to get the
208        // discriminant value, which will match the raw return code value that ErrorCode
209        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
210        // Safety: PickupErrorCode discriminants are always error code values, and thus
211        // the Result returned here will always be an `Err` variant, so we can always
212        // extract the error without panicking
213        Self::result_from_i8(value as i8).unwrap_err()
214    }
215}
216
217/// Error codes used by
218/// [SharedCreepProperties::say](crate::SharedCreepProperties::say).
219///
220/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.say).
221///
222/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L826)
223#[derive(
224    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
225)]
226#[repr(i8)]
227pub enum SayErrorCode {
228    NotOwner = -1,
229    Busy = -4,
230}
231
232impl FromReturnCode for SayErrorCode {
233    type Error = Self;
234
235    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
236        let maybe_result = Self::try_result_from_i8(val);
237        #[cfg(feature = "unsafe-return-conversion")]
238        unsafe {
239            maybe_result.unwrap_unchecked()
240        }
241        #[cfg(not(feature = "unsafe-return-conversion"))]
242        maybe_result.unwrap()
243    }
244
245    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
246        match val {
247            0 => Some(Ok(())),
248            -1 => Some(Err(SayErrorCode::NotOwner)),
249            -4 => Some(Err(SayErrorCode::Busy)),
250            _ => None,
251        }
252    }
253}
254
255impl fmt::Display for SayErrorCode {
256    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
257        let msg: &'static str = match self {
258            SayErrorCode::NotOwner => "you are not the owner of this creep",
259            SayErrorCode::Busy => "the creep is still being spawned",
260        };
261
262        write!(f, "{}", msg)
263    }
264}
265
266impl Error for SayErrorCode {}
267
268impl From<SayErrorCode> for ErrorCode {
269    fn from(value: SayErrorCode) -> Self {
270        // Safety: SayErrorCode is repr(i8), so we can cast it to get the discriminant
271        // 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
272        // Safety: SayErrorCode discriminants are always error code values, and thus the
273        // Result returned here will always be an `Err` variant, so we can always
274        // extract the error without panicking
275        Self::result_from_i8(value as i8).unwrap_err()
276    }
277}
278
279/// Error codes used by
280/// [SharedCreepProperties::suicide](crate::SharedCreepProperties::suicide).
281///
282/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.suicide).
283///
284/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L813)
285#[derive(
286    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
287)]
288#[repr(i8)]
289pub enum SuicideErrorCode {
290    NotOwner = -1,
291    Busy = -4,
292}
293
294impl FromReturnCode for SuicideErrorCode {
295    type Error = Self;
296
297    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
298        let maybe_result = Self::try_result_from_i8(val);
299        #[cfg(feature = "unsafe-return-conversion")]
300        unsafe {
301            maybe_result.unwrap_unchecked()
302        }
303        #[cfg(not(feature = "unsafe-return-conversion"))]
304        maybe_result.unwrap()
305    }
306
307    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
308        match val {
309            0 => Some(Ok(())),
310            -1 => Some(Err(SuicideErrorCode::NotOwner)),
311            -4 => Some(Err(SuicideErrorCode::Busy)),
312            _ => None,
313        }
314    }
315}
316
317impl fmt::Display for SuicideErrorCode {
318    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
319        let msg: &'static str = match self {
320            SuicideErrorCode::NotOwner => "you are not the owner of this creep",
321            SuicideErrorCode::Busy => "the creep is still being spawned",
322        };
323
324        write!(f, "{}", msg)
325    }
326}
327
328impl Error for SuicideErrorCode {}
329
330impl From<SuicideErrorCode> for ErrorCode {
331    fn from(value: SuicideErrorCode) -> Self {
332        // Safety: SuicideErrorCode is repr(i8), so we can cast it to get the
333        // discriminant value, which will match the raw return code value that ErrorCode
334        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
335        // Safety: SuicideErrorCode discriminants are always error code values, and thus
336        // the Result returned here will always be an `Err` variant, so we can always
337        // extract the error without panicking
338        Self::result_from_i8(value as i8).unwrap_err()
339    }
340}
341
342/// Error codes used by
343/// [SharedCreepProperties::transfer](crate::SharedCreepProperties::transfer).
344///
345/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.transfer).
346///
347/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L428)
348#[derive(
349    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
350)]
351#[repr(i8)]
352pub enum TransferErrorCode {
353    NotOwner = -1,
354    Busy = -4,
355    NotEnoughResources = -6,
356    InvalidTarget = -7,
357    Full = -8,
358    NotInRange = -9,
359    InvalidArgs = -10,
360}
361
362impl FromReturnCode for TransferErrorCode {
363    type Error = Self;
364
365    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
366        let maybe_result = Self::try_result_from_i8(val);
367        #[cfg(feature = "unsafe-return-conversion")]
368        unsafe {
369            maybe_result.unwrap_unchecked()
370        }
371        #[cfg(not(feature = "unsafe-return-conversion"))]
372        maybe_result.unwrap()
373    }
374
375    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
376        match val {
377            0 => Some(Ok(())),
378            -1 => Some(Err(TransferErrorCode::NotOwner)),
379            -4 => Some(Err(TransferErrorCode::Busy)),
380            -6 => Some(Err(TransferErrorCode::NotEnoughResources)),
381            -7 => Some(Err(TransferErrorCode::InvalidTarget)),
382            -8 => Some(Err(TransferErrorCode::Full)),
383            -9 => Some(Err(TransferErrorCode::NotInRange)),
384            -10 => Some(Err(TransferErrorCode::InvalidArgs)),
385            _ => None,
386        }
387    }
388}
389
390impl fmt::Display for TransferErrorCode {
391    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
392        let msg: &'static str = match self {
393            TransferErrorCode::NotOwner => "you are not the owner of this creep",
394            TransferErrorCode::Busy => "the creep is still being spawned",
395            TransferErrorCode::NotEnoughResources => "the creep does not have the given amount of resources",
396            TransferErrorCode::InvalidTarget => "the target is not a valid object which can contain the specified resource",
397            TransferErrorCode::Full => "the target cannot receive any more resources",
398            TransferErrorCode::NotInRange => "the target is too far away",
399            TransferErrorCode::InvalidArgs => "the resourcetype is not one of the resource_* constants, or the amount is incorrect",
400        };
401
402        write!(f, "{}", msg)
403    }
404}
405
406impl Error for TransferErrorCode {}
407
408impl From<TransferErrorCode> for ErrorCode {
409    fn from(value: TransferErrorCode) -> Self {
410        // Safety: TransferErrorCode is repr(i8), so we can cast it to get the
411        // discriminant value, which will match the raw return code value that ErrorCode
412        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
413        // Safety: TransferErrorCode discriminants are always error code values, and
414        // thus the Result returned here will always be an `Err` variant, so we can
415        // always extract the error without panicking
416        Self::result_from_i8(value as i8).unwrap_err()
417    }
418}
419
420/// Error codes used by
421/// [SharedCreepProperties::withdraw](crate::SharedCreepProperties::withdraw).
422///
423/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.withdraw).
424///
425/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L493)
426#[derive(
427    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
428)]
429#[repr(i8)]
430pub enum WithdrawErrorCode {
431    NotOwner = -1,
432    Busy = -4,
433    NotEnoughResources = -6,
434    InvalidTarget = -7,
435    Full = -8,
436    NotInRange = -9,
437    InvalidArgs = -10,
438}
439
440impl FromReturnCode for WithdrawErrorCode {
441    type Error = Self;
442
443    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
444        let maybe_result = Self::try_result_from_i8(val);
445        #[cfg(feature = "unsafe-return-conversion")]
446        unsafe {
447            maybe_result.unwrap_unchecked()
448        }
449        #[cfg(not(feature = "unsafe-return-conversion"))]
450        maybe_result.unwrap()
451    }
452
453    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
454        match val {
455            0 => Some(Ok(())),
456            -1 => Some(Err(WithdrawErrorCode::NotOwner)),
457            -4 => Some(Err(WithdrawErrorCode::Busy)),
458            -6 => Some(Err(WithdrawErrorCode::NotEnoughResources)),
459            -7 => Some(Err(WithdrawErrorCode::InvalidTarget)),
460            -8 => Some(Err(WithdrawErrorCode::Full)),
461            -9 => Some(Err(WithdrawErrorCode::NotInRange)),
462            -10 => Some(Err(WithdrawErrorCode::InvalidArgs)),
463            _ => None,
464        }
465    }
466}
467
468impl fmt::Display for WithdrawErrorCode {
469    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
470        let msg: &'static str = match self {
471            WithdrawErrorCode::NotOwner => "you are not the owner of this creep, or there is a hostile rampart on top of the target",
472            WithdrawErrorCode::Busy => "the creep is still being spawned",
473            WithdrawErrorCode::NotEnoughResources => "the target does not have the given amount of resources",
474            WithdrawErrorCode::InvalidTarget => "the target is not a valid object which can contain the specified resource",
475            WithdrawErrorCode::Full => "the creep's carry is full",
476            WithdrawErrorCode::NotInRange => "the target is too far away",
477            WithdrawErrorCode::InvalidArgs => "the resourcetype is not one of the resource_* constants, or the amount is incorrect",
478        };
479
480        write!(f, "{}", msg)
481    }
482}
483
484impl Error for WithdrawErrorCode {}
485
486impl From<WithdrawErrorCode> for ErrorCode {
487    fn from(value: WithdrawErrorCode) -> Self {
488        // Safety: WithdrawErrorCode is repr(i8), so we can cast it to get the
489        // discriminant value, which will match the raw return code value that ErrorCode
490        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
491        // Safety: WithdrawErrorCode discriminants are always error code values, and
492        // thus the Result returned here will always be an `Err` variant, so we can
493        // always extract the error without panicking
494        Self::result_from_i8(value as i8).unwrap_err()
495    }
496}