screeps/enums/action_error_codes/
structureobserver_error_codes.rs1use std::{error::Error, fmt};
2
3use num_derive::FromPrimitive;
4use serde_repr::{Deserialize_repr, Serialize_repr};
5
6use crate::{constants::ErrorCode, FromReturnCode};
7
8#[derive(
15 Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
16)]
17#[repr(i8)]
18pub enum ObserveRoomErrorCode {
19 NotOwner = -1,
20 NotInRange = -9,
21 InvalidArgs = -10,
22 RclNotEnough = -14,
23}
24
25impl FromReturnCode for ObserveRoomErrorCode {
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(ObserveRoomErrorCode::NotOwner)),
42 -9 => Some(Err(ObserveRoomErrorCode::NotInRange)),
43 -10 => Some(Err(ObserveRoomErrorCode::InvalidArgs)),
44 -14 => Some(Err(ObserveRoomErrorCode::RclNotEnough)),
45 _ => None,
46 }
47 }
48}
49
50impl fmt::Display for ObserveRoomErrorCode {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 let msg: &'static str = match self {
53 ObserveRoomErrorCode::NotOwner => "you are not the owner of this structure",
54 ObserveRoomErrorCode::NotInRange => "room roomname is not in observing range",
55 ObserveRoomErrorCode::InvalidArgs => "roomname argument is not a valid room name value",
56 ObserveRoomErrorCode::RclNotEnough => {
57 "room controller level insufficient to use this structure"
58 }
59 };
60
61 write!(f, "{}", msg)
62 }
63}
64
65impl Error for ObserveRoomErrorCode {}
66
67impl From<ObserveRoomErrorCode> for ErrorCode {
68 fn from(value: ObserveRoomErrorCode) -> Self {
69 Self::result_from_i8(value as i8).unwrap_err()
76 }
77}