tetratto_core/model/
mod.rs

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