Skip to main content

nil_core/
error.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::continent::{ContinentIndex, Coord};
5use crate::infrastructure::building::level::BuildingLevel;
6use crate::infrastructure::building::{BuildingId, MineId, StorageId};
7use crate::military::army::ArmyId;
8use crate::military::maneuver::ManeuverId;
9use crate::military::unit::UnitId;
10use crate::npc::bot::BotId;
11use crate::npc::precursor::PrecursorId;
12use crate::player::PlayerId;
13use crate::report::ReportId;
14use serde::Serialize;
15use serde::ser::Serializer;
16use std::result::Result as StdResult;
17use strum::EnumIs;
18
19pub type Result<T, E = Error> = StdResult<T, E>;
20pub type AnyResult<T> = anyhow::Result<T>;
21
22#[derive(Clone, Debug, EnumIs, thiserror::Error)]
23#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
24#[cfg_attr(feature = "typescript", ts(as = "String"))]
25#[remain::sorted]
26pub enum Error {
27  #[error("Army not found")]
28  ArmyNotFound(ArmyId),
29
30  #[error("Army is not idle")]
31  ArmyNotIdle(ArmyId),
32
33  #[error("Bot already spawned: {0}")]
34  BotAlreadySpawned(BotId),
35
36  #[error("Bot not found: {0}")]
37  BotNotFound(BotId),
38
39  #[error("No stats found for building \"{0}\"")]
40  BuildingStatsNotFound(BuildingId),
41
42  #[error("No stats found for building \"{0}\" at level {1}")]
43  BuildingStatsNotFoundForLevel(BuildingId, BuildingLevel),
44
45  #[error("Building \"{0}\" is already at its minimum level")]
46  CannotDecreaseBuildingLevel(BuildingId),
47
48  #[error("Building \"{0}\" is already at its maximum level")]
49  CannotIncreaseBuildingLevel(BuildingId),
50
51  #[error("Cheating is not allowed in this world")]
52  CheatingNotAllowed,
53
54  #[error("City not found: {0}")]
55  CityNotFound(Coord),
56
57  #[error("Failed to deserialize event")]
58  FailedToDeserializeEvent,
59
60  #[error("Failed to read savedata file")]
61  FailedToReadSavedata,
62
63  #[error("Failed to serialize event")]
64  FailedToSerializeEvent,
65
66  #[error("Failed to write savedata file")]
67  FailedToWriteSavedata,
68
69  #[error("Not authorized to execute this action")]
70  Forbidden,
71
72  #[error("Index out of bounds: {0}")]
73  IndexOutOfBounds(ContinentIndex),
74
75  #[error("Insufficient resources")]
76  InsufficientResources,
77
78  #[error("Insufficient units")]
79  InsufficientUnits,
80
81  #[error("Expected maneuver to be pending, but it is done")]
82  ManeuverIsDone(ManeuverId),
83
84  #[error("Expected maneuver to be done, but it is pending")]
85  ManeuverIsPending(ManeuverId),
86
87  #[error("Maneuver is already returning")]
88  ManeuverIsReturning(ManeuverId),
89
90  #[error("Maneuver not found")]
91  ManeuverNotFound(ManeuverId),
92
93  #[error("No stats found for mine \"{0}\"")]
94  MineStatsNotFound(MineId),
95
96  #[error("No stats found for mine \"{0}\" at level {1}")]
97  MineStatsNotFoundForLevel(MineId, BuildingLevel),
98
99  #[error("No players in the world")]
100  NoPlayer,
101
102  #[error("Player \"{0}\" has already taken their turn")]
103  NotWaitingPlayer(PlayerId),
104
105  #[error("Origin and destination have the same coords")]
106  OriginIsDestination(Coord),
107
108  #[error("Player already spawned: {0}")]
109  PlayerAlreadySpawned(PlayerId),
110
111  #[error("Player not found: {0}")]
112  PlayerNotFound(PlayerId),
113
114  #[error("Precursor not found: {0}")]
115  PrecursorNotFound(PrecursorId),
116
117  #[error("Report not found")]
118  ReportNotFound(ReportId),
119
120  #[error("Round already started")]
121  RoundAlreadyStarted,
122
123  #[error("Round has pending players")]
124  RoundHasPendingPlayers,
125
126  #[error("Round has not started yet")]
127  RoundNotStarted,
128
129  #[error("No stats found for storage \"{0}\"")]
130  StorageStatsNotFound(StorageId),
131
132  #[error("No stats found for storage \"{0}\" at level {1}")]
133  StorageStatsNotFoundForLevel(StorageId, BuildingLevel),
134
135  #[error("Expected \"{0}\", got \"{1}\"")]
136  UnexpectedUnit(UnitId, UnitId),
137
138  #[error("No stats found for wall at level {0}")]
139  WallStatsNotFoundForLevel(BuildingLevel),
140
141  #[error("World is full")]
142  WorldIsFull,
143}
144
145impl Serialize for Error {
146  fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
147  where
148    S: Serializer,
149  {
150    serializer.serialize_str(self.to_string().as_str())
151  }
152}