screeps/enums/action_error_codes/
structurerampart_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 SetPublicErrorCode {
19 NotOwner = -1,
20}
21
22impl FromReturnCode for SetPublicErrorCode {
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 0 => Some(Ok(())),
38 -1 => Some(Err(SetPublicErrorCode::NotOwner)),
39 _ => None,
40 }
41 }
42}
43
44impl fmt::Display for SetPublicErrorCode {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 let msg: &'static str = match self {
47 SetPublicErrorCode::NotOwner => "you are not the owner of this structure",
48 };
49
50 write!(f, "{}", msg)
51 }
52}
53
54impl Error for SetPublicErrorCode {}
55
56impl From<SetPublicErrorCode> for ErrorCode {
57 fn from(value: SetPublicErrorCode) -> Self {
58 Self::result_from_i8(value as i8).unwrap_err()
65 }
66}