Skip to main content

tetratto_core/model/
mod.rs

1pub mod addr;
2pub mod apps;
3pub mod auth;
4pub mod carp;
5pub mod communities;
6pub mod communities_permissions;
7pub mod guest_logs;
8pub mod mail;
9pub mod moderation;
10pub mod oauth;
11pub mod permissions;
12pub mod reactions;
13pub mod requests;
14pub mod socket;
15pub mod stacks;
16pub mod uploads;
17
18use std::fmt::Display;
19use serde::{Deserialize, Serialize};
20
21#[derive(Serialize, Deserialize)]
22pub struct ApiReturn<T>
23where
24    T: Serialize,
25{
26    pub ok: bool,
27    pub message: String,
28    pub payload: T,
29}
30
31#[derive(Debug)]
32pub enum Error {
33    MiscError(String),
34    DatabaseConnection(String),
35    UserNotFound,
36    GeneralNotFound(String),
37    RegistrationDisabled,
38    DatabaseError(String),
39    IncorrectPassword,
40    NotAllowed,
41    AlreadyAuthenticated,
42    DataTooLong(String),
43    DataTooShort(String),
44    FileTooLarge,
45    FileTooSmall,
46    UsernameInUse,
47    TitleInUse,
48    QuestionsDisabled,
49    RequiresSupporter,
50    DrawingsDisabled,
51    AppHitStorageLimit,
52    DoesNotSupportField(String),
53    Unknown,
54}
55
56impl Display for Error {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        f.write_str(&match self {
59            Self::MiscError(msg) => msg.to_owned(),
60            Self::DatabaseConnection(msg) => msg.to_owned(),
61            Self::DatabaseError(msg) => format!("Database error: {msg}"),
62            Self::UserNotFound => "Unable to find user with given parameters".to_string(),
63            Self::GeneralNotFound(name) => format!("Unable to find requested {name}"),
64            Self::RegistrationDisabled => "Registration is disabled".to_string(),
65            Self::IncorrectPassword => "The given password is invalid".to_string(),
66            Self::NotAllowed => "You are not allowed to do this".to_string(),
67            Self::AlreadyAuthenticated => "Already authenticated".to_string(),
68            Self::DataTooLong(name) => format!("Given {name} is too long!"),
69            Self::DataTooShort(name) => format!("Given {name} is too short!"),
70            Self::FileTooLarge => "Given file is too large".to_string(),
71            Self::FileTooSmall => "Given file is too small".to_string(),
72            Self::UsernameInUse => "Username in use".to_string(),
73            Self::TitleInUse => "Title in use".to_string(),
74            Self::QuestionsDisabled => "You are not allowed to ask questions there".to_string(),
75            Self::RequiresSupporter => "Only site supporters can do this".to_string(),
76            Self::DrawingsDisabled => "You are not allowed to submit drawings there".to_string(),
77            Self::AppHitStorageLimit => "This app has already hit its storage limit, or will do so if this data is processed.".to_string(),
78            Self::DoesNotSupportField(name) => format!("{name} does not support this field"),
79            _ => format!("An unknown error as occurred: ({:?})", self),
80        })
81    }
82}
83
84impl<T> From<Error> for ApiReturn<T>
85where
86    T: Default + Serialize,
87{
88    fn from(val: Error) -> Self {
89        ApiReturn {
90            ok: false,
91            message: val.to_string(),
92            payload: T::default(),
93        }
94    }
95}
96
97pub type Result<T> = std::result::Result<T, Error>;