1use nil_core::world::config::WorldId;
5use serde::Serialize;
6use serde::ser::Serializer;
7use std::convert::Infallible;
8use std::io;
9use std::result::Result as StdResult;
10use tokio::task::JoinError;
11
12pub use nil_core::error::Error as CoreError;
13pub use nil_server_database::error::Error as DatabaseError;
14
15pub type Result<T, E = Error> = StdResult<T, E>;
16pub type AnyResult<T> = anyhow::Result<T>;
17
18#[derive(Debug, thiserror::Error)]
19pub enum Error {
20 #[error("Incorrect username or password")]
21 IncorrectUserCredentials,
22
23 #[error("Incorrect world password")]
24 IncorrectWorldCredentials(WorldId),
25
26 #[error("Expected at most {max} characters, got {current}")]
27 MaxCharactersExceeded { max: usize, current: usize },
28
29 #[error("Missing password")]
30 MissingPassword,
31
32 #[error("World limit reached")]
33 WorldLimitReached,
34
35 #[error("World not found")]
36 WorldNotFound(WorldId),
37
38 #[error(transparent)]
39 Core(#[from] CoreError),
40 #[error(transparent)]
41 Database(#[from] DatabaseError),
42 #[error(transparent)]
43 Io(#[from] io::Error),
44 #[error(transparent)]
45 Unknown(#[from] anyhow::Error),
46}
47
48impl Serialize for Error {
49 fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
50 where
51 S: Serializer,
52 {
53 serializer.serialize_str(self.to_string().as_str())
54 }
55}
56
57impl<E> From<Result<Infallible, E>> for Error
58where
59 E: Into<Error>,
60{
61 fn from(value: Result<Infallible, E>) -> Self {
62 value.unwrap_err().into()
63 }
64}
65
66impl From<io::ErrorKind> for Error {
67 fn from(value: io::ErrorKind) -> Self {
68 Self::Io(io::Error::from(value))
69 }
70}
71
72impl From<JoinError> for Error {
73 fn from(err: JoinError) -> Self {
74 Self::Io(io::Error::from(err))
75 }
76}